├── .gitignore ├── README.md ├── Adafruit_STMPE610.h ├── Adafruit_ILI9341.h ├── Adafruit_STMPE610.cpp ├── Adafruit_ILI9341.cpp └── ip-switch.ino /.gitignore: -------------------------------------------------------------------------------- 1 | .txt 2 | .pdf 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IP switch 2 | ![IP switch](https://remoteqth.com/img/slide-ip-sw-rev4-01.jpg) 3 | Based on [ESP32-GATEWAY](https://www.olimex.com/Products/IoT/ESP32/ESP32-GATEWAY/open-source-hardware) 4 | 5 | ## Controled from 6 | - [Manual IP switch MK2](https://github.com/ok1hra/Manual_IP_switch_MK2) | [Wiki](https://remoteqth.com/wiki/index.php?page=Manual+IP+switch+II) 7 | -------------------------------------------------------------------------------- /Adafruit_STMPE610.h: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is a library for the Adafruit STMPE610 Resistive 3 | touch screen controller breakout 4 | ----> http://www.adafruit.com/products/1571 5 | 6 | Check out the links above for our tutorials and wiring diagrams 7 | These breakouts use SPI or I2C to communicate 8 | 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Limor Fried/Ladyada for Adafruit Industries. 14 | MIT license, all text above must be included in any redistribution 15 | ****************************************************/ 16 | 17 | #ifndef _ADAFRUIT_STMPE610H_ 18 | #define _ADAFRUIT_STMPE610H_ 19 | 20 | #if ARDUINO >= 100 21 | #include "Arduino.h" 22 | #else 23 | #include "WProgram.h" 24 | #endif 25 | 26 | #include 27 | 28 | 29 | #define STMPE_ADDR 0x4d 30 | 31 | #define STMPE_SYS_CTRL1 0x03 32 | #define STMPE_SYS_CTRL1_RESET 0x02 33 | #define STMPE_SYS_CTRL2 0x04 34 | 35 | #define STMPE_TSC_CTRL 0x40 36 | #define STMPE_TSC_CTRL_EN 0x01 37 | #define STMPE_TSC_CTRL_XYZ 0x00 38 | #define STMPE_TSC_CTRL_XY 0x02 39 | 40 | #define STMPE_INT_CTRL 0x09 41 | #define STMPE_INT_CTRL_POL_HIGH 0x04 42 | #define STMPE_INT_CTRL_POL_LOW 0x00 43 | #define STMPE_INT_CTRL_EDGE 0x02 44 | #define STMPE_INT_CTRL_LEVEL 0x00 45 | #define STMPE_INT_CTRL_ENABLE 0x01 46 | #define STMPE_INT_CTRL_DISABLE 0x00 47 | 48 | 49 | 50 | #define STMPE_INT_EN 0x0A 51 | #define STMPE_INT_EN_TOUCHDET 0x01 52 | #define STMPE_INT_EN_FIFOTH 0x02 53 | #define STMPE_INT_EN_FIFOOF 0x04 54 | #define STMPE_INT_EN_FIFOFULL 0x08 55 | #define STMPE_INT_EN_FIFOEMPTY 0x10 56 | #define STMPE_INT_EN_ADC 0x40 57 | #define STMPE_INT_EN_GPIO 0x80 58 | 59 | #define STMPE_INT_STA 0x0B 60 | #define STMPE_INT_STA_TOUCHDET 0x01 61 | 62 | #define STMPE_ADC_CTRL1 0x20 63 | #define STMPE_ADC_CTRL1_12BIT 0x08 64 | #define STMPE_ADC_CTRL1_10BIT 0x00 65 | 66 | #define STMPE_ADC_CTRL2 0x21 67 | #define STMPE_ADC_CTRL2_1_625MHZ 0x00 68 | #define STMPE_ADC_CTRL2_3_25MHZ 0x01 69 | #define STMPE_ADC_CTRL2_6_5MHZ 0x02 70 | 71 | #define STMPE_TSC_CFG 0x41 72 | #define STMPE_TSC_CFG_1SAMPLE 0x00 73 | #define STMPE_TSC_CFG_2SAMPLE 0x40 74 | #define STMPE_TSC_CFG_4SAMPLE 0x80 75 | #define STMPE_TSC_CFG_8SAMPLE 0xC0 76 | #define STMPE_TSC_CFG_DELAY_10US 0x00 77 | #define STMPE_TSC_CFG_DELAY_50US 0x08 78 | #define STMPE_TSC_CFG_DELAY_100US 0x10 79 | #define STMPE_TSC_CFG_DELAY_500US 0x18 80 | #define STMPE_TSC_CFG_DELAY_1MS 0x20 81 | #define STMPE_TSC_CFG_DELAY_5MS 0x28 82 | #define STMPE_TSC_CFG_DELAY_10MS 0x30 83 | #define STMPE_TSC_CFG_DELAY_50MS 0x38 84 | #define STMPE_TSC_CFG_SETTLE_10US 0x00 85 | #define STMPE_TSC_CFG_SETTLE_100US 0x01 86 | #define STMPE_TSC_CFG_SETTLE_500US 0x02 87 | #define STMPE_TSC_CFG_SETTLE_1MS 0x03 88 | #define STMPE_TSC_CFG_SETTLE_5MS 0x04 89 | #define STMPE_TSC_CFG_SETTLE_10MS 0x05 90 | #define STMPE_TSC_CFG_SETTLE_50MS 0x06 91 | #define STMPE_TSC_CFG_SETTLE_100MS 0x07 92 | 93 | #define STMPE_FIFO_TH 0x4A 94 | 95 | #define STMPE_FIFO_SIZE 0x4C 96 | 97 | #define STMPE_FIFO_STA 0x4B 98 | #define STMPE_FIFO_STA_RESET 0x01 99 | #define STMPE_FIFO_STA_OFLOW 0x80 100 | #define STMPE_FIFO_STA_FULL 0x40 101 | #define STMPE_FIFO_STA_EMPTY 0x20 102 | #define STMPE_FIFO_STA_THTRIG 0x10 103 | 104 | #define STMPE_TSC_I_DRIVE 0x58 105 | #define STMPE_TSC_I_DRIVE_20MA 0x00 106 | #define STMPE_TSC_I_DRIVE_50MA 0x01 107 | 108 | #define STMPE_TSC_DATA_X 0x4D 109 | #define STMPE_TSC_DATA_Y 0x4F 110 | #define STMPE_TSC_FRACTION_Z 0x56 111 | 112 | #define STMPE_GPIO_SET_PIN 0x10 113 | #define STMPE_GPIO_CLR_PIN 0x11 114 | #define STMPE_GPIO_DIR 0x13 115 | #define STMPE_GPIO_ALT_FUNCT 0x17 116 | 117 | 118 | class TS_Point { 119 | public: 120 | TS_Point(void); 121 | TS_Point(int16_t x, int16_t y, int16_t z); 122 | 123 | bool operator==(TS_Point); 124 | bool operator!=(TS_Point); 125 | 126 | int16_t x, y, z; 127 | }; 128 | 129 | class Adafruit_STMPE610{ 130 | public: 131 | Adafruit_STMPE610(uint8_t cspin, uint8_t mosipin, uint8_t misopin, uint8_t clkpin); 132 | Adafruit_STMPE610(uint8_t cs); 133 | Adafruit_STMPE610(void); 134 | boolean begin(uint8_t i2caddr = STMPE_ADDR); 135 | 136 | void writeRegister8(uint8_t reg, uint8_t val); 137 | uint16_t readRegister16(uint8_t reg); 138 | uint8_t readRegister8(uint8_t reg); 139 | void readData(uint16_t *x, uint16_t *y, uint8_t *z); 140 | uint16_t getVersion(); 141 | boolean touched(void); 142 | boolean bufferEmpty(void); 143 | uint8_t bufferSize(void); 144 | TS_Point getPoint(void); 145 | 146 | private: 147 | uint8_t spiIn(); 148 | void spiOut(uint8_t x); 149 | 150 | int8_t _CS, _MOSI, _MISO, _CLK; 151 | uint8_t _i2caddr; 152 | 153 | int m_spiMode; 154 | }; 155 | 156 | #endif 157 | -------------------------------------------------------------------------------- /Adafruit_ILI9341.h: -------------------------------------------------------------------------------- 1 | /****************************************************************** 2 | This is our library for the Adafruit ILI9341 Breakout and Shield 3 | ----> http://www.adafruit.com/products/1651 4 | 5 | Check out the links above for our tutorials and wiring diagrams 6 | These displays use SPI to communicate, 4 or 5 pins are required 7 | to interface (RST is optional) 8 | Adafruit invests time and resources providing this open source 9 | code, please support Adafruit and open-source hardware by 10 | purchasing products from Adafruit! 11 | 12 | Written by Limor Fried/Ladyada for Adafruit Industries. 13 | MIT license, all text above must be included in any redistribution 14 | *******************************************************************/ 15 | 16 | #ifndef _ADAFRUIT_ILI9341H_ 17 | #define _ADAFRUIT_ILI9341H_ 18 | 19 | #if ARDUINO >= 100 20 | #include "Arduino.h" 21 | #include "Print.h" 22 | #else 23 | #include "WProgram.h" 24 | #endif 25 | #include 26 | #include "Adafruit_GFX.h" 27 | 28 | #if defined(ARDUINO_STM32_FEATHER) 29 | typedef volatile uint32 RwReg; 30 | #endif 31 | #if defined(ARDUINO_FEATHER52) 32 | typedef volatile uint32_t RwReg; 33 | #endif 34 | 35 | #define ILI9341_TFTWIDTH 240 36 | #define ILI9341_TFTHEIGHT 320 37 | 38 | #define ILI9341_NOP 0x00 39 | #define ILI9341_SWRESET 0x01 40 | #define ILI9341_RDDID 0x04 41 | #define ILI9341_RDDST 0x09 42 | 43 | #define ILI9341_SLPIN 0x10 44 | #define ILI9341_SLPOUT 0x11 45 | #define ILI9341_PTLON 0x12 46 | #define ILI9341_NORON 0x13 47 | 48 | #define ILI9341_RDMODE 0x0A 49 | #define ILI9341_RDMADCTL 0x0B 50 | #define ILI9341_RDPIXFMT 0x0C 51 | #define ILI9341_RDIMGFMT 0x0D 52 | #define ILI9341_RDSELFDIAG 0x0F 53 | 54 | #define ILI9341_INVOFF 0x20 55 | #define ILI9341_INVON 0x21 56 | #define ILI9341_GAMMASET 0x26 57 | #define ILI9341_DISPOFF 0x28 58 | #define ILI9341_DISPON 0x29 59 | 60 | #define ILI9341_CASET 0x2A 61 | #define ILI9341_PASET 0x2B 62 | #define ILI9341_RAMWR 0x2C 63 | #define ILI9341_RAMRD 0x2E 64 | 65 | #define ILI9341_PTLAR 0x30 66 | #define ILI9341_MADCTL 0x36 67 | #define ILI9341_VSCRSADD 0x37 68 | #define ILI9341_PIXFMT 0x3A 69 | 70 | #define ILI9341_FRMCTR1 0xB1 71 | #define ILI9341_FRMCTR2 0xB2 72 | #define ILI9341_FRMCTR3 0xB3 73 | #define ILI9341_INVCTR 0xB4 74 | #define ILI9341_DFUNCTR 0xB6 75 | 76 | #define ILI9341_PWCTR1 0xC0 77 | #define ILI9341_PWCTR2 0xC1 78 | #define ILI9341_PWCTR3 0xC2 79 | #define ILI9341_PWCTR4 0xC3 80 | #define ILI9341_PWCTR5 0xC4 81 | #define ILI9341_VMCTR1 0xC5 82 | #define ILI9341_VMCTR2 0xC7 83 | 84 | #define ILI9341_RDID1 0xDA 85 | #define ILI9341_RDID2 0xDB 86 | #define ILI9341_RDID3 0xDC 87 | #define ILI9341_RDID4 0xDD 88 | 89 | #define ILI9341_GMCTRP1 0xE0 90 | #define ILI9341_GMCTRN1 0xE1 91 | /* 92 | #define ILI9341_PWCTR6 0xFC 93 | 94 | */ 95 | 96 | // Color definitions 97 | #define ILI9341_BLACK 0x0000 /* 0, 0, 0 */ 98 | #define ILI9341_NAVY 0x000F /* 0, 0, 128 */ 99 | #define ILI9341_DARKGREEN 0x03E0 /* 0, 128, 0 */ 100 | #define ILI9341_DARKCYAN 0x03EF /* 0, 128, 128 */ 101 | #define ILI9341_MAROON 0x7800 /* 128, 0, 0 */ 102 | #define ILI9341_PURPLE 0x780F /* 128, 0, 128 */ 103 | #define ILI9341_OLIVE 0x7BE0 /* 128, 128, 0 */ 104 | #define ILI9341_LIGHTGREY 0xC618 /* 192, 192, 192 */ 105 | #define ILI9341_DARKGREY 0x7BEF /* 128, 128, 128 */ 106 | #define ILI9341_BLUE 0x001F /* 0, 0, 255 */ 107 | #define ILI9341_GREEN 0x07E0 /* 0, 255, 0 */ 108 | #define ILI9341_CYAN 0x07FF /* 0, 255, 255 */ 109 | #define ILI9341_RED 0xF800 /* 255, 0, 0 */ 110 | #define ILI9341_MAGENTA 0xF81F /* 255, 0, 255 */ 111 | #define ILI9341_YELLOW 0xFFE0 /* 255, 255, 0 */ 112 | #define ILI9341_WHITE 0xFFFF /* 255, 255, 255 */ 113 | #define ILI9341_ORANGE 0xFD20 /* 255, 165, 0 */ 114 | #define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */ 115 | #define ILI9341_PINK 0xF81F 116 | 117 | #if defined (__AVR__) || defined(TEENSYDUINO) || defined(ESP8266) || defined (ESP32) || defined(__arm__) 118 | #define USE_FAST_PINIO 119 | #endif 120 | 121 | class Adafruit_ILI9341 : public Adafruit_GFX { 122 | protected: 123 | 124 | public: 125 | Adafruit_ILI9341(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK, int8_t _RST = -1, int8_t _MISO = -1); 126 | Adafruit_ILI9341(int8_t _CS, int8_t _DC, int8_t _RST = -1); 127 | 128 | #ifndef ESP32 129 | void begin(uint32_t freq = 0); 130 | #else 131 | void begin(uint32_t freq = 0, SPIClass &spi=SPI); 132 | #endif 133 | void setRotation(uint8_t r); 134 | void invertDisplay(boolean i); 135 | void scrollTo(uint16_t y); 136 | 137 | // Required Non-Transaction 138 | void drawPixel(int16_t x, int16_t y, uint16_t color); 139 | 140 | // Transaction API 141 | void startWrite(void); 142 | void endWrite(void); 143 | void writePixel(int16_t x, int16_t y, uint16_t color); 144 | void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); 145 | void writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); 146 | void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); 147 | 148 | // Transaction API not used by GFX 149 | void setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h); 150 | void writePixel(uint16_t color); 151 | void writePixels(uint16_t * colors, uint32_t len); 152 | void writeColor(uint16_t color, uint32_t len); 153 | void pushColor(uint16_t color); 154 | 155 | // Recommended Non-Transaction 156 | void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); 157 | void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); 158 | void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); 159 | 160 | using Adafruit_GFX::drawRGBBitmap; // Check base class first 161 | void drawRGBBitmap(int16_t x, int16_t y, 162 | uint16_t *pcolors, int16_t w, int16_t h); 163 | 164 | uint8_t readcommand8(uint8_t reg, uint8_t index = 0); 165 | 166 | uint16_t color565(uint8_t r, uint8_t g, uint8_t b); 167 | 168 | private: 169 | #ifdef ESP32 170 | SPIClass _spi; 171 | #endif 172 | uint32_t _freq; 173 | #if defined (__AVR__) || defined(TEENSYDUINO) 174 | int8_t _cs, _dc, _rst, _sclk, _mosi, _miso; 175 | #ifdef USE_FAST_PINIO 176 | volatile uint8_t *mosiport, *misoport, *clkport, *dcport, *csport; 177 | uint8_t mosipinmask, misopinmask, clkpinmask, cspinmask, dcpinmask; 178 | #endif 179 | #elif defined (__arm__) 180 | int32_t _cs, _dc, _rst, _sclk, _mosi, _miso; 181 | #ifdef USE_FAST_PINIO 182 | volatile RwReg *mosiport, *misoport, *clkport, *dcport, *csport; 183 | uint32_t mosipinmask, misopinmask, clkpinmask, cspinmask, dcpinmask; 184 | #endif 185 | #elif defined (ESP8266) || defined (ESP32) 186 | int8_t _cs, _dc, _rst, _sclk, _mosi, _miso; 187 | #ifdef USE_FAST_PINIO 188 | volatile uint32_t *mosiport, *misoport, *clkport, *dcport, *csport; 189 | uint32_t mosipinmask, misopinmask, clkpinmask, cspinmask, dcpinmask; 190 | #endif 191 | #else 192 | int8_t _cs, _dc, _rst, _sclk, _mosi, _miso; 193 | #endif 194 | 195 | void writeCommand(uint8_t cmd); 196 | void spiWrite(uint8_t v); 197 | uint8_t spiRead(void); 198 | }; 199 | 200 | #endif 201 | -------------------------------------------------------------------------------- /Adafruit_STMPE610.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is a library for the Adafruit STMPE610 Resistive 3 | touch screen controller breakout 4 | ----> http://www.adafruit.com/products/1571 5 | 6 | Check out the links above for our tutorials and wiring diagrams 7 | These breakouts use SPI or I2C to communicate 8 | 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Limor Fried/Ladyada for Adafruit Industries. 14 | MIT license, all text above must be included in any redistribution 15 | ****************************************************/ 16 | 17 | 18 | #if ARDUINO >= 100 19 | #include "Arduino.h" 20 | #else 21 | #include "WProgram.h" 22 | #endif 23 | 24 | #include 25 | #include 26 | 27 | #include "Adafruit_STMPE610.h" 28 | 29 | #if defined (SPI_HAS_TRANSACTION) 30 | // SPI transaction support allows managing SPI settings and prevents 31 | // conflicts between libraries, with a hardware-neutral interface 32 | static SPISettings mySPISettings; 33 | #elif defined (__AVR__) 34 | static uint8_t SPCRbackup; 35 | static uint8_t mySPCR; 36 | #endif 37 | 38 | /**************************************************************************/ 39 | /*! 40 | @brief Instantiates a new STMPE610 class 41 | */ 42 | /**************************************************************************/ 43 | // software SPI 44 | Adafruit_STMPE610::Adafruit_STMPE610(uint8_t cspin, uint8_t mosipin, uint8_t misopin, uint8_t clkpin) { 45 | _CS = cspin; 46 | _MOSI = mosipin; 47 | _MISO = misopin; 48 | _CLK = clkpin; 49 | } 50 | 51 | // hardware SPI 52 | Adafruit_STMPE610::Adafruit_STMPE610(uint8_t cspin) { 53 | _CS = cspin; 54 | _MOSI = _MISO = _CLK = -1; 55 | } 56 | 57 | // I2C 58 | Adafruit_STMPE610::Adafruit_STMPE610() { 59 | // use i2c 60 | _CS = -1; 61 | } 62 | 63 | 64 | /**************************************************************************/ 65 | /*! 66 | @brief Setups the HW 67 | */ 68 | /**************************************************************************/ 69 | boolean Adafruit_STMPE610::begin(uint8_t i2caddr) { 70 | if (_CS != -1 && _CLK == -1) { 71 | // hardware SPI 72 | pinMode(_CS, OUTPUT); 73 | digitalWrite(_CS, HIGH); 74 | 75 | #if defined (SPI_HAS_TRANSACTION) 76 | SPI.begin(); 77 | mySPISettings = SPISettings(1000000, MSBFIRST, SPI_MODE0); 78 | #elif defined (__AVR__) 79 | SPCRbackup = SPCR; 80 | SPI.begin(); 81 | SPI.setClockDivider(SPI_CLOCK_DIV16); 82 | SPI.setDataMode(SPI_MODE0); 83 | mySPCR = SPCR; // save our preferred state 84 | //Serial.print("mySPCR = 0x"); Serial.println(SPCR, HEX); 85 | SPCR = SPCRbackup; // then restore 86 | #elif defined (__arm__) 87 | SPI.begin(); 88 | SPI.setClockDivider(84); 89 | SPI.setDataMode(SPI_MODE0); 90 | #endif 91 | m_spiMode = SPI_MODE0; 92 | } else if (_CS != -1) { 93 | // software SPI 94 | pinMode(_CLK, OUTPUT); 95 | pinMode(_CS, OUTPUT); 96 | pinMode(_MOSI, OUTPUT); 97 | pinMode(_MISO, INPUT); 98 | } else { 99 | Wire.begin(); 100 | _i2caddr = i2caddr; 101 | return true; 102 | } 103 | 104 | // try mode0 105 | if (getVersion() != 0x811) { 106 | if (_CS != -1 && _CLK == -1) { 107 | //Serial.println("try MODE1"); 108 | #if defined (SPI_HAS_TRANSACTION) 109 | mySPISettings = SPISettings(1000000, MSBFIRST, SPI_MODE1); 110 | #elif defined (__AVR__) 111 | SPCRbackup = SPCR; 112 | SPI.begin(); 113 | SPI.setDataMode(SPI_MODE1); 114 | SPI.setClockDivider(SPI_CLOCK_DIV16); 115 | mySPCR = SPCR; // save our new preferred state 116 | //Serial.print("mySPCR = 0x"); Serial.println(SPCR, HEX); 117 | SPCR = SPCRbackup; // then restore 118 | #elif defined (__arm__) 119 | SPI.setClockDivider(84); 120 | SPI.setDataMode(SPI_MODE1); 121 | #endif 122 | m_spiMode = SPI_MODE1; 123 | 124 | if (getVersion() != 0x811) { 125 | return false; 126 | } 127 | } else { 128 | return false; 129 | } 130 | } 131 | writeRegister8(STMPE_SYS_CTRL1, STMPE_SYS_CTRL1_RESET); 132 | delay(10); 133 | 134 | for (uint8_t i=0; i<65; i++) { 135 | readRegister8(i); 136 | } 137 | 138 | writeRegister8(STMPE_SYS_CTRL2, 0x0); // turn on clocks! 139 | writeRegister8(STMPE_TSC_CTRL, STMPE_TSC_CTRL_XYZ | STMPE_TSC_CTRL_EN); // XYZ and enable! 140 | //Serial.println(readRegister8(STMPE_TSC_CTRL), HEX); 141 | writeRegister8(STMPE_INT_EN, STMPE_INT_EN_TOUCHDET); 142 | writeRegister8(STMPE_ADC_CTRL1, STMPE_ADC_CTRL1_10BIT | (0x6 << 4)); // 96 clocks per conversion 143 | writeRegister8(STMPE_ADC_CTRL2, STMPE_ADC_CTRL2_6_5MHZ); 144 | writeRegister8(STMPE_TSC_CFG, STMPE_TSC_CFG_4SAMPLE | STMPE_TSC_CFG_DELAY_1MS | STMPE_TSC_CFG_SETTLE_5MS); 145 | writeRegister8(STMPE_TSC_FRACTION_Z, 0x6); 146 | writeRegister8(STMPE_FIFO_TH, 1); 147 | writeRegister8(STMPE_FIFO_STA, STMPE_FIFO_STA_RESET); 148 | writeRegister8(STMPE_FIFO_STA, 0); // unreset 149 | writeRegister8(STMPE_TSC_I_DRIVE, STMPE_TSC_I_DRIVE_50MA); 150 | writeRegister8(STMPE_INT_STA, 0xFF); // reset all ints 151 | writeRegister8(STMPE_INT_CTRL, STMPE_INT_CTRL_POL_HIGH | STMPE_INT_CTRL_ENABLE); 152 | 153 | #if defined (__AVR__) && !defined (SPI_HAS_TRANSACTION) 154 | if (_CS != -1 && _CLK == -1) 155 | SPCR = SPCRbackup; // restore SPI state 156 | #endif 157 | return true; 158 | } 159 | 160 | boolean Adafruit_STMPE610::touched(void) { 161 | return (readRegister8(STMPE_TSC_CTRL) & 0x80); 162 | } 163 | 164 | boolean Adafruit_STMPE610::bufferEmpty(void) { 165 | return (readRegister8(STMPE_FIFO_STA) & STMPE_FIFO_STA_EMPTY); 166 | } 167 | 168 | uint8_t Adafruit_STMPE610::bufferSize(void) { 169 | return readRegister8(STMPE_FIFO_SIZE); 170 | } 171 | 172 | uint16_t Adafruit_STMPE610::getVersion() { 173 | uint16_t v; 174 | //Serial.print("get version"); 175 | v = readRegister8(0); 176 | v <<= 8; 177 | v |= readRegister8(1); 178 | //Serial.print("Version: 0x"); Serial.println(v, HEX); 179 | return v; 180 | } 181 | 182 | 183 | /*****************************/ 184 | 185 | void Adafruit_STMPE610::readData(uint16_t *x, uint16_t *y, uint8_t *z) { 186 | uint8_t data[4]; 187 | Wire.requestFrom(0x4d, 5); 188 | *z = Wire.read(); 189 | for (uint8_t i=0; i<4; i++) { 190 | data[i] = Wire.read();//readRegister8(0xD7); //SPI.transfer(0x00); 191 | // Serial.print("0x"); Serial.print(data[i], HEX); Serial.print(" / "); 192 | } 193 | *x = data[1]; 194 | *x <<= 8; 195 | *x |= data[0]; 196 | *y = data[3]; 197 | *y <<= 8; 198 | *y |= data[2]; 199 | // *z = data[3]; 200 | 201 | // if (bufferEmpty()) 202 | // writeRegister8(STMPE_INT_STA, 0xFF); // reset all ints 203 | Wire.endTransmission(); 204 | } 205 | 206 | TS_Point Adafruit_STMPE610::getPoint(void) { 207 | uint16_t x, y; 208 | uint8_t z; 209 | readData(&x, &y, &z); 210 | 211 | return TS_Point(x, y, z); 212 | } 213 | 214 | uint8_t Adafruit_STMPE610::spiIn() { 215 | if (_CLK == -1) { 216 | #if defined (SPI_HAS_TRANSACTION) 217 | uint8_t d = SPI.transfer(0); 218 | return d; 219 | #elif defined (__AVR__) 220 | SPCRbackup = SPCR; 221 | SPCR = mySPCR; 222 | uint8_t d = SPI.transfer(0); 223 | SPCR = SPCRbackup; 224 | return d; 225 | #elif defined (__arm__) 226 | SPI.setClockDivider(84); 227 | SPI.setDataMode(m_spiMode); 228 | uint8_t d = SPI.transfer(0); 229 | return d; 230 | #endif 231 | } 232 | else 233 | return shiftIn(_MISO, _CLK, MSBFIRST); 234 | } 235 | void Adafruit_STMPE610::spiOut(uint8_t x) { 236 | if (_CLK == -1) { 237 | #if defined (SPI_HAS_TRANSACTION) 238 | SPI.transfer(x); 239 | #elif defined (__AVR__) 240 | SPCRbackup = SPCR; 241 | SPCR = mySPCR; 242 | SPI.transfer(x); 243 | SPCR = SPCRbackup; 244 | #elif defined (__arm__) 245 | SPI.setClockDivider(84); 246 | SPI.setDataMode(m_spiMode); 247 | SPI.transfer(x); 248 | #endif 249 | } 250 | else 251 | shiftOut(_MOSI, _CLK, MSBFIRST, x); 252 | } 253 | 254 | uint8_t Adafruit_STMPE610::readRegister8(uint8_t reg) { 255 | uint8_t x ; 256 | if (_CS == -1) { 257 | // use i2c 258 | Wire.beginTransmission(_i2caddr); 259 | Wire.write((byte)reg); 260 | Wire.endTransmission(); 261 | Wire.beginTransmission(_i2caddr); 262 | Wire.requestFrom(_i2caddr, (byte)1); 263 | x = Wire.read(); 264 | Wire.endTransmission(); 265 | 266 | //Serial.print("$"); Serial.print(reg, HEX); 267 | //Serial.print(": 0x"); Serial.println(x, HEX); 268 | } else { 269 | #if defined (SPI_HAS_TRANSACTION) 270 | if (_CLK == -1) SPI.beginTransaction(mySPISettings); 271 | #endif 272 | digitalWrite(_CS, LOW); 273 | spiOut(0x80 | reg); 274 | spiOut(0x00); 275 | x = spiIn(); 276 | digitalWrite(_CS, HIGH); 277 | #if defined (SPI_HAS_TRANSACTION) 278 | if (_CLK == -1) SPI.endTransaction(); 279 | #endif 280 | } 281 | 282 | return x; 283 | } 284 | uint16_t Adafruit_STMPE610::readRegister16(uint8_t reg) { 285 | uint16_t x = 0; 286 | if (_CS == -1) { 287 | // use i2c 288 | Wire.beginTransmission(_i2caddr); 289 | Wire.write((byte)reg); 290 | Wire.endTransmission(); 291 | Wire.requestFrom(_i2caddr, (byte)2); 292 | x = Wire.read(); 293 | x<<=8; 294 | x |= Wire.read(); 295 | Wire.endTransmission(); 296 | 297 | } if (_CLK == -1) { 298 | // hardware SPI 299 | #if defined (SPI_HAS_TRANSACTION) 300 | if (_CLK == -1) SPI.beginTransaction(mySPISettings); 301 | #endif 302 | digitalWrite(_CS, LOW); 303 | spiOut(0x80 | reg); 304 | spiOut(0x00); 305 | x = spiIn(); 306 | x<<=8; 307 | x |= spiIn(); 308 | digitalWrite(_CS, HIGH); 309 | #if defined (SPI_HAS_TRANSACTION) 310 | if (_CLK == -1) SPI.endTransaction(); 311 | #endif 312 | } 313 | 314 | //Serial.print("$"); Serial.print(reg, HEX); 315 | //Serial.print(": 0x"); Serial.println(x, HEX); 316 | return x; 317 | } 318 | 319 | void Adafruit_STMPE610::writeRegister8(uint8_t reg, uint8_t val) { 320 | if (_CS == -1) { 321 | // use i2c 322 | Wire.beginTransmission(_i2caddr); 323 | Wire.write((byte)reg); 324 | Wire.write(val); 325 | Wire.endTransmission(); 326 | } else { 327 | #if defined (SPI_HAS_TRANSACTION) 328 | if (_CLK == -1) SPI.beginTransaction(mySPISettings); 329 | #endif 330 | digitalWrite(_CS, LOW); 331 | spiOut(reg); 332 | spiOut(val); 333 | digitalWrite(_CS, HIGH); 334 | #if defined (SPI_HAS_TRANSACTION) 335 | if (_CLK == -1) SPI.endTransaction(); 336 | #endif 337 | } 338 | } 339 | 340 | /****************/ 341 | 342 | TS_Point::TS_Point(void) { 343 | x = y = 0; 344 | } 345 | 346 | TS_Point::TS_Point(int16_t x0, int16_t y0, int16_t z0) { 347 | x = x0; 348 | y = y0; 349 | z = z0; 350 | } 351 | 352 | bool TS_Point::operator==(TS_Point p1) { 353 | return ((p1.x == x) && (p1.y == y) && (p1.z == z)); 354 | } 355 | 356 | bool TS_Point::operator!=(TS_Point p1) { 357 | return ((p1.x != x) || (p1.y != y) || (p1.z != z)); 358 | } 359 | -------------------------------------------------------------------------------- /Adafruit_ILI9341.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | This is our library for the Adafruit ILI9341 Breakout and Shield 3 | ----> http://www.adafruit.com/products/1651 4 | 5 | Check out the links above for our tutorials and wiring diagrams 6 | These displays use SPI to communicate, 4 or 5 pins are required to 7 | interface (RST is optional) 8 | Adafruit invests time and resources providing this open source code, 9 | please support Adafruit and open-source hardware by purchasing 10 | products from Adafruit! 11 | 12 | Written by Limor Fried/Ladyada for Adafruit Industries. 13 | MIT license, all text above must be included in any redistribution 14 | ****************************************************/ 15 | 16 | #include "Adafruit_ILI9341.h" 17 | #ifndef ARDUINO_STM32_FEATHER 18 | #include "pins_arduino.h" 19 | #ifndef RASPI 20 | #include "wiring_private.h" 21 | #endif 22 | #endif 23 | #include 24 | 25 | 26 | #define MADCTL_MY 0x80 27 | #define MADCTL_MX 0x40 28 | #define MADCTL_MV 0x20 29 | #define MADCTL_ML 0x10 30 | #define MADCTL_RGB 0x00 31 | #define MADCTL_BGR 0x08 32 | #define MADCTL_MH 0x04 33 | 34 | /* 35 | * Control Pins 36 | * */ 37 | 38 | #ifdef USE_FAST_PINIO 39 | #define SPI_DC_HIGH() *dcport |= dcpinmask 40 | #define SPI_DC_LOW() *dcport &= ~dcpinmask 41 | #define SPI_CS_HIGH() *csport |= cspinmask 42 | #define SPI_CS_LOW() *csport &= ~cspinmask 43 | #else 44 | #define SPI_DC_HIGH() digitalWrite(_dc, HIGH) 45 | #define SPI_DC_LOW() digitalWrite(_dc, LOW) 46 | #define SPI_CS_HIGH() digitalWrite(_cs, HIGH) 47 | #define SPI_CS_LOW() digitalWrite(_cs, LOW) 48 | #endif 49 | 50 | /* 51 | * Software SPI Macros 52 | * */ 53 | 54 | #ifdef USE_FAST_PINIO 55 | #define SSPI_MOSI_HIGH() *mosiport |= mosipinmask 56 | #define SSPI_MOSI_LOW() *mosiport &= ~mosipinmask 57 | #define SSPI_SCK_HIGH() *clkport |= clkpinmask 58 | #define SSPI_SCK_LOW() *clkport &= ~clkpinmask 59 | #define SSPI_MISO_READ() ((*misoport & misopinmask) != 0) 60 | #else 61 | #define SSPI_MOSI_HIGH() digitalWrite(_mosi, HIGH) 62 | #define SSPI_MOSI_LOW() digitalWrite(_mosi, LOW) 63 | #define SSPI_SCK_HIGH() digitalWrite(_sclk, HIGH) 64 | #define SSPI_SCK_LOW() digitalWrite(_sclk, LOW) 65 | #define SSPI_MISO_READ() digitalRead(_miso) 66 | #endif 67 | 68 | #define SSPI_BEGIN_TRANSACTION() 69 | #define SSPI_END_TRANSACTION() 70 | #define SSPI_WRITE(v) spiWrite(v) 71 | #define SSPI_WRITE16(s) SSPI_WRITE((s) >> 8); SSPI_WRITE(s) 72 | #define SSPI_WRITE32(l) SSPI_WRITE((l) >> 24); SSPI_WRITE((l) >> 16); SSPI_WRITE((l) >> 8); SSPI_WRITE(l) 73 | #define SSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<(l); i+=2){ SSPI_WRITE(((uint8_t*)(c))[i+1]); SSPI_WRITE(((uint8_t*)(c))[i]); } 74 | 75 | /* 76 | * Hardware SPI Macros 77 | * */ 78 | 79 | #ifndef ESP32 80 | #define SPI_OBJECT SPI 81 | #else 82 | #define SPI_OBJECT _spi 83 | #endif 84 | 85 | #if defined (__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1) 86 | #define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(SPI_CLOCK_DIV2); 87 | #elif defined (__arm__) 88 | #define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(11); 89 | #elif defined(ESP8266) || defined(ESP32) 90 | #define HSPI_SET_CLOCK() SPI_OBJECT.setFrequency(_freq); 91 | #elif defined(RASPI) 92 | #define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq); 93 | #elif defined(ARDUINO_ARCH_STM32F1) 94 | #define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq); 95 | #else 96 | #define HSPI_SET_CLOCK() 97 | #endif 98 | 99 | #ifdef SPI_HAS_TRANSACTION 100 | #define HSPI_BEGIN_TRANSACTION() SPI_OBJECT.beginTransaction(SPISettings(_freq, MSBFIRST, SPI_MODE0)) 101 | #define HSPI_END_TRANSACTION() SPI_OBJECT.endTransaction() 102 | #else 103 | #define HSPI_BEGIN_TRANSACTION() HSPI_SET_CLOCK(); SPI_OBJECT.setBitOrder(MSBFIRST); SPI_OBJECT.setDataMode(SPI_MODE0) 104 | #define HSPI_END_TRANSACTION() 105 | #endif 106 | 107 | #ifdef ESP32 108 | #define SPI_HAS_WRITE_PIXELS 109 | #endif 110 | #if defined(ESP8266) || defined(ESP32) 111 | // Optimized SPI (ESP8266 and ESP32) 112 | #define HSPI_READ() SPI_OBJECT.transfer(0) 113 | #define HSPI_WRITE(b) SPI_OBJECT.write(b) 114 | #define HSPI_WRITE16(s) SPI_OBJECT.write16(s) 115 | #define HSPI_WRITE32(l) SPI_OBJECT.write32(l) 116 | #ifdef SPI_HAS_WRITE_PIXELS 117 | #define SPI_MAX_PIXELS_AT_ONCE 32 118 | #define HSPI_WRITE_PIXELS(c,l) SPI_OBJECT.writePixels(c,l) 119 | #else 120 | #define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<((l)/2); i++){ SPI_WRITE16(((uint16_t*)(c))[i]); } 121 | #endif 122 | #else 123 | // Standard Byte-by-Byte SPI 124 | 125 | #if defined (__AVR__) || defined(TEENSYDUINO) 126 | static inline uint8_t _avr_spi_read(void) __attribute__((always_inline)); 127 | static inline uint8_t _avr_spi_read(void) { 128 | uint8_t r = 0; 129 | SPDR = r; 130 | while(!(SPSR & _BV(SPIF))); 131 | r = SPDR; 132 | return r; 133 | } 134 | #define HSPI_WRITE(b) {SPDR = (b); while(!(SPSR & _BV(SPIF)));} 135 | #define HSPI_READ() _avr_spi_read() 136 | #else 137 | #define HSPI_WRITE(b) SPI_OBJECT.transfer((uint8_t)(b)) 138 | #define HSPI_READ() HSPI_WRITE(0) 139 | #endif 140 | #define HSPI_WRITE16(s) HSPI_WRITE((s) >> 8); HSPI_WRITE(s) 141 | #define HSPI_WRITE32(l) HSPI_WRITE((l) >> 24); HSPI_WRITE((l) >> 16); HSPI_WRITE((l) >> 8); HSPI_WRITE(l) 142 | #define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<(l); i+=2){ HSPI_WRITE(((uint8_t*)(c))[i+1]); HSPI_WRITE(((uint8_t*)(c))[i]); } 143 | #endif 144 | 145 | /* 146 | * Final SPI Macros 147 | * */ 148 | #if defined (ARDUINO_ARCH_ARC32) 149 | #define SPI_DEFAULT_FREQ 16000000 150 | #elif defined (__AVR__) || defined(TEENSYDUINO) 151 | #define SPI_DEFAULT_FREQ 8000000 152 | #elif defined(ESP8266) || defined(ESP32) 153 | #define SPI_DEFAULT_FREQ 40000000 154 | #elif defined(RASPI) 155 | #define SPI_DEFAULT_FREQ 80000000 156 | #elif defined(ARDUINO_ARCH_STM32F1) 157 | #define SPI_DEFAULT_FREQ 36000000 158 | #else 159 | #define SPI_DEFAULT_FREQ 24000000 160 | #endif 161 | 162 | #define SPI_BEGIN() if(_sclk < 0){SPI_OBJECT.begin();} 163 | #define SPI_BEGIN_TRANSACTION() if(_sclk < 0){HSPI_BEGIN_TRANSACTION();} 164 | #define SPI_END_TRANSACTION() if(_sclk < 0){HSPI_END_TRANSACTION();} 165 | #define SPI_WRITE16(s) if(_sclk < 0){HSPI_WRITE16(s);}else{SSPI_WRITE16(s);} 166 | #define SPI_WRITE32(l) if(_sclk < 0){HSPI_WRITE32(l);}else{SSPI_WRITE32(l);} 167 | #define SPI_WRITE_PIXELS(c,l) if(_sclk < 0){HSPI_WRITE_PIXELS(c,l);}else{SSPI_WRITE_PIXELS(c,l);} 168 | 169 | // Pass 8-bit (each) R,G,B, get back 16-bit packed color 170 | uint16_t Adafruit_ILI9341::color565(uint8_t r, uint8_t g, uint8_t b) { 171 | return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3); 172 | } 173 | 174 | Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t mosi, 175 | int8_t sclk, int8_t rst, int8_t miso) : Adafruit_GFX(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT) { 176 | _cs = cs; 177 | _dc = dc; 178 | _rst = rst; 179 | _sclk = sclk; 180 | _mosi = mosi; 181 | _miso = miso; 182 | _freq = 0; 183 | #ifdef USE_FAST_PINIO 184 | csport = portOutputRegister(digitalPinToPort(_cs)); 185 | cspinmask = digitalPinToBitMask(_cs); 186 | dcport = portOutputRegister(digitalPinToPort(_dc)); 187 | dcpinmask = digitalPinToBitMask(_dc); 188 | clkport = portOutputRegister(digitalPinToPort(_sclk)); 189 | clkpinmask = digitalPinToBitMask(_sclk); 190 | mosiport = portOutputRegister(digitalPinToPort(_mosi)); 191 | mosipinmask = digitalPinToBitMask(_mosi); 192 | if(miso >= 0){ 193 | misoport = portInputRegister(digitalPinToPort(_miso)); 194 | misopinmask = digitalPinToBitMask(_miso); 195 | } else { 196 | misoport = 0; 197 | misopinmask = 0; 198 | } 199 | #endif 200 | } 201 | 202 | Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t rst) : Adafruit_GFX(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT) { 203 | _cs = cs; 204 | _dc = dc; 205 | _rst = rst; 206 | _sclk = -1; 207 | _mosi = -1; 208 | _miso = -1; 209 | _freq = 0; 210 | #ifdef USE_FAST_PINIO 211 | csport = portOutputRegister(digitalPinToPort(_cs)); 212 | cspinmask = digitalPinToBitMask(_cs); 213 | dcport = portOutputRegister(digitalPinToPort(_dc)); 214 | dcpinmask = digitalPinToBitMask(_dc); 215 | clkport = 0; 216 | clkpinmask = 0; 217 | mosiport = 0; 218 | mosipinmask = 0; 219 | misoport = 0; 220 | misopinmask = 0; 221 | #endif 222 | } 223 | 224 | 225 | #ifdef ESP32 226 | void Adafruit_ILI9341::begin(uint32_t freq, SPIClass &spi) 227 | #else 228 | void Adafruit_ILI9341::begin(uint32_t freq) 229 | #endif 230 | { 231 | #ifdef ESP32 232 | _spi = spi; 233 | #endif 234 | if(!freq){ 235 | freq = SPI_DEFAULT_FREQ; 236 | } 237 | _freq = freq; 238 | 239 | // Control Pins 240 | pinMode(_dc, OUTPUT); 241 | digitalWrite(_dc, LOW); 242 | pinMode(_cs, OUTPUT); 243 | digitalWrite(_cs, HIGH); 244 | 245 | // Software SPI 246 | if(_sclk >= 0){ 247 | pinMode(_mosi, OUTPUT); 248 | digitalWrite(_mosi, LOW); 249 | pinMode(_sclk, OUTPUT); 250 | digitalWrite(_sclk, HIGH); 251 | if(_miso >= 0){ 252 | pinMode(_miso, INPUT); 253 | } 254 | } 255 | 256 | // Hardware SPI 257 | SPI_BEGIN(); 258 | pinMode(_dc, OUTPUT); 259 | // toggle RST low to reset 260 | if (_rst >= 0) { 261 | pinMode(_rst, OUTPUT); 262 | digitalWrite(_rst, HIGH); 263 | delay(100); 264 | digitalWrite(_rst, LOW); 265 | delay(100); 266 | digitalWrite(_rst, HIGH); 267 | delay(200); 268 | } 269 | 270 | startWrite(); 271 | 272 | writeCommand(0xEF); 273 | spiWrite(0x03); 274 | spiWrite(0x80); 275 | spiWrite(0x02); 276 | 277 | writeCommand(0xCF); 278 | spiWrite(0x00); 279 | spiWrite(0XC1); 280 | spiWrite(0X30); 281 | 282 | writeCommand(0xED); 283 | spiWrite(0x64); 284 | spiWrite(0x03); 285 | spiWrite(0X12); 286 | spiWrite(0X81); 287 | 288 | writeCommand(0xE8); 289 | spiWrite(0x85); 290 | spiWrite(0x00); 291 | spiWrite(0x78); 292 | 293 | writeCommand(0xCB); 294 | spiWrite(0x39); 295 | spiWrite(0x2C); 296 | spiWrite(0x00); 297 | spiWrite(0x34); 298 | spiWrite(0x02); 299 | 300 | writeCommand(0xF7); 301 | spiWrite(0x20); 302 | 303 | writeCommand(0xEA); 304 | spiWrite(0x00); 305 | spiWrite(0x00); 306 | 307 | writeCommand(ILI9341_PWCTR1); //Power control 308 | spiWrite(0x23); //VRH[5:0] 309 | 310 | writeCommand(ILI9341_PWCTR2); //Power control 311 | spiWrite(0x10); //SAP[2:0];BT[3:0] 312 | 313 | writeCommand(ILI9341_VMCTR1); //VCM control 314 | spiWrite(0x3e); 315 | spiWrite(0x28); 316 | 317 | writeCommand(ILI9341_VMCTR2); //VCM control2 318 | spiWrite(0x86); //-- 319 | 320 | writeCommand(ILI9341_MADCTL); // Memory Access Control 321 | spiWrite(0x48); 322 | 323 | writeCommand(ILI9341_VSCRSADD); // Vertical scroll 324 | SPI_WRITE16(0); // Zero 325 | 326 | writeCommand(ILI9341_PIXFMT); 327 | spiWrite(0x55); 328 | 329 | writeCommand(ILI9341_FRMCTR1); 330 | spiWrite(0x00); 331 | spiWrite(0x18); 332 | 333 | writeCommand(ILI9341_DFUNCTR); // Display Function Control 334 | spiWrite(0x08); 335 | spiWrite(0x82); 336 | spiWrite(0x27); 337 | 338 | writeCommand(0xF2); // 3Gamma Function Disable 339 | spiWrite(0x00); 340 | 341 | writeCommand(ILI9341_GAMMASET); //Gamma curve selected 342 | spiWrite(0x01); 343 | 344 | writeCommand(ILI9341_GMCTRP1); //Set Gamma 345 | spiWrite(0x0F); 346 | spiWrite(0x31); 347 | spiWrite(0x2B); 348 | spiWrite(0x0C); 349 | spiWrite(0x0E); 350 | spiWrite(0x08); 351 | spiWrite(0x4E); 352 | spiWrite(0xF1); 353 | spiWrite(0x37); 354 | spiWrite(0x07); 355 | spiWrite(0x10); 356 | spiWrite(0x03); 357 | spiWrite(0x0E); 358 | spiWrite(0x09); 359 | spiWrite(0x00); 360 | 361 | writeCommand(ILI9341_GMCTRN1); //Set Gamma 362 | spiWrite(0x00); 363 | spiWrite(0x0E); 364 | spiWrite(0x14); 365 | spiWrite(0x03); 366 | spiWrite(0x11); 367 | spiWrite(0x07); 368 | spiWrite(0x31); 369 | spiWrite(0xC1); 370 | spiWrite(0x48); 371 | spiWrite(0x08); 372 | spiWrite(0x0F); 373 | spiWrite(0x0C); 374 | spiWrite(0x31); 375 | spiWrite(0x36); 376 | spiWrite(0x0F); 377 | 378 | writeCommand(ILI9341_SLPOUT); //Exit Sleep 379 | delay(120); 380 | writeCommand(ILI9341_DISPON); //Display on 381 | delay(120); 382 | endWrite(); 383 | 384 | _width = ILI9341_TFTWIDTH; 385 | _height = ILI9341_TFTHEIGHT; 386 | } 387 | 388 | void Adafruit_ILI9341::setRotation(uint8_t m) { 389 | rotation = m % 4; // can't be higher than 3 390 | switch (rotation) { 391 | case 0: 392 | m = (MADCTL_MX | MADCTL_BGR); 393 | _width = ILI9341_TFTWIDTH; 394 | _height = ILI9341_TFTHEIGHT; 395 | break; 396 | case 1: 397 | m = (MADCTL_MV | MADCTL_BGR); 398 | _width = ILI9341_TFTHEIGHT; 399 | _height = ILI9341_TFTWIDTH; 400 | break; 401 | case 2: 402 | m = (MADCTL_MY | MADCTL_BGR); 403 | _width = ILI9341_TFTWIDTH; 404 | _height = ILI9341_TFTHEIGHT; 405 | break; 406 | case 3: 407 | m = (MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR); 408 | _width = ILI9341_TFTHEIGHT; 409 | _height = ILI9341_TFTWIDTH; 410 | break; 411 | } 412 | 413 | startWrite(); 414 | writeCommand(ILI9341_MADCTL); 415 | spiWrite(m); 416 | endWrite(); 417 | } 418 | 419 | void Adafruit_ILI9341::invertDisplay(boolean i) { 420 | startWrite(); 421 | writeCommand(i ? ILI9341_INVON : ILI9341_INVOFF); 422 | endWrite(); 423 | } 424 | 425 | void Adafruit_ILI9341::scrollTo(uint16_t y) { 426 | startWrite(); 427 | writeCommand(ILI9341_VSCRSADD); 428 | SPI_WRITE16(y); 429 | endWrite(); 430 | } 431 | 432 | uint8_t Adafruit_ILI9341::spiRead() { 433 | if(_sclk < 0){ 434 | return HSPI_READ(); 435 | } 436 | if(_miso < 0){ 437 | return 0; 438 | } 439 | uint8_t r = 0; 440 | for (uint8_t i=0; i<8; i++) { 441 | SSPI_SCK_LOW(); 442 | SSPI_SCK_HIGH(); 443 | r <<= 1; 444 | if (SSPI_MISO_READ()){ 445 | r |= 0x1; 446 | } 447 | } 448 | return r; 449 | } 450 | 451 | void Adafruit_ILI9341::spiWrite(uint8_t b) { 452 | if(_sclk < 0){ 453 | HSPI_WRITE(b); 454 | return; 455 | } 456 | for(uint8_t bit = 0x80; bit; bit >>= 1){ 457 | if((b) & bit){ 458 | SSPI_MOSI_HIGH(); 459 | } else { 460 | SSPI_MOSI_LOW(); 461 | } 462 | SSPI_SCK_LOW(); 463 | SSPI_SCK_HIGH(); 464 | } 465 | } 466 | 467 | 468 | /* 469 | * Transaction API 470 | * */ 471 | 472 | void Adafruit_ILI9341::startWrite(void){ 473 | SPI_BEGIN_TRANSACTION(); 474 | SPI_CS_LOW(); 475 | } 476 | 477 | void Adafruit_ILI9341::endWrite(void){ 478 | SPI_CS_HIGH(); 479 | SPI_END_TRANSACTION(); 480 | } 481 | 482 | void Adafruit_ILI9341::writeCommand(uint8_t cmd){ 483 | SPI_DC_LOW(); 484 | spiWrite(cmd); 485 | SPI_DC_HIGH(); 486 | } 487 | 488 | void Adafruit_ILI9341::setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h) { 489 | uint32_t xa = ((uint32_t)x << 16) | (x+w-1); 490 | uint32_t ya = ((uint32_t)y << 16) | (y+h-1); 491 | writeCommand(ILI9341_CASET); // Column addr set 492 | SPI_WRITE32(xa); 493 | writeCommand(ILI9341_PASET); // Row addr set 494 | SPI_WRITE32(ya); 495 | writeCommand(ILI9341_RAMWR); // write to RAM 496 | } 497 | 498 | void Adafruit_ILI9341::pushColor(uint16_t color) { 499 | startWrite(); 500 | SPI_WRITE16(color); 501 | endWrite(); 502 | } 503 | 504 | 505 | void Adafruit_ILI9341::writePixel(uint16_t color){ 506 | SPI_WRITE16(color); 507 | } 508 | 509 | void Adafruit_ILI9341::writePixels(uint16_t * colors, uint32_t len){ 510 | SPI_WRITE_PIXELS((uint8_t*)colors , len * 2); 511 | } 512 | 513 | void Adafruit_ILI9341::writeColor(uint16_t color, uint32_t len){ 514 | #ifdef SPI_HAS_WRITE_PIXELS 515 | if(_sclk >= 0){ 516 | for (uint32_t t=0; t SPI_MAX_PIXELS_AT_ONCE)?SPI_MAX_PIXELS_AT_ONCE:len; 523 | uint16_t tlen = 0; 524 | 525 | for (uint32_t t=0; tblen)?blen:len; 531 | writePixels(temp, tlen); 532 | len -= tlen; 533 | } 534 | #else 535 | uint8_t hi = color >> 8, lo = color; 536 | if(_sclk < 0){ //AVR Optimization 537 | for (uint32_t t=len; t; t--){ 538 | HSPI_WRITE(hi); 539 | HSPI_WRITE(lo); 540 | } 541 | return; 542 | } 543 | for (uint32_t t=len; t; t--){ 544 | spiWrite(hi); 545 | spiWrite(lo); 546 | } 547 | #endif 548 | } 549 | 550 | void Adafruit_ILI9341::writePixel(int16_t x, int16_t y, uint16_t color) { 551 | if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return; 552 | setAddrWindow(x,y,1,1); 553 | writePixel(color); 554 | } 555 | 556 | void Adafruit_ILI9341::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color){ 557 | if((x >= _width) || (y >= _height)) return; 558 | int16_t x2 = x + w - 1, y2 = y + h - 1; 559 | if((x2 < 0) || (y2 < 0)) return; 560 | 561 | // Clip left/top 562 | if(x < 0) { 563 | x = 0; 564 | w = x2 + 1; 565 | } 566 | if(y < 0) { 567 | y = 0; 568 | h = y2 + 1; 569 | } 570 | 571 | // Clip right/bottom 572 | if(x2 >= _width) w = _width - x; 573 | if(y2 >= _height) h = _height - y; 574 | 575 | int32_t len = (int32_t)w * h; 576 | setAddrWindow(x, y, w, h); 577 | writeColor(color, len); 578 | } 579 | 580 | void Adafruit_ILI9341::writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color){ 581 | writeFillRect(x, y, 1, h, color); 582 | } 583 | 584 | void Adafruit_ILI9341::writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color){ 585 | writeFillRect(x, y, w, 1, color); 586 | } 587 | 588 | uint8_t Adafruit_ILI9341::readcommand8(uint8_t c, uint8_t index) { 589 | uint32_t freq = _freq; 590 | if(_freq > 24000000){ 591 | _freq = 24000000; 592 | } 593 | startWrite(); 594 | writeCommand(0xD9); // woo sekret command? 595 | spiWrite(0x10 + index); 596 | writeCommand(c); 597 | uint8_t r = spiRead(); 598 | endWrite(); 599 | _freq = freq; 600 | return r; 601 | } 602 | 603 | void Adafruit_ILI9341::drawPixel(int16_t x, int16_t y, uint16_t color){ 604 | startWrite(); 605 | writePixel(x, y, color); 606 | endWrite(); 607 | } 608 | 609 | void Adafruit_ILI9341::drawFastVLine(int16_t x, int16_t y, 610 | int16_t h, uint16_t color) { 611 | startWrite(); 612 | writeFastVLine(x, y, h, color); 613 | endWrite(); 614 | } 615 | 616 | void Adafruit_ILI9341::drawFastHLine(int16_t x, int16_t y, 617 | int16_t w, uint16_t color) { 618 | startWrite(); 619 | writeFastHLine(x, y, w, color); 620 | endWrite(); 621 | } 622 | 623 | void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, 624 | uint16_t color) { 625 | startWrite(); 626 | writeFillRect(x,y,w,h,color); 627 | endWrite(); 628 | } 629 | 630 | // Adapted from https://github.com/PaulStoffregen/ILI9341_t3 631 | // by Marc MERLIN. See examples/pictureEmbed to use this. 632 | // 5/6/2017: function name and arguments have changed for compatibility 633 | // with current GFX library and to avoid naming problems in prior 634 | // implementation. Formerly drawBitmap() with arguments in different order. 635 | void Adafruit_ILI9341::drawRGBBitmap(int16_t x, int16_t y, 636 | uint16_t *pcolors, int16_t w, int16_t h) { 637 | 638 | int16_t x2, y2; // Lower-right coord 639 | if(( x >= _width ) || // Off-edge right 640 | ( y >= _height) || // " top 641 | ((x2 = (x+w-1)) < 0 ) || // " left 642 | ((y2 = (y+h-1)) < 0) ) return; // " bottom 643 | 644 | int16_t bx1=0, by1=0, // Clipped top-left within bitmap 645 | saveW=w; // Save original bitmap width value 646 | if(x < 0) { // Clip left 647 | w += x; 648 | bx1 = -x; 649 | x = 0; 650 | } 651 | if(y < 0) { // Clip top 652 | h += y; 653 | by1 = -y; 654 | y = 0; 655 | } 656 | if(x2 >= _width ) w = _width - x; // Clip right 657 | if(y2 >= _height) h = _height - y; // Clip bottom 658 | 659 | pcolors += by1 * saveW + bx1; // Offset bitmap ptr to clipped top-left 660 | startWrite(); 661 | setAddrWindow(x, y, w, h); // Clipped area 662 | while(h--) { // For each (clipped) scanline... 663 | writePixels(pcolors, w); // Push one (clipped) row 664 | pcolors += saveW; // Advance pointer by one full (unclipped) line 665 | } 666 | endWrite(); 667 | } 668 | -------------------------------------------------------------------------------- /ip-switch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | IP Switch 4 | ---------------------- 5 | https://remoteqth.com/wiki/index.php?page=IP+Switch+with+ESP32-GATEWAY 6 | 7 | ___ _ ___ _____ _ _ 8 | | _ \___ _ __ ___| |_ ___ / _ \_ _| || | __ ___ _ __ 9 | | / -_) ' \/ _ \ _/ -_) (_) || | | __ |_/ _/ _ \ ' \ 10 | |_|_\___|_|_|_\___/\__\___|\__\_\|_| |_||_(_)__\___/_|_|_| 11 | 12 | 13 | This program is free software: you can redistribute it and/or modify 14 | it under the terms of the GNU General Public License as published by 15 | the Free Software Foundation, either version 3 of the License, or 16 | (at your option) any later version. 17 | 18 | This program is distributed in the hope that it will be useful, 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | GNU General Public License for more details. 22 | 23 | You should have received a copy of the GNU General Public License 24 | along with this program. If not, see . 25 | 26 | Send test packet 27 | echo -n -e '\x00ms:bro;' | nc -u -w1 192.168.1.23 88 | hexdump -C 28 | Remote USB access 29 | screen /dev/ttyUSB0 115200 30 | 31 | HARDWARE ESP32-GATEWAY/ESP32-POE 32 | 33 | Changelog: 34 | 2021-12 - update for new lib 35 | 2020-12 - fix ifdef pinout setting 36 | 2020-09 - fix enter number in CLI 37 | add WatchDogTimer 38 | 2020-08 - show used gpio in CLI 39 | 2020-03 - support XL switch on ESP32-POE https://www.olimex.com/Products/IoT/ESP32/ESP32-POE/ 40 | - support Icom CI-V Band Decoder 41 | - support TFT LCD by https://www.olimex.com/Products/Modules/LCD/MOD-LCD2-8RTP/ 42 | 2020-01 - telnet access key generate automaticaly 43 | - redesign CLI enter value 44 | 2019-10 - add #define PCBrev04 45 | 2019-09 - disable start snake, show key in serial terminal 46 | 2019-06 - telnet with loggin support 47 | 2019-05 - reboot and clear output watchdog 48 | 2019-04 - group button support (idea TNX SM0MDG) 49 | - add serial to IP interface (3V3 rx gpio16, tx gpio17) 50 | - set Switch incoming UDP port from CLI 51 | 2019-03 - multi control support (idea TNX SM0MDG) 52 | 2019-01 - add CLI 53 | - redesign UDP communications 54 | - NET-ID prefix/sufix support 55 | 2018-12 - add web status page 56 | - add OTA 57 | 2018-09 - add IP switch support 58 | - blink LED after DHCP connect and receive sync packet 59 | 2018-08 add Band decoder support 60 | 61 | ToDo 62 | - esp32 watchdog 63 | - telnet inactivity watchdog > close 64 | - custom name for outputs 65 | - https://github.com/espressif/arduino-esp32/blob/master/libraries/Update/examples/AWS_S3_OTA_Update/AWS_S3_OTA_Update.ino 66 | - https://randomnerdtutorials.com/esp32-esp8266-relay-web-server/ 67 | */ 68 | //------------------------------------------------------------------------------------------------------- 69 | 70 | #define PCBrev04 // Enable for ESP32-GATEWAY PCB revision 0.4 or later 71 | //#define XLswitch // Enable for XL switch hardware with ESP32-POE 72 | const char* REV = "20211216"; 73 | const char* otaPassword = "remoteqth"; 74 | 75 | //------------------------------------------------------------------------------------------------------- 76 | 77 | int XLswitchANT = 16; // number of antenna output 78 | int XLswitchTRX = 4; // number of trx output 79 | byte XLswitchOutputs[4][2]; // 4 trx, 2 byte = 16 bit 80 | 81 | // #define XLswitchCIV // Icom CIV 82 | #if defined(XLswitchCIV) 83 | #define REQUEST 500 // [ms] use TXD output for sending frequency request 84 | #define CIV_ADRESS 0x56 // CIV input HEX Icom adress (0x is prefix) 85 | #define CIV_ADR_OUT 0x56 // CIV output HEX Icom adress (0x is prefix) 86 | int fromAdress = 0xE0; // 0E 87 | byte rdI[11]; //read data icom 88 | String rdIS; //read data icom string 89 | long freqPrev1; 90 | byte incomingByte = 0; 91 | int state = 1; // state machine 92 | bool StateMachineEnd = false; 93 | int BAND = 0; 94 | int previousBAND = -1; 95 | long freq = 0; 96 | const long Freq2Band[14][2] = {/* 97 | Freq Hz from to Band number 98 | */ {1810000, 2000000}, // #1 [160m] 99 | {3500000, 3800000}, // #2 [80m] 100 | {7000000, 7200000}, // #3 [40m] 101 | {10100000, 10150000}, // #4 [30m] 102 | {14000000, 14350000}, // #5 [20m] 103 | {18068000, 18168000}, // #6 [17m] 104 | {21000000, 21450000}, // #7 [15m] 105 | {24890000, 24990000}, // #8 [12m] 106 | {28000000, 29700000}, // #9 [10m] 107 | {50000000, 52000000}, // #10 [6m] 108 | {70000000, 72000000}, // #11 [4m] 109 | {144000000, 146000000}, // #12 [2m] 110 | {430000000, 440000000}, // #13 [70cm] 111 | {1240000000, 1300000000}, // #14 [23cm] 112 | // {2300000000, 2450000000}, // #15 [13cm] 113 | // {3300000000, 3500000000}, // #16 [9cm] 114 | // // {5650000000, 5850000000}, // #16 [6cm] 115 | }; 116 | long RequestTimeout[2]={0, 117 | #if defined(REQUEST) 118 | REQUEST 119 | #else 120 | 0 121 | #endif 122 | }; 123 | #endif 124 | 125 | #define ETHERNET // Enable ESP32 ethernet (DHCP IPv4) 126 | #define ETH_ADDR 0 127 | #define ETH_TYPE ETH_PHY_LAN8720 128 | #define ETH_POWER 5 129 | #define ETH_MDC 23 130 | #define ETH_MDIO 18 131 | #define ETH_CLK ETH_CLOCK_GPIO17_OUT // settings for ESP32 GATEWAY rev f-g 132 | // #define ETH_CLK ETH_CLOCK_GPIO0_OUT // settings for ESP32 GATEWAY rev c and older 133 | // ETH.begin(ETH_ADDR, ETH_POWER, ETH_MDC, ETH_MDIO, ETH_TYPE, ETH_CLK); 134 | // #define WIFI // Enable ESP32 WIFI (DHCP IPv4) 135 | const char* ssid = ""; 136 | const char* password = ""; 137 | 138 | char key[100]; 139 | byte InputByte[21]; 140 | // #define Ser2net // Serial to ip proxy - DISABLE if board revision 0.3 or lower 141 | #define EnableOTA // Enable flashing ESP32 Over The Air 142 | bool HW_BCD_SW = 0; // enable hardware ID board bcd switch (disable if not installed) 143 | int NumberOfEncoderOutputs = 8; // 2-16 144 | long HW_BCD_SWTimer[2]{0,3000}; 145 | byte NET_ID = 0x00; // Unique ID number [0-F] hex format - over BCD switch 146 | bool EnableSerialDebug = 0; 147 | #define HTTP_SERVER_PORT 80 // Web server port 148 | int IncomingSwitchUdpPort; 149 | #define ShiftOut // Enable ShiftOut register 150 | #define UdpAnswer // Send UDP answer confirm packet 151 | int BroadcastPort; // destination broadcast packet port 152 | bool EnableGroupPrefix = 0; // enable multi controller control 153 | bool EnableGroupButton = 0; // group to one from 154 | unsigned int GroupButton[8]={1,2,3,4,5,6,7,8}; 155 | byte DetectedRemoteSw[16][4]; 156 | unsigned int DetectedRemoteSwPort[16]; 157 | 158 | const int SERIAL_BAUDRATE = 115200; // serial debug baudrate 159 | int SERIAL1_BAUDRATE; // serial1 to IP baudrate 160 | // #if defined(Ser2net) && !defined(XLswitch) 161 | int incomingByte = 0; // for incoming serial data 162 | // #endif 163 | 164 | #if !defined(Ser2net) && !defined(XLswitch) && !defined(PCBrev04) 165 | const int BCD[4] = {34, 33, 32, 10}; // BCD encoder PINs 166 | #endif 167 | 168 | int i = 0; 169 | #include 170 | #include 171 | #include "EEPROM.h" 172 | #define EEPROM_SIZE 141 /* 173 | 0 -listen source 174 | 1 -net ID 175 | 2 -encoder range 176 | 3 -HW_BCD_SW 177 | 4 -EnableGroupPrefix 178 | 5 -EnableGroupButton 179 | 6-13 -GroupButton 180 | 14-17 - SERIAL1_BAUDRATE 181 | 18-21 - SerialServerIPport 182 | 22-25 - IncomingSwitchUdpPort 183 | 26-29 - RebootWatchdog 184 | 30-33 - OutputWatchdog 185 | 34 - Bank0 storage 186 | 35 - Bank1 storage 187 | 36 - Bank2 storage 188 | 37-40 - Authorised telnet client IP 189 | 41-140 - Authorised telnet client key 190 | */ 191 | unsigned int RebootWatchdog; 192 | unsigned int OutputWatchdog; 193 | unsigned long WatchdogTimer=0; 194 | 195 | // 73 seconds WDT (WatchDogTimer) 196 | #include 197 | #define WDT_TIMEOUT 73 198 | long WdtTimer=0; 199 | 200 | WiFiServer server(HTTP_SERVER_PORT); 201 | bool DHCP_ENABLE = 1; 202 | // Client variables 203 | char linebuf[80]; 204 | int charcount=0; 205 | //Are we currently connected? 206 | boolean connected = false; 207 | //The udp library class 208 | WiFiUDP UdpCommand; 209 | uint8_t buffer[50] = ""; 210 | unsigned char packetBuffer[10]; 211 | int UDPpacketSize; 212 | byte TxUdpBuffer[8]; 213 | #include 214 | static bool eth_connected = false; 215 | IPAddress RemoteSwIP(0, 0, 0, 0); // remote UDP IP switch - set from UDP DetectRemote array 216 | int RemoteSwPort = 0; // remote UDP IP switch port 217 | String HTTP_req; 218 | #if defined(EnableOTA) 219 | #include 220 | #include 221 | #endif 222 | 223 | #if defined(Ser2net) && !defined(XLswitch) 224 | #define RX1 16 225 | #define TX1 17 226 | HardwareSerial Serial_one(1); 227 | #endif 228 | #if defined(XLswitchCIV) && defined(XLswitch) 229 | #define RX1 35 230 | #define TX1 33 231 | HardwareSerial Serial_one(1); 232 | #endif 233 | #if defined(PCBrev04) 234 | const int ShiftOutClockPin = 12; 235 | const int ShiftOutLatchPin = 32; 236 | const int ShiftOutDataPin = 33; 237 | byte ShiftOutByte[3]; 238 | #elif defined(XLswitch) 239 | const int ShiftOutDataPin = 33; 240 | const int ShiftOutLatchPin = 32; 241 | const int ShiftOutClockPin = 4; 242 | byte ShiftOutByte[5]; 243 | #else 244 | const int ShiftOutDataPin = 17; 245 | const int ShiftOutLatchPin = 16; 246 | const int ShiftOutClockPin = 5; 247 | byte ShiftOutByte[3]; 248 | #endif 249 | #if defined(XLswitch) 250 | const int StatusLedAPin = 5; 251 | const int StatusLedBPin = 13; 252 | bool StatusLedB = false; 253 | long StatusLedBTimer[2] = {0,500}; 254 | int LcdNeedRefresh = 100; 255 | 256 | long LcdFpsTestTimer[2] = {0,1}; 257 | int LcdFpsTestCounter[2]={0,0}; 258 | bool LcdFpsTestForward=true; 259 | // https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts 260 | 261 | /*************************************************** 262 | This is an example made by Adafruit and modifed by Olimex for MOD-LCD2.8RTP 263 | This demo was tested with Olimex MOD-LCD2.8RTP and ESP32-EVB and OLIMEXINO-2560. 264 | The boards were connected via UEXT connector and cable. 265 | 266 | Make sure to establish proper hardware connections with your board. 267 | The display requires SPI, the touschreen I2C. Refer to Board_Pinout.h. 268 | 269 | The original example is a GFX example for the Adafruit ILI9341 Breakout and Shield 270 | ----> http://www.adafruit.com/products/1651 271 | 272 | Check out the link above for Adafruit's tutorials and wiring diagrams 273 | These displays use SPI to communicate, 4 or 5 pins are required to 274 | interface (RST is optional) 275 | Adafruit invests time and resources providing the open source code, 276 | please support Adafruit and open-source hardware by purchasing 277 | products from Adafruit! 278 | 279 | Written by Limor Fried/Ladyada for Adafruit Industries. 280 | MIT license, all text above must be included in any redistribution 281 | ****************************************************/ 282 | 283 | // In order to work you have to install Adafruit GFX Library 284 | // To do so go to: 285 | // Main menu --> Sketch --> Inlcude Librariy --> Manage Libraries... 286 | // In the search box filter "Adafruit GFX Library" and install it 287 | // Tested with version 1.2.3 of the library 288 | 289 | // #include "Board_Pinout.h" 290 | #include "SPI.h" 291 | #include "Adafruit_GFX.h" 292 | #include "Adafruit_ILI9341.h" 293 | #include "Wire.h" 294 | #include "Adafruit_STMPE610.h" 295 | 296 | // This is calibration data for the raw touch data to the screen coordinates 297 | #define TS_MINX 290 298 | #define TS_MINY 285 299 | #define TS_MAXX 7520 300 | #define TS_MAXY 7510 301 | #define TS_I2C_ADDRESS 0x4d 302 | 303 | // This is pinouts for ESP32-EVB 304 | #define TFT_DC 15 305 | #define TFT_CS 5 306 | #define TFT_MOSI 2 307 | #define TFT_CLK 14 308 | 309 | Adafruit_STMPE610 ts = Adafruit_STMPE610(); 310 | 311 | // Size of the color selection boxes and the paintbrush size 312 | #define BOXSIZE 40 313 | 314 | Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); 315 | 316 | uint8_t tp[5]; 317 | 318 | /* 319 | #define ILI9341_BLACK 0x0000 ///< 0, 0, 0 320 | #define ILI9341_NAVY 0x000F ///< 0, 0, 123 321 | #define ILI9341_DARKGREEN 0x03E0 ///< 0, 125, 0 322 | #define ILI9341_DARKCYAN 0x03EF ///< 0, 125, 123 323 | #define ILI9341_MAROON 0x7800 ///< 123, 0, 0 324 | #define ILI9341_PURPLE 0x780F ///< 123, 0, 123 325 | #define ILI9341_OLIVE 0x7BE0 ///< 123, 125, 0 326 | #define ILI9341_LIGHTGREY 0xC618 ///< 198, 195, 198 327 | #define ILI9341_DARKGREY 0x7BEF ///< 123, 125, 123 328 | #define ILI9341_BLUE 0x001F ///< 0, 0, 255 329 | #define ILI9341_GREEN 0x07E0 ///< 0, 255, 0 330 | #define ILI9341_CYAN 0x07FF ///< 0, 255, 255 331 | #define ILI9341_RED 0xF800 ///< 255, 0, 0 332 | #define ILI9341_MAGENTA 0xF81F ///< 255, 0, 255 333 | #define ILI9341_YELLOW 0xFFE0 ///< 255, 255, 0 334 | #define ILI9341_WHITE 0xFFFF ///< 255, 255, 255 335 | #define ILI9341_ORANGE 0xFD20 ///< 255, 165, 0 336 | #define ILI9341_GREENYELLOW 0xAFE5 ///< 173, 255, 41 337 | #define ILI9341_PINK 0xFC18 ///< 255, 130, 198 338 | */ 339 | #endif 340 | 341 | #define MAX_SRV_CLIENTS 1 342 | int SerialServerIPport; 343 | // WiFiServer SerialServer(SerialServerIPport); 344 | WiFiServer SerialServer; 345 | WiFiClient SerialServerClients[MAX_SRV_CLIENTS]; 346 | 347 | int TelnetServerIPport = 23; 348 | WiFiServer TelnetServer; 349 | WiFiClient TelnetServerClients[MAX_SRV_CLIENTS]; 350 | IPAddress TelnetServerClientAuth; 351 | bool TelnetAuthorized = false; 352 | int TelnetAuthStep=0; 353 | int TelnetAuthStepFails=0; 354 | int TelnetLoginFails=0; 355 | long TelnetLoginFailsBanTimer[2]={0,600000}; 356 | int RandomNumber; 357 | 358 | #if !defined(XLswitch) 359 | long MeterRefreshTimer[2]={0,100}; 360 | const int MeterPin[4] = {34,35,36,39}; 361 | int MeterValue[4]; 362 | #endif 363 | 364 | int CompareInt; 365 | //------------------------------------------------------------------------------------------------------- 366 | 367 | void setup() { 368 | Serial.begin(SERIAL_BAUDRATE); 369 | while(!Serial) { 370 | ; // wait for serial port to connect. Needed for native USB port only 371 | } 372 | #if defined(ShiftOut) 373 | pinMode(ShiftOutLatchPin, OUTPUT); 374 | pinMode(ShiftOutClockPin, OUTPUT); 375 | pinMode(ShiftOutDataPin, OUTPUT); 376 | #endif 377 | 378 | #if !defined(Ser2net) && !defined(XLswitch) && !defined(PCBrev04) 379 | for (int i = 0; i < 4; i++) { 380 | pinMode(BCD[i], INPUT); 381 | } 382 | #endif 383 | 384 | #if defined(XLswitch) 385 | pinMode(StatusLedAPin, OUTPUT); 386 | digitalWrite(StatusLedAPin, LOW); 387 | pinMode(StatusLedBPin, OUTPUT); 388 | digitalWrite(StatusLedBPin, LOW); 389 | 390 | delay(1000); 391 | tft.begin(); 392 | Wire.begin(); 393 | pinMode(TFT_DC, OUTPUT); 394 | // read diagnostics (optional but can help debug problems) 395 | //uint8_t x = tft.readcommand8(ILI9341_RDMODE); 396 | delay(1000); 397 | ts.begin(TS_I2C_ADDRESS); 398 | tft.fillScreen(ILI9341_BLACK); 399 | #endif 400 | 401 | // Listen source 402 | if (!EEPROM.begin(EEPROM_SIZE)){ 403 | if(EnableSerialDebug==1){ 404 | Serial.println("failed to initialise EEPROM"); delay(1); 405 | } 406 | } 407 | // 0-listen source 408 | TxUdpBuffer[2] = EEPROM.read(0); 409 | if(TxUdpBuffer[2]=='o'||TxUdpBuffer[2]=='r'||TxUdpBuffer[2]=='m'||TxUdpBuffer[2]=='e'){ 410 | // OK 411 | }else{ 412 | TxUdpBuffer[2]='n'; 413 | } 414 | 415 | // 1-net ID 416 | #if !defined(Ser2net) && !defined(XLswitch) 417 | if(HW_BCD_SW==true){ 418 | bitClear(NET_ID, 0); 419 | bitClear(NET_ID, 1); 420 | bitClear(NET_ID, 2); 421 | bitClear(NET_ID, 3); 422 | NET_ID = NET_ID | GetBoardId(); 423 | TxUdpBuffer[0] = NET_ID; 424 | }else{ 425 | #endif 426 | NET_ID = EEPROM.read(1); 427 | TxUdpBuffer[0] = NET_ID; 428 | #if !defined(Ser2net) && !defined(XLswitch) 429 | } 430 | #endif 431 | 432 | // 2-encoder range 433 | NumberOfEncoderOutputs = EEPROM.read(2); 434 | if(NumberOfEncoderOutputs < 2 || NumberOfEncoderOutputs > 0x0f){ 435 | NumberOfEncoderOutputs=8; 436 | } 437 | 438 | #if defined(Ser2net) && !defined(XLswitch) 439 | HW_BCD_SW = 0; 440 | #else 441 | // 3-HW_BCD_SW 442 | Serial.print("HW BCD on/off "); 443 | if(EEPROM.read(3)<2){ 444 | HW_BCD_SW = EEPROM.read(3); 445 | Serial.println("read from EEPROM"); 446 | }else{ 447 | Serial.println("set to OFF"); 448 | } 449 | #endif 450 | 451 | // 4-EnableGroupPrefix 452 | // Serial.print("Enable group NET-ID prefix "); 453 | if(EEPROM.read(4)<2){ 454 | EnableGroupPrefix=EEPROM.read(4); 455 | } 456 | // Serial.println(EnableGroupPrefix); 457 | 458 | // 5-EnableGroupButton 459 | if(EEPROM.read(5)<2){ 460 | EnableGroupButton=EEPROM.read(5); 461 | } 462 | 463 | // 6-13 ButtonGroup 464 | if(EnableGroupButton==true){ 465 | for (int i = 0; i < 8; i++) { 466 | if(EEPROM.read(6+i)<9){ 467 | GroupButton[i]=EEPROM.read(6+i); 468 | } 469 | } 470 | } 471 | 472 | SERIAL1_BAUDRATE=EEPROM.readInt(14); 473 | SerialServerIPport=EEPROM.readInt(18); 474 | IncomingSwitchUdpPort=EEPROM.readInt(22); 475 | BroadcastPort=IncomingSwitchUdpPort; 476 | RebootWatchdog=EEPROM.readUInt(26); 477 | if(RebootWatchdog>10080){ 478 | RebootWatchdog=0; 479 | } 480 | OutputWatchdog=EEPROM.readUInt(30); 481 | if(OutputWatchdog>10080){ 482 | OutputWatchdog=0; 483 | } 484 | if(RebootWatchdog>0){ 485 | ShiftOutByte[0]=EEPROM.readByte(34); 486 | ShiftOutByte[1]=EEPROM.readByte(35); 487 | ShiftOutByte[2]=EEPROM.readByte(36); 488 | } 489 | TelnetServerClientAuth[0]=EEPROM.readByte(37); 490 | TelnetServerClientAuth[1]=EEPROM.readByte(38); 491 | TelnetServerClientAuth[2]=EEPROM.readByte(39); 492 | TelnetServerClientAuth[3]=EEPROM.readByte(40); 493 | 494 | // 41-140 key 495 | // if clear, generate 496 | if(EEPROM.readByte(41)==255 && EEPROM.readByte(140)==255){ 497 | Serial.println(); 498 | Serial.println(" ** GENERATE KEY **"); 499 | for(int i=41; i<141; i++){ 500 | EEPROM.writeChar(i, RandomChar()); 501 | Serial.print("*"); 502 | } 503 | Serial.println(); 504 | // to defaults 505 | IncomingSwitchUdpPort=88; 506 | EEPROM.writeInt(22, IncomingSwitchUdpPort); 507 | BroadcastPort=IncomingSwitchUdpPort; 508 | EEPROM.commit(); 509 | } 510 | // read 511 | for(int i=41; i<141; i++){ 512 | key[i-41] = EEPROM.readChar(i); 513 | } 514 | 515 | // if(EnableSerialDebug==1){ 516 | Serial.println(); 517 | Serial.print("Version: "); 518 | Serial.println(REV); 519 | Serial.println("==============================="); 520 | Serial.print("SLAVE DEVICE NET-ID: 0x"); 521 | if(NET_ID <=0x0f){ 522 | Serial.print(F("0")); 523 | } 524 | Serial.println(NET_ID, HEX); 525 | Serial.print("Listen MASTER: "); 526 | if(TxUdpBuffer[2] == 'o'){ 527 | Serial.println("Open Interface III"); 528 | } 529 | if(TxUdpBuffer[2] == 'r' ){ 530 | Serial.println("Band decoder MK2"); 531 | } 532 | if(TxUdpBuffer[2] == 'm' ){ 533 | Serial.println("IP switch master"); 534 | } 535 | #if !defined(XLswitch) 536 | if(TxUdpBuffer[2] == 'e' ){ 537 | Serial.println("Meter"); 538 | for (int i = 0; i < 4; i++) { 539 | pinMode(MeterPin[i], INPUT); 540 | } 541 | } 542 | #endif 543 | if(TxUdpBuffer[2] == 'n' ){ 544 | Serial.println("none"); 545 | } 546 | Serial.println("==============================="); 547 | Serial.println(" press '?' for list commands"); 548 | Serial.println(); 549 | Serial.println(); 550 | // } 551 | 552 | #if defined(WIFI) 553 | if(EnableSerialDebug==1){ 554 | Serial.print("WIFI Connecting to "); 555 | Serial.print(ssid); 556 | } 557 | WiFi.begin(ssid, password); 558 | // attempt to connect to Wifi network: 559 | while(WiFi.status() != WL_CONNECTED) { 560 | // Connect to WPA/WPA2 network. Change this line if using open or WEP network: 561 | delay(500); 562 | if(EnableSerialDebug==1){ 563 | Serial.print("."); 564 | } 565 | } 566 | // LED1status = !LED1status; 567 | // digitalWrite(LED1, LED1status); // signalize wifi connected 568 | if(EnableSerialDebug==1){ 569 | Serial.println(""); 570 | Serial.println("WIFI connected"); 571 | Serial.print("WIFI IP address: "); 572 | Serial.println(WiFi.localIP()); 573 | Serial.print("WIFI dBm: "); 574 | Serial.println(WiFi.RSSI()); 575 | } 576 | #if !defined(Ser2net) && !defined(XLswitch) 577 | pinMode(BCD[1], OUTPUT); // LED 578 | digitalWrite(BCD[1], HIGH); 579 | delay(100); 580 | digitalWrite(BCD[1], LOW); 581 | delay(100); 582 | digitalWrite(BCD[1], HIGH); 583 | delay(100); 584 | digitalWrite(BCD[1], LOW); 585 | delay(100); 586 | pinMode(BCD[1], INPUT); 587 | #endif 588 | #endif 589 | 590 | #if defined(ETHERNET) 591 | WiFi.onEvent(EthEvent); 592 | // ETH.begin(); 593 | ETH.begin(ETH_ADDR, ETH_POWER, ETH_MDC, ETH_MDIO, ETH_TYPE, ETH_CLK); 594 | if(DHCP_ENABLE==false){ 595 | ETH.config(IPAddress(192, 168, 1, 188), IPAddress(192, 168, 1, 255),IPAddress(255, 255, 255, 0),IPAddress(8, 8, 8, 8)); 596 | //config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000); 597 | } 598 | 599 | #endif 600 | server.begin(); 601 | UdpCommand.begin(IncomingSwitchUdpPort); // incoming udp port 602 | 603 | #if defined(EnableOTA) 604 | // Port defaults to 3232 605 | // ArduinoOTA.setPort(3232); 606 | // Hostname defaults to esp3232-[MAC] 607 | 608 | #if defined(XLswitch) 609 | String StringHostname = "XL-switchID-"+String(NET_ID, HEX); 610 | #else 611 | String StringHostname = "IP-relayID-"+String(NET_ID, HEX); 612 | #endif 613 | char copy[13]; 614 | StringHostname.toCharArray(copy, 13); 615 | 616 | ArduinoOTA.setHostname(copy); 617 | ArduinoOTA.setPassword(otaPassword); 618 | // $ echo password | md5sum 619 | // ArduinoOTA.setPasswordHash("5587ba7a03b12a409ee5830cea97e079"); 620 | ArduinoOTA 621 | .onStart([]() { 622 | String type; 623 | if (ArduinoOTA.getCommand() == U_FLASH) 624 | type = "sketch"; 625 | else // U_SPIFFS 626 | type = "filesystem"; 627 | 628 | // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() 629 | Serial.println("Start updating " + type); 630 | #if defined(XLswitch) 631 | tft.fillScreen(ILI9341_ORANGE); 632 | tft.setRotation(1); 633 | tft.setTextColor(ILI9341_WHITE); 634 | tft.setCursor(155,30); 635 | tft.setTextSize(6); 636 | tft.println("!"); 637 | tft.setCursor(70,100); 638 | tft.setTextSize(3); 639 | tft.println("OTA update"); 640 | tft.drawRect(57, 150, 206, 16, ILI9341_WHITE); 641 | tft.setCursor(100,180); 642 | tft.setTextSize(1); 643 | tft.println("RemoteQTH.com firmware"); 644 | #endif 645 | }) 646 | .onEnd([]() { 647 | Serial.println("\nEnd"); 648 | #if defined(XLswitch) 649 | tft.fillScreen(ILI9341_BLACK); 650 | #endif 651 | }) 652 | .onProgress([](unsigned int progress, unsigned int total) { 653 | Serial.printf("Progress: %u%%\r", (progress / (total / 100))); 654 | #if defined(XLswitch) 655 | tft.fillRect(60, 153, (progress / (total / 100))*2, 10, ILI9341_WHITE); 656 | #endif 657 | }) 658 | .onError([](ota_error_t error) { 659 | Serial.printf("Error[%u]: ", error); 660 | if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); 661 | else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); 662 | else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); 663 | else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); 664 | else if (error == OTA_END_ERROR) Serial.println("End Failed"); 665 | }); 666 | 667 | ArduinoOTA.begin(); 668 | #endif 669 | 670 | #if defined(Ser2net) && !defined(XLswitch) 671 | Serial_one.begin(SERIAL1_BAUDRATE, SERIAL_8N1, RX1, TX1); 672 | // Serial2.begin(9600); 673 | SerialServer.begin(SerialServerIPport); 674 | SerialServer.setNoDelay(true); 675 | #endif 676 | 677 | #if defined(XLswitchCIV) && defined(XLswitch) 678 | Serial_one.begin(SERIAL1_BAUDRATE, SERIAL_8N1, RX1, TX1); 679 | #endif 680 | #if defined(XLswitch) 681 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 682 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, B10000000); 683 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, B00000000); 684 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, B00000000); 685 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, B00000000); 686 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, B00000000); 687 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 688 | #endif 689 | 690 | TelnetServer.begin(TelnetServerIPport); 691 | // TelnetlServer.setNoDelay(true); 692 | 693 | // WDT 694 | esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts 695 | esp_task_wdt_add(NULL); //add current thread to WDT watch 696 | WdtTimer=millis(); 697 | 698 | 699 | } 700 | 701 | //------------------------------------------------------------------------------------------------------- 702 | 703 | void loop() { 704 | // blank loop 80us 705 | // SerialToIp(); 706 | http(); 707 | RX_UDP(); 708 | // RX_UDP_XLswitch(); 709 | CLI(); 710 | Telnet(); 711 | // CheckNetId(); 712 | // Meter(); 713 | Watchdog(); 714 | // CIV(); 715 | // LcdDisplay(); 716 | // LcdFpsTest(); 717 | #if defined(EnableOTA) 718 | ArduinoOTA.handle(); 719 | #endif 720 | // Demo(); 721 | } 722 | // SUBROUTINES ------------------------------------------------------------------------------------------------------- 723 | void CIV(){ 724 | #if defined(XLswitchCIV) && defined(XLswitch) 725 | if(TxUdpBuffer[2]=='I'){ 726 | if (Serial_one.available()) { 727 | incomingByte = Serial_one.read(); 728 | // if(EnableSerialDebug==1){ 729 | // Prn(3, 0, String(incomingByte)); 730 | // // Serial.print(incomingByte); 731 | // // Serial.print("|"); 732 | // // Serial.println(incomingByte, HEX); 733 | // } 734 | icomSM(incomingByte); 735 | rdIS=""; 736 | // if(rdI[10]==0xFD){ // state machine end 737 | if(StateMachineEnd == true){ // state machine end 738 | StateMachineEnd = false; 739 | for (int i=9; i>=5; i-- ){ 740 | if (rdI[i] < 10) { // leading zero 741 | rdIS = rdIS + 0; 742 | } 743 | rdIS = rdIS + String(rdI[i], HEX); // append BCD digit from HEX variable to string 744 | } 745 | freq = rdIS.toInt(); 746 | if(EnableSerialDebug==1){ 747 | Prn(3, 0, String(freq)); 748 | Prn(3, 1, " Hz"); 749 | } 750 | // Serial.println(freq); 751 | // Serial.println("-------"); 752 | FreqToBandRules(); 753 | // bandSET(); 754 | LcdNeedRefresh=1; 755 | RequestTimeout[0]=millis(); 756 | } 757 | } 758 | 759 | #if defined(REQUEST) 760 | if(REQUEST > 0 && (millis() - RequestTimeout[0] > RequestTimeout[1])){ 761 | txCIV(3, 0, CIV_ADRESS); // ([command], [freq]) 3=read 762 | RequestTimeout[0]=millis(); 763 | } 764 | #endif 765 | } 766 | #endif 767 | } 768 | 769 | //--------------------------------------------------------------------------------------------------------- 770 | #if defined(XLswitchCIV) && defined(XLswitch) 771 | 772 | //--------------------------------------------------------------------------------------------------------- 773 | void txCIV(int commandCIV, long dataCIVtx, int toAddress) { 774 | //Serial.flush(); 775 | Serial_one.write(254); // FE 776 | Serial_one.write(254); // FE 777 | Serial_one.write(toAddress); // to adress 778 | Serial_one.write(fromAdress); // from OE 779 | Serial_one.write(commandCIV); // data 780 | if (dataCIVtx != 0){ 781 | String freqCIVtx = String(dataCIVtx); // to string 782 | String freqCIVtxPart; 783 | while (freqCIVtx.length() < 10) { // leding zeros 784 | freqCIVtx = 0 + freqCIVtx; 785 | } 786 | for (int x=8; x>=0; x=x-2){ // loop for 5x2 char [xx xx xx xx xx] 787 | freqCIVtxPart = freqCIVtx.substring(x,x+2); // cut freq to five part 788 | Serial_one.write(hexToDec(freqCIVtxPart)); // HEX to DEC, because write as DEC format from HEX variable 789 | } 790 | } 791 | Serial_one.write(253); // FD 792 | // Serial.flush(); 793 | while(Serial_one.available()){ // clear buffer 794 | Serial_one.read(); 795 | } 796 | if(EnableSerialDebug==1){ 797 | Prn(3, 1, "FEFE"+String(toAddress, HEX)+String(fromAdress, HEX)+String(commandCIV, HEX)+"FD"); 798 | } 799 | } 800 | 801 | //--------------------------------------------------------------------------------------------------------- 802 | unsigned int hexToDec(String hexString) { 803 | unsigned int decValue = 0; 804 | int nextInt; 805 | for (int i = 0; i < hexString.length(); i++) { 806 | nextInt = int(hexString.charAt(i)); 807 | if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9); 808 | if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15); 809 | if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15); 810 | nextInt = constrain(nextInt, 0, 15); 811 | decValue = (decValue * 16) + nextInt; 812 | } 813 | return decValue; 814 | } 815 | 816 | //--------------------------------------------------------------------------------------------------------- 817 | void icomSM(byte b){ // state machine 818 | // This filter solves read from 0x00 0x05 0x03 commands and 00 E0 F1 address used by software 819 | // Serial.print(b, HEX); 820 | // Serial.print(" | "); 821 | // Serial.println(state); 822 | switch (state) { 823 | case 1: if( b == 0xFE ){ state = 2; rdI[0]=b; rdI[10]=0x00; }; break; 824 | case 2: if( b == 0xFE ){ state = 3; rdI[1]=b; }else{ state = 1;}; break; 825 | // addresses that use different software 00-trx, e0-pc-ale, winlinkRMS, f1-winlink trimode 826 | case 3: if( b == 0x00 || b == 0xE0 || b == 0x0E || b == 0xF1 ){ state = 4; rdI[2]=b; // choose command $03 827 | }else if( b == CIV_ADRESS ){ state = 6; rdI[2]=b; 828 | }else if( b == 0xFE ){ state = 3; rdI[1]=b; // FE (3x reduce to 2x) 829 | }else{ state = 1;}; break; // or $05 830 | 831 | case 4: if( b == CIV_ADRESS ){ state = 5; rdI[3]=b; }else{ state = 1;}; break; // select command $03 832 | case 5: if( b == 0x00 || b == 0x03 ){state = 8; rdI[4]=b; // freq 833 | }else if( b == 0x04 ){state = 14; rdI[4]=b; // mode 834 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 835 | }else{ state = 1;}; break; 836 | 837 | case 6: if( b == 0x00 || b == 0xE0 || b == 0xF1 ){ state = 7; rdI[3]=b; }else{ state = 1;}; break; // select command $05 838 | case 7: if( b == 0x00 || b == 0x05 ){ state = 8; rdI[4]=b; }else{ state = 1;}; break; 839 | 840 | case 8: if( b <= 0x99 ){state = 9; rdI[5]=b; // 10Hz 1Hz 841 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 842 | }else{state = 1;}; break; 843 | case 9: if( b <= 0x99 ){state = 10; rdI[6]=b; // 1kHz 100Hz 844 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 845 | }else{state = 1;}; break; 846 | case 10: if( b <= 0x99 ){state = 11; rdI[7]=b; // 100kHz 10kHz 847 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 848 | }else{state = 1;}; break; 849 | case 11: if( b <= 0x52 ){state = 12; rdI[8]=b; // 10MHz 1Mhz 850 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 851 | }else{state = 1;}; break; 852 | case 12: if( b <= 0x01 || b == 0x04){state = 13; rdI[9]=b; // 1GHz 100MHz <-- 1xx/4xx MHz limit 853 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 854 | }else{state = 1;}; break; 855 | case 13: if( b == 0xFD ){state = 1; rdI[10]=b; StateMachineEnd = true; 856 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 857 | }else{state = 1; rdI[10] = 0x00;}; break; 858 | 859 | case 14: if( b <= 0x12 ){state = 15; rdI[5]=b; 860 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 861 | }else{state = 1;}; break; // Mode 862 | case 15: if( b <= 0x03 ){state = 16; rdI[6]=b; 863 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 864 | }else{state = 1;}; break; // Filter 865 | case 16: if( b == 0xFD ){state = 1; rdI[7]=b; 866 | }else if( b == 0xFE ){ state = 2; rdI[0]=b; // FE 867 | }else{state = 1; rdI[7] = 0;}; break; 868 | } 869 | } 870 | //------------------------------------------------------------------------------------------------------- 871 | void FreqToBandRules(){ 872 | if (freq >=Freq2Band[0][0] && freq <=Freq2Band[0][1] ) {BAND=1;} // 160m 873 | else if (freq >=Freq2Band[1][0] && freq <=Freq2Band[1][1] ) {BAND=2;} // 80m 874 | else if (freq >=Freq2Band[2][0] && freq <=Freq2Band[2][1] ) {BAND=3;} // 40m 875 | else if (freq >=Freq2Band[3][0] && freq <=Freq2Band[3][1] ) {BAND=4;} // 30m 876 | else if (freq >=Freq2Band[4][0] && freq <=Freq2Band[4][1] ) {BAND=5;} // 20m 877 | else if (freq >=Freq2Band[5][0] && freq <=Freq2Band[5][1] ) {BAND=6;} // 17m 878 | else if (freq >=Freq2Band[6][0] && freq <=Freq2Band[6][1] ) {BAND=7;} // 15m 879 | else if (freq >=Freq2Band[7][0] && freq <=Freq2Band[7][1] ) {BAND=8;} // 12m 880 | else if (freq >=Freq2Band[8][0] && freq <=Freq2Band[8][1] ) {BAND=9;} // 10m 881 | else if (freq >=Freq2Band[9][0] && freq <=Freq2Band[9][1] ) {BAND=10;} // 6m 882 | else if (freq >=Freq2Band[10][0] && freq <=Freq2Band[10][1] ) {BAND=11;} // 2m 883 | else if (freq >=Freq2Band[11][0] && freq <=Freq2Band[11][1] ) {BAND=12;} // 70cm 884 | else {BAND=0;} // out of range 885 | } 886 | 887 | //------------------------------------------------------------------------------------------------------- 888 | #endif 889 | 890 | //------------------------------------------------------------------------------------------------------- 891 | 892 | #if defined(XLswitch) 893 | void LcdFpsTest() { 894 | // tft.fillScreen(ILI9341_BLACK); 895 | tft.setRotation(1); 896 | 897 | // if(millis()-LcdFpsTestTimer[0]>LcdFpsTestTimer[1]){ 898 | // tft.setTextColor(ILI9341_WHITE); 899 | // tft.fillRect(80, 30, 200, 30, ILI9341_BLACK); 900 | // tft.setCursor(80,30); 901 | // tft.setTextSize(4); 902 | // tft.print("fps "); 903 | // tft.print(LcdFpsTestCounter[0]); 904 | // LcdFpsTestTimer[0]=millis(); 905 | // LcdFpsTestCounter[0]=0; 906 | // } 907 | // 908 | // // tft.drawRect(57, 150, 206, 16, ILI9341_WHITE); 909 | // tft.fillRect(60+LcdFpsTestCounter[1], 153, 1, 10, ILI9341_WHITE); 910 | // 911 | // tft.fillRect(60+(200-LcdFpsTestCounter[1]), 183, 2, 10, ILI9341_WHITE); 912 | // tft.fillRect(60+(200-LcdFpsTestCounter[1])+2, 183, 2, 10, ILI9341_BLACK); 913 | // LcdFpsTestCounter[0]++; 914 | // LcdFpsTestCounter[1]++; 915 | // if(LcdFpsTestCounter[1]>200){ 916 | // tft.fillRect(57, 150, 206, 16, ILI9341_BLACK); 917 | // tft.fillRect(57, 180, 206, 16, ILI9341_BLACK); 918 | // LcdFpsTestCounter[1]=0; 919 | // } 920 | 921 | 922 | 923 | if(LcdFpsTestCounter[0]>2 || LcdFpsTestCounter[0]<1){ 924 | LcdFpsTestForward=!LcdFpsTestForward; 925 | } 926 | if(LcdFpsTestForward==true){ 927 | LcdFpsTestCounter[0]--; 928 | }else{ 929 | LcdFpsTestCounter[0]++; 930 | } 931 | tft.setTextColor(ILI9341_WHITE); 932 | tft.fillRect(80, 30, 200, 30, ILI9341_BLACK); 933 | tft.setCursor(80,30); 934 | tft.setTextSize(4); 935 | tft.print(LcdFpsTestCounter[0]); 936 | tft.print(" ms"); 937 | 938 | 939 | tft.fillRect(57, 150, 206, 16, ILI9341_BLACK); 940 | for (int i = 0; i < 200; i++) { 941 | tft.fillRect(60+i, 153, 1, 10, ILI9341_WHITE); 942 | tft.fillRect(60+200-i, 183, 1, 10, ILI9341_WHITE); 943 | delay(LcdFpsTestCounter[0]); 944 | } 945 | for (int i = 200; i > 0; i--) { 946 | tft.fillRect(60+i, 153, 1, 10, ILI9341_BLACK); 947 | tft.fillRect(60+200-i, 183, 1, 10, ILI9341_BLACK); 948 | delay(LcdFpsTestCounter[0]); 949 | } 950 | 951 | } 952 | #endif 953 | //------------------------------------------------------------------------------------------------------- 954 | 955 | void LcdDisplay() { // 320x240 px 956 | #if defined(XLswitch) 957 | if(LcdNeedRefresh>0){ 958 | int LineSpace = (226-18)/XLswitchTRX; 959 | int RowSpace = 320/XLswitchANT; 960 | 961 | 962 | // default 963 | if(LcdNeedRefresh==100){ 964 | // Clear Screen 965 | tft.fillScreen(ILI9341_BLACK); 966 | tft.setRotation(1); 967 | 968 | tft.fillRect(0, 0, 320, 18, ILI9341_DARKGREY); 969 | tft.setTextColor(ILI9341_ORANGE); 970 | tft.setCursor(0,0); 971 | tft.setTextSize(2); 972 | tft.print("XLswitch"); 973 | if(TxUdpBuffer[2]=='I'){ 974 | tft.setCursor(260,0); 975 | tft.setTextColor(ILI9341_LIGHTGREY); 976 | tft.setTextSize(2); 977 | tft.print("Icom"); 978 | }else{ 979 | tft.setCursor(250,0); 980 | tft.setTextColor(ILI9341_LIGHTGREY); 981 | tft.setTextSize(2); 982 | tft.print("ID-"); 983 | tft.print(String(IdPrefix(NET_ID), HEX) ); 984 | tft.print(String(IdSufix(NET_ID), HEX) ); 985 | } 986 | // tft.drawLine(0,18,340,18, ILI9341_LIGHTGREY); 987 | 988 | // lines + numbers 989 | for (int i = 0; i < XLswitchTRX+1; i++) { 990 | tft.drawLine(0,18+i*LineSpace,340,18+i*LineSpace, ILI9341_LIGHTGREY); 991 | if(iMeterRefreshTimer[1]){ 1185 | MeterRefreshTimer[0]=millis(); 1186 | for (int i = 0; i < 4; i++) { 1187 | MeterValue[i] = analogRead(MeterPin[i]); 1188 | if(EnableSerialDebug==1){ 1189 | Serial.print(MeterValue[i]); 1190 | Serial.print(" "); 1191 | } 1192 | } 1193 | if(EnableSerialDebug==1){ 1194 | Serial.println(); 1195 | Serial.println(MeterValue[0], BIN); 1196 | } 1197 | // int tmp; 1198 | // byte buffer[6]; 1199 | // tmp=MeterValue[0]; 1200 | // for (int i = 0; i < 4; i++) { 1201 | // 1202 | // } 1203 | // buffer[0]= 1204 | // 1205 | // TxUDP('E', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 1); 1206 | 1207 | } 1208 | #endif 1209 | } 1210 | //--------------------------------------------------------------------------------------------------------- 1211 | // void SplitValue(int A, int B){ 1212 | // int tmp; 1213 | // tmp=MeterValue[0]; 1214 | // for (int i = 0; i < 4; i++) { 1215 | // 1216 | // } 1217 | // 1218 | // 1219 | // packetBuffer[0]; 1220 | // 1221 | // } 1222 | 1223 | byte HiByte(int ID){ 1224 | bitClear(ID, 0); // -> 1225 | bitClear(ID, 1); 1226 | bitClear(ID, 2); 1227 | bitClear(ID, 3); 1228 | ID = ID >> 4; 1229 | return(ID); 1230 | } 1231 | 1232 | //--------------------------------------------------------------------------------------------------------- 1233 | 1234 | byte LowByte(int ID){ 1235 | bitClear(ID, 4); 1236 | bitClear(ID, 5); 1237 | bitClear(ID, 6); 1238 | bitClear(ID, 7); // <- 1239 | return(ID); 1240 | } 1241 | 1242 | //------------------------------------------------------------------------------------------------------- 1243 | void Watchdog(){ 1244 | 1245 | // WDT 1246 | if(millis()-WdtTimer > 60000){ 1247 | esp_task_wdt_reset(); 1248 | WdtTimer=millis(); 1249 | if(EnableSerialDebug==true){ 1250 | Prn(3, 0,"WDT reset "); 1251 | Prn(3, 1, String(millis()/1000) ); 1252 | } 1253 | } 1254 | 1255 | if(RebootWatchdog > 0 && millis()-WatchdogTimer > RebootWatchdog*60000){ 1256 | Serial.println(); 1257 | Serial.println("** Activate reboot watchdog - IP switch will be restarted **"); 1258 | EEPROM.writeByte(34, ShiftOutByte[0]); 1259 | EEPROM.writeByte(35, ShiftOutByte[1]); 1260 | EEPROM.writeByte(36, ShiftOutByte[2]); 1261 | EEPROM.commit(); 1262 | delay(1000); 1263 | TelnetServerClients[0].stop(); 1264 | ESP.restart(); 1265 | } 1266 | 1267 | if(OutputWatchdog > 0 && millis()-WatchdogTimer > OutputWatchdog*60000 && OutputWatchdog < 123456){ 1268 | Serial.println(); 1269 | Serial.println("** Activate clear output watchdog **"); 1270 | ShiftOutByte[0]=0x00; 1271 | ShiftOutByte[1]=0x00; 1272 | ShiftOutByte[2]=0x00; 1273 | EEPROM.writeByte(34, ShiftOutByte[0]); 1274 | EEPROM.writeByte(35, ShiftOutByte[1]); 1275 | EEPROM.writeByte(36, ShiftOutByte[2]); 1276 | EEPROM.commit(); 1277 | #if defined(ShiftOut) 1278 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 1279 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, 0x01); 1280 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 1281 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 1282 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 1283 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 1284 | #endif 1285 | // deactivate 1286 | OutputWatchdog=123456; 1287 | } 1288 | 1289 | #if defined(XLswitch) 1290 | if(StatusLedB==true && millis()-StatusLedBTimer[0]>StatusLedBTimer[1]){ 1291 | digitalWrite(StatusLedBPin, LOW); 1292 | StatusLedB = false; 1293 | } 1294 | #endif 1295 | 1296 | } 1297 | 1298 | //------------------------------------------------------------------------------------------------------- 1299 | void CheckNetId(){ 1300 | #if !defined(Ser2net) && !defined(XLswitch) 1301 | if (HW_BCD_SW==true){ 1302 | if(millis()-HW_BCD_SWTimer[0]>HW_BCD_SWTimer[1]){ 1303 | bitClear(NET_ID, 0); 1304 | bitClear(NET_ID, 1); 1305 | bitClear(NET_ID, 2); 1306 | bitClear(NET_ID, 3); 1307 | NET_ID = NET_ID | GetBoardId(); 1308 | if(NET_ID!=TxUdpBuffer[0]){ 1309 | TxUdpBuffer[0] = NET_ID; 1310 | EEPROM.write(1, NET_ID); // address, value 1311 | EEPROM.commit(); 1312 | Serial.print("** Now NET-ID change to 0x"); 1313 | if(NET_ID <=0x0f){ 1314 | Serial.print(F("0")); 1315 | } 1316 | Serial.print(NET_ID, HEX); 1317 | Serial.println(" **"); 1318 | if(EnableSerialDebug==1){ 1319 | Serial.print("EEPROM read ["); 1320 | Serial.print(EEPROM.read(1)); 1321 | Serial.println("]"); 1322 | } 1323 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1324 | if(TxUdpBuffer[2] == 'm'){ 1325 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1326 | } 1327 | } 1328 | HW_BCD_SWTimer[0]=millis(); 1329 | } 1330 | } 1331 | #endif 1332 | } 1333 | 1334 | //------------------------------------------------------------------------------------------------------- 1335 | void CLI(){ 1336 | int OUT=2; 1337 | int intBuf=0; 1338 | int mult=1; 1339 | // incomingByte = 0; 1340 | 1341 | if (Serial.available() > 0) { 1342 | incomingByte = Serial.read(); 1343 | OUT = 0; 1344 | } 1345 | 1346 | if(TelnetServerClients[0].connected() && OUT!=0){ 1347 | TelnetAuthorized = false; 1348 | if(TelnetServerClients[0].remoteIP()==TelnetServerClientAuth){ 1349 | TelnetAuthorized = true; 1350 | // timeout ... > false 1351 | }else{ 1352 | TelnetAuth(); 1353 | } 1354 | if(TelnetAuthorized==true){ 1355 | // incomingByte = TelnetRX(); 1356 | if(incomingByte!=0){ 1357 | OUT=1; 1358 | } 1359 | } 1360 | }else if(!TelnetServerClients[0].connected()){ 1361 | TelnetAuthStep=0; 1362 | } 1363 | 1364 | if(OUT<2){ 1365 | // ? 1366 | if(incomingByte==63){ 1367 | ListCommands(OUT); 1368 | 1369 | 1370 | // // e 1371 | // }else if(incomingByte==101){ 1372 | // Prn(OUT, 1," Change source of control? (y/n)"); 1373 | // EnterChar(OUT); 1374 | // if(incomingByte==89 || incomingByte==121){ 1375 | // TxUdpBuffer[2] = 'e'; 1376 | // EEPROM.write(0, 'e'); // address, value 1377 | // EEPROM.commit(); 1378 | // Prn(OUT, 1,"Now control from: Meter"); 1379 | // if(EnableSerialDebug==1){ 1380 | // Prn(OUT, 0,"EEPROM read ["); 1381 | // Prn(OUT, 0, String(EEPROM.read(0)) ); 1382 | // Prn(OUT, 1,"]"); 1383 | // } 1384 | // TxUDP('e', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1385 | // if(TxUdpBuffer[2] == 'm'){ 1386 | // TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1387 | // } 1388 | // } 1389 | 1390 | // m 1391 | }else if(incomingByte==109){ 1392 | Prn(OUT, 1," Change source of control? (y/n)"); 1393 | EnterChar(OUT); 1394 | if(incomingByte==89 || incomingByte==121){ 1395 | TxUdpBuffer[2] = 'm'; 1396 | EEPROM.write(0, 'm'); // address, value 1397 | EEPROM.commit(); 1398 | Prn(OUT, 1,"Now control from: IP switch master"); 1399 | if(EnableSerialDebug==1){ 1400 | Prn(OUT, 0,"EEPROM read ["); 1401 | Prn(OUT, 0, String(EEPROM.read(0)) ); 1402 | Prn(OUT, 1,"]"); 1403 | } 1404 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1405 | if(TxUdpBuffer[2] == 'm'){ 1406 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1407 | } 1408 | #if defined(XLswitch) 1409 | LcdNeedRefresh=100; 1410 | #endif 1411 | } 1412 | 1413 | 1414 | #if defined(XLswitchCIV) && defined(XLswitch) 1415 | // I 1416 | }else if(incomingByte==73){ 1417 | Prn(OUT, 1," Change source of control? (y/n)"); 1418 | EnterChar(OUT); 1419 | if(incomingByte==89 || incomingByte==121){ 1420 | TxUdpBuffer[2] = 'I'; 1421 | EEPROM.write(0, 'I'); // address, value 1422 | EEPROM.commit(); 1423 | Prn(OUT, 1,"Now control from: Icom CI-V"); 1424 | if(EnableSerialDebug==1){ 1425 | Prn(OUT, 0,"EEPROM read ["); 1426 | Prn(OUT, 0, String(EEPROM.read(0)) ); 1427 | Prn(OUT, 1,"]"); 1428 | } 1429 | LcdNeedRefresh=100; 1430 | // TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1431 | // if(TxUdpBuffer[2] == 'm'){ 1432 | // TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1433 | // } 1434 | } 1435 | #endif 1436 | // // r 1437 | // }else if(incomingByte==114){ 1438 | // Prn(OUT, 1," Change source of control? (y/n)"); 1439 | // EnterChar(OUT); 1440 | // if(incomingByte==89 || incomingByte==121){ 1441 | // EnableGroupPrefix=false; 1442 | // EEPROM.write(4, EnableGroupPrefix); 1443 | // EEPROM.commit(); 1444 | // EnableGroupButton=false; 1445 | // EEPROM.write(5, EnableGroupButton); 1446 | // EEPROM.commit(); 1447 | // TxUdpBuffer[2] = 'r'; 1448 | // EEPROM.write(0, 'r'); // address, value 1449 | // EEPROM.commit(); 1450 | // Prn(OUT, 1,"Now control from: Band decoder"); 1451 | // if(EnableSerialDebug==1){ 1452 | // Prn(OUT, 0,"EEPROM read ["); 1453 | // Prn(OUT, 0, String(EEPROM.read(0)) ); 1454 | // Prn(OUT, 1,"]"); 1455 | // } 1456 | // TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1457 | // if(TxUdpBuffer[2] == 'm'){ 1458 | // TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1459 | // } 1460 | // } 1461 | // o 1462 | }else if(incomingByte==111){ 1463 | Prn(OUT, 1," Change source of control? (y/n)"); 1464 | EnterChar(OUT); 1465 | if(incomingByte==89 || incomingByte==121){ 1466 | EnableGroupPrefix=false; 1467 | EEPROM.write(4, EnableGroupPrefix); 1468 | EEPROM.commit(); 1469 | EnableGroupButton=false; 1470 | EEPROM.write(5, EnableGroupButton); 1471 | EEPROM.commit(); 1472 | TxUdpBuffer[2] = 'o'; 1473 | EEPROM.write(0, 'o'); // address, value 1474 | EEPROM.commit(); 1475 | Prn(OUT, 1,"Now control from: Open Interface III"); 1476 | if(EnableSerialDebug==1){ 1477 | Prn(OUT, 0,"EEPROM read ["); 1478 | Prn(OUT, 0, String(EEPROM.read(0)) ); 1479 | Prn(OUT, 1,"]"); 1480 | } 1481 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1482 | if(TxUdpBuffer[2] == 'm'){ 1483 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1484 | } 1485 | } 1486 | 1487 | // n 1488 | }else if(incomingByte==110){ 1489 | Prn(OUT, 1," Change source of control? (y/n)"); 1490 | EnterChar(OUT); 1491 | if(incomingByte==89 || incomingByte==121){ 1492 | EnableGroupPrefix=false; 1493 | EEPROM.write(4, EnableGroupPrefix); 1494 | EnableGroupButton=false; 1495 | EEPROM.write(5, EnableGroupButton); 1496 | TxUdpBuffer[2] = 'n'; 1497 | EEPROM.write(0, 'n'); // address, value 1498 | EEPROM.commit(); 1499 | Prn(OUT, 1,"Now control from: none"); 1500 | } 1501 | 1502 | // w 1503 | }else if(incomingByte==119 && TxUdpBuffer[2]!='n'){ 1504 | Prn(OUT, 0,"Input reboot watchdog in minutes (0-10080), 0-disable and press "); 1505 | if(TelnetAuthorized==true){ 1506 | Prn(OUT, 1,"[enter]"); 1507 | }else{ 1508 | Prn(OUT, 1,"[;]"); 1509 | } 1510 | Prn(OUT, 1,"recomended 1440 (1 day)"); 1511 | Enter(); 1512 | for (int i=InputByte[0]; i>0; i--){ 1513 | intBuf = intBuf + ((InputByte[i]-48)*mult); 1514 | mult = mult*10; 1515 | } 1516 | if(intBuf>=0 && intBuf<=10080){ 1517 | RebootWatchdog = intBuf; 1518 | EEPROM.writeUInt(26, RebootWatchdog); 1519 | EEPROM.commit(); 1520 | Prn(OUT, 0," Set "); 1521 | Prn(OUT, 0, String(EEPROM.readUInt(26)) ); 1522 | Prn(OUT, 1," minutes"); 1523 | }else{ 1524 | Prn(OUT, 0,"Out of range."); 1525 | } 1526 | 1527 | // W 1528 | }else if(incomingByte==87 && TxUdpBuffer[2]!='n'){ 1529 | Prn(OUT, 0,"Input clear output watchdog in minutes (0-10080), 0-disable and press "); 1530 | if(TelnetAuthorized==true){ 1531 | Prn(OUT, 1,"[enter]"); 1532 | }else{ 1533 | Prn(OUT, 1,"[;]"); 1534 | } 1535 | Prn(OUT, 1,"note: if you need clear output after reboot watchdog, set smaller than it"); 1536 | Enter(); 1537 | for (int i=InputByte[0]; i>0; i--){ 1538 | intBuf = intBuf + ((InputByte[i]-48)*mult); 1539 | mult = mult*10; 1540 | } 1541 | if(intBuf>=0 && intBuf<=10080){ 1542 | OutputWatchdog = intBuf; 1543 | EEPROM.writeUInt(30, OutputWatchdog); 1544 | EEPROM.commit(); 1545 | Prn(OUT, 0," Set "); 1546 | Prn(OUT, 0, String(EEPROM.readUInt(30)) ); 1547 | Prn(OUT, 1," minutes"); 1548 | }else{ 1549 | Prn(OUT, 0,"Out of range."); 1550 | } 1551 | 1552 | // < 1553 | }else if(incomingByte==60 && TxUdpBuffer[2]!='n'){ 1554 | Prn(OUT, 0,"write UDP port (1-65535) and press "); 1555 | if(TelnetAuthorized==true){ 1556 | Prn(OUT, 1,"[enter]"); 1557 | }else{ 1558 | Prn(OUT, 1,"[;]"); 1559 | } 1560 | Enter(); 1561 | for (int i=InputByte[0]; i>0; i--){ 1562 | intBuf = intBuf + ((InputByte[i]-48)*mult); 1563 | mult = mult*10; 1564 | } 1565 | if(intBuf>=1 && intBuf<=65535){ 1566 | IncomingSwitchUdpPort = intBuf; 1567 | EEPROM.writeInt(22, IncomingSwitchUdpPort); 1568 | EEPROM.commit(); 1569 | Prn(OUT, 0," Set "); 1570 | Prn(OUT, 1, String(IncomingSwitchUdpPort) ); 1571 | Prn(OUT, 0,"** device will be restarted **"); 1572 | delay(1000); 1573 | TelnetServerClients[0].stop(); 1574 | ESP.restart(); 1575 | }else{ 1576 | Prn(OUT, 0,"Out of range."); 1577 | } 1578 | 1579 | // / 1580 | }else if(incomingByte==47 && TxUdpBuffer[2] == 'm'){ 1581 | Prn(OUT, 0,"write encoder rannge number (2-16) and press "); 1582 | if(TelnetAuthorized==true){ 1583 | Prn(OUT, 1,"[enter]"); 1584 | }else{ 1585 | Prn(OUT, 1,"[;]"); 1586 | } 1587 | Enter(); 1588 | for (int i=InputByte[0]; i>0; i--){ 1589 | intBuf = intBuf + ((InputByte[i]-48)*mult); 1590 | mult = mult*10; 1591 | } 1592 | if(intBuf>=2 && intBuf<=16){ 1593 | NumberOfEncoderOutputs = intBuf; 1594 | /* EnterChar(OUT); 1595 | // 2-G 1596 | if( (incomingByte>=50 && incomingByte<=57) || (incomingByte>=97 && incomingByte<=103) ){ 1597 | if(incomingByte>=50 && incomingByte<=57){ 1598 | NumberOfEncoderOutputs = incomingByte-48; 1599 | }else if(incomingByte>=97 && incomingByte<=103){ 1600 | NumberOfEncoderOutputs = incomingByte-87; 1601 | }*/ 1602 | NumberOfEncoderOutputs--; 1603 | EEPROM.write(2, NumberOfEncoderOutputs); // address, value 1604 | EEPROM.commit(); 1605 | Prn(OUT, 0,"** Now Encoder range change to "); 1606 | Prn(OUT, 0, String(NumberOfEncoderOutputs+1) ); 1607 | Prn(OUT, 1," **"); 1608 | if(EnableSerialDebug==1){ 1609 | Prn(OUT, 0,"EEPROM read ["); 1610 | Prn(OUT, 0, String(EEPROM.read(2)) ); 1611 | Prn(OUT, 1,"]"); 1612 | } 1613 | TxUDP('s', packetBuffer[2], 'c', 'f', 'm', 1); // 0=broadcast, 1= direct to RX IP 1614 | if(TxUdpBuffer[2] == 'm'){ 1615 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1616 | } 1617 | }else{ 1618 | Prn(OUT, 1,"Out of range"); 1619 | } 1620 | 1621 | // % 1622 | }else if(incomingByte==37){ 1623 | EnableGroupButton=!EnableGroupButton; 1624 | Prn(OUT, 0,"** Group buttons (one from) ["); 1625 | EEPROM.write(5, EnableGroupButton); 1626 | EEPROM.commit(); 1627 | if(EnableGroupButton==true){ 1628 | Prn(OUT, 1,"ON] **"); 1629 | for (int i = 0; i < 8; i++) { 1630 | if(EEPROM.read(6+i)<9){ 1631 | GroupButton[i]=EEPROM.read(6+i); 1632 | } 1633 | } 1634 | }else{ 1635 | Prn(OUT, 1,"OFF] **"); 1636 | } 1637 | 1638 | // : 1639 | }else if(incomingByte==58 && EnableGroupButton==true){ 1640 | Prn(OUT, 1," List groups"); 1641 | for (int i = 0; i < 8; i++) { 1642 | Prn(OUT, 0," Button "); 1643 | Prn(OUT, 0, String(i+1) ); 1644 | Prn(OUT, 0," in group "); 1645 | Prn(OUT, 1, String(GroupButton[i]) ); 1646 | } 1647 | 1648 | // ! 1649 | }else if(incomingByte==33 && EnableGroupButton==true){ 1650 | Prn(OUT, 1,"Press button number 1-8..."); 1651 | EnterChar(OUT); 1652 | if( (incomingByte>=49 && incomingByte<=56)){ 1653 | unsigned int ButtonNumber=incomingByte-48; 1654 | Prn(OUT, 0,"Press Group number 1-8 for button "); 1655 | Prn(OUT, 1, String(ButtonNumber) ); 1656 | EnterChar(OUT); 1657 | if( (incomingByte>=49 && incomingByte<=56)){ 1658 | unsigned int ButtonGroup=incomingByte-48; 1659 | Prn(OUT, 0," store Button "); 1660 | Prn(OUT, 0, String(ButtonNumber) ); 1661 | Prn(OUT, 0," to group "); 1662 | Prn(OUT, 1, String(ButtonGroup) ); 1663 | GroupButton[ButtonNumber-1]=ButtonGroup; 1664 | for (int i = 0; i < 8; i++) { 1665 | EEPROM.write(6+i, GroupButton[i]); 1666 | } 1667 | EEPROM.commit(); 1668 | }else{ 1669 | Prn(OUT, 1," accepts 0-8, exit"); 1670 | } 1671 | }else{ 1672 | Prn(OUT, 1," accepts 0-8, exit"); 1673 | } 1674 | 1675 | // * 1676 | }else if(incomingByte==42){ 1677 | EnableSerialDebug=!EnableSerialDebug; 1678 | Prn(OUT, 0,"** Serial DEBUG "); 1679 | if(EnableSerialDebug==true){ 1680 | Prn(OUT, 1,"ENABLE **"); 1681 | }else{ 1682 | Prn(OUT, 1,"DISABLE **"); 1683 | } 1684 | 1685 | // + 1686 | #if !defined(Ser2net) && !defined(XLswitch) 1687 | }else if(incomingByte==43 && TxUdpBuffer[2]!='n'){ 1688 | HW_BCD_SW=!HW_BCD_SW; 1689 | Prn(OUT, 0,"** Net ID sufix by "); 1690 | EEPROM.write(3, HW_BCD_SW); 1691 | EEPROM.commit(); 1692 | if(HW_BCD_SW==true){ 1693 | Prn(OUT, 1,"EEPROM/[BCD switch] **"); 1694 | bitClear(NET_ID, 0); 1695 | bitClear(NET_ID, 1); 1696 | bitClear(NET_ID, 2); 1697 | bitClear(NET_ID, 3); 1698 | NET_ID = NET_ID | GetBoardId(); 1699 | TxUdpBuffer[0] = NET_ID; 1700 | }else{ 1701 | NET_ID = EEPROM.read(1); 1702 | TxUdpBuffer[0] = NET_ID; 1703 | Prn(OUT, 1,"[EEPROM]/BCD switch **"); 1704 | } 1705 | #endif 1706 | 1707 | // # 1708 | }else if(incomingByte==35 && TxUdpBuffer[2]!='n'){ 1709 | if(EnableGroupPrefix==false){ 1710 | Prn(OUT, 1,"Press NET-ID X_ prefix 0-f..."); 1711 | }else{ 1712 | Prn(OUT, 1,"Press NET-ID _X sufix 0-f..."); 1713 | } 1714 | EnterChar(OUT); 1715 | if( (incomingByte>=48 && incomingByte<=57) || (incomingByte>=97 && incomingByte<=102) ){ 1716 | // prefix 1717 | if(EnableGroupPrefix==false){ 1718 | bitClear(NET_ID, 4); 1719 | bitClear(NET_ID, 5); 1720 | bitClear(NET_ID, 6); 1721 | bitClear(NET_ID, 7); 1722 | Serial.write(incomingByte); 1723 | Prn(OUT, 1,""); 1724 | if(incomingByte>=48 && incomingByte<=57){ 1725 | incomingByte = incomingByte-48; 1726 | incomingByte = (byte)incomingByte << 4; 1727 | NET_ID = NET_ID | incomingByte; 1728 | TxUdpBuffer[0] = NET_ID; 1729 | }else if(incomingByte>=97 && incomingByte<=102){ 1730 | incomingByte = incomingByte-87; 1731 | incomingByte = (byte)incomingByte << 4; 1732 | NET_ID = NET_ID | incomingByte; 1733 | TxUdpBuffer[0] = NET_ID; 1734 | } 1735 | } 1736 | // sufix 1737 | if(HW_BCD_SW==false){ 1738 | if(EnableGroupPrefix==false){ 1739 | Prn(OUT, 1,"Press NET-ID _X sufix 0-f..."); 1740 | EnterChar(OUT); 1741 | } 1742 | if( (incomingByte>=48 && incomingByte<=57) || (incomingByte>=97 && incomingByte<=102) ){ 1743 | bitClear(NET_ID, 0); 1744 | bitClear(NET_ID, 1); 1745 | bitClear(NET_ID, 2); 1746 | bitClear(NET_ID, 3); 1747 | Serial.write(incomingByte); 1748 | Prn(OUT, 1, ""); 1749 | if(incomingByte>=48 && incomingByte<=57){ 1750 | incomingByte = incomingByte-48; 1751 | NET_ID = NET_ID | incomingByte; 1752 | TxUdpBuffer[0] = NET_ID; 1753 | }else if(incomingByte>=97 && incomingByte<=102){ 1754 | incomingByte = incomingByte-87; 1755 | NET_ID = NET_ID | incomingByte; 1756 | TxUdpBuffer[0] = NET_ID; 1757 | } 1758 | // #endif 1759 | EEPROM.write(1, NET_ID); // address, value 1760 | EEPROM.commit(); 1761 | Prn(OUT, 0,"** Now NET-ID change to 0x"); 1762 | if(NET_ID <=0x0f){ 1763 | Prn(OUT, 0, String("0")); 1764 | } 1765 | Prn(OUT, 0, String(NET_ID, HEX) ); 1766 | Prn(OUT, 1," **"); 1767 | if(EnableSerialDebug==1){ 1768 | Prn(OUT, 0,"EEPROM read ["); 1769 | Prn(OUT, 0, String(EEPROM.read(1), HEX) ); 1770 | Prn(OUT, 1,"]"); 1771 | } 1772 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1773 | if(TxUdpBuffer[2] == 'm'){ 1774 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1775 | } 1776 | // #if !defined(HW_BCD_SW) 1777 | }else{ 1778 | Prn(OUT, 1," accepts 0-f, exit"); 1779 | } 1780 | // #endif 1781 | } 1782 | }else{ 1783 | Prn(OUT, 1," accepts 0-f, exit"); 1784 | } 1785 | // E 1786 | }else if(incomingByte==69 && OUT==0){ 1787 | Prn(OUT, 1," Erase whole eeprom (also telnet key)? (y/n)"); 1788 | EnterChar(OUT); 1789 | if(incomingByte==89 || incomingByte==121){ 1790 | Prn(OUT, 1," Stop erase? (y/n)"); 1791 | EnterChar(OUT); 1792 | if(incomingByte==78 || incomingByte==110){ 1793 | for(int i=0; i0; i--){ 1859 | intBuf = intBuf + ((InputByte[i]-48)*mult); 1860 | mult = mult*10; 1861 | } 1862 | if(intBuf>=80 && intBuf<=5000000){ 1863 | SERIAL1_BAUDRATE = intBuf; 1864 | EEPROM.writeInt(14, SERIAL1_BAUDRATE); 1865 | EEPROM.commit(); 1866 | Prn(OUT, 0," Set "); 1867 | Prn(OUT, 1, String(SERIAL1_BAUDRATE) ); 1868 | Prn(OUT, 0,"** device will be restarted **"); 1869 | delay(1000); 1870 | TelnetServerClients[0].stop(); 1871 | ESP.restart(); 1872 | }else{ 1873 | Prn(OUT, 0,"Out of range."); 1874 | } 1875 | #endif 1876 | 1877 | // ) 1878 | #if defined(Ser2net) && !defined(XLswitch) 1879 | }else if(incomingByte==41){ 1880 | Prn(OUT, 0,"Write IP port (1-65535) and press "); 1881 | if(TelnetAuthorized==true){ 1882 | Prn(OUT, 1,"[enter]"); 1883 | }else{ 1884 | Prn(OUT, 1,"[;]"); 1885 | } 1886 | Enter(); 1887 | for (int i=InputByte[0]; i>0; i--){ 1888 | intBuf = intBuf + ((InputByte[i]-48)*mult); 1889 | mult = mult*10; 1890 | } 1891 | if(intBuf>=1 && intBuf<=65535){ 1892 | SerialServerIPport = intBuf; 1893 | EEPROM.writeInt(18, SerialServerIPport); 1894 | EEPROM.commit(); 1895 | Prn(OUT, 0," Set "); 1896 | Prn(OUT, 1, String(SerialServerIPport) ); 1897 | Prn(OUT, 0,"** device will be restarted **"); 1898 | delay(1000); 1899 | TelnetServerClients[0].stop(); 1900 | ESP.restart(); 1901 | }else{ 1902 | Prn(OUT, 0,"Out of range."); 1903 | } 1904 | #endif 1905 | 1906 | // & 1907 | }else if(incomingByte==38 && TxUdpBuffer[2]!='n'){ 1908 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 1909 | if(TxUdpBuffer[2] == 'm'){ 1910 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 1911 | } 1912 | 1913 | // q 1914 | }else if(incomingByte==113 && TelnetServerClients[0].connected() ){ 1915 | TelnetServerClients[0].stop(); 1916 | TelnetAuthorized=false; 1917 | // TelnetServerClientAuth = {0,0,0,0}; 1918 | TelnetAuthStep=0; 1919 | 1920 | // Q 1921 | }else if(incomingByte==81 && TelnetServerClients[0].connected() ){ 1922 | TelnetServerClients[0].stop(); 1923 | TelnetAuthorized=false; 1924 | TelnetServerClientAuth = {0,0,0,0}; 1925 | Serial.println(TelnetServerClientAuth); 1926 | TelnetAuthStep=0; 1927 | EEPROM.write(37, 0); // address, value 1928 | EEPROM.write(38, 0); // address, value 1929 | EEPROM.write(39, 0); // address, value 1930 | EEPROM.write(40, 0); // address, value 1931 | EEPROM.commit(); 1932 | 1933 | // @ 1934 | }else if(incomingByte==64){ 1935 | Prn(OUT, 1,"** IP switch will be restarted **"); 1936 | if(RebootWatchdog > 0){ 1937 | Prn(OUT, 1," Activate reboot watchdog - store outputs to EEPROM..."); 1938 | EEPROM.writeByte(34, ShiftOutByte[0]); 1939 | EEPROM.writeByte(35, ShiftOutByte[1]); 1940 | EEPROM.writeByte(36, ShiftOutByte[2]); 1941 | EEPROM.commit(); 1942 | delay(1000); 1943 | } 1944 | TelnetServerClients[0].stop(); 1945 | ESP.restart(); 1946 | 1947 | // LF 1948 | }else if(incomingByte==10){ 1949 | Prn(OUT, 1,""); 1950 | 1951 | // anykey 1952 | }else{ 1953 | Prn(OUT, 0," ["); 1954 | Prn(OUT, 0, String(incomingByte) ); //, DEC); 1955 | Prn(OUT, 1,"] unknown command"); 1956 | ListCommands(OUT); 1957 | } 1958 | incomingByte=0; 1959 | } 1960 | } 1961 | //------------------------------------------------------------------------------------------------------- 1962 | void Enter(){ 1963 | int OUT; 1964 | if(TelnetAuthorized==true){ 1965 | OUT=1; 1966 | }else{ 1967 | OUT=0; 1968 | } 1969 | 1970 | InputByte[0]=0; 1971 | incomingByte = 0; 1972 | bool br=false; 1973 | Prn(OUT, 0,"> "); 1974 | 1975 | if(OUT==0){ 1976 | while(br==false) { 1977 | if(Serial.available()){ 1978 | incomingByte=Serial.read(); 1979 | if(incomingByte==13 || incomingByte==59){ // CR or ; 1980 | br=true; 1981 | Serial.println(""); 1982 | }else{ 1983 | Serial.write(incomingByte); 1984 | InputByte[InputByte[0]+1]=incomingByte; 1985 | InputByte[0]++; 1986 | } 1987 | if(InputByte[0]==20){ 1988 | br=true; 1989 | Prn(OUT, 1," too long"); 1990 | } 1991 | } 1992 | } 1993 | 1994 | }else if(OUT==1){ 1995 | if (TelnetServerClients[0] && TelnetServerClients[0].connected()){ 1996 | 1997 | while(br==false){ 1998 | if(TelnetServerClients[0].available()){ 1999 | incomingByte=TelnetServerClients[0].read(); 2000 | if(incomingByte==13){ 2001 | br=true; 2002 | Prn(OUT, 1,""); 2003 | }else{ 2004 | TelnetServerClients[0].write(incomingByte); 2005 | InputByte[InputByte[0]+1]=incomingByte; 2006 | InputByte[0]++; 2007 | } 2008 | if(InputByte[0]==20){ 2009 | br=true; 2010 | Prn(OUT, 1," too long"); 2011 | } 2012 | } 2013 | } 2014 | } 2015 | } 2016 | 2017 | // Serial.println(); 2018 | // for (int i=1; i"); 2029 | if(OUT==0){ 2030 | while (Serial.available() == 0) { 2031 | // Wait 2032 | } 2033 | incomingByte = Serial.read(); 2034 | }else if(OUT==1){ 2035 | if (TelnetServerClients[0] && TelnetServerClients[0].connected()){ 2036 | while(incomingByte==0){ 2037 | if(TelnetServerClients[0].available()){ 2038 | incomingByte=TelnetServerClients[0].read(); 2039 | } 2040 | } 2041 | if(EnableSerialDebug==1){ 2042 | Serial.println(); 2043 | Serial.print("Telnet rx-"); 2044 | Serial.print(incomingByte, DEC); 2045 | } 2046 | } 2047 | } 2048 | Prn(OUT, 1, String(char(incomingByte)) ); 2049 | } 2050 | 2051 | //------------------------------------------------------------------------------------------------------- 2052 | 2053 | void EnterInt(int OUT){ 2054 | incomingByte = 0; 2055 | Prn(OUT, 0,"> "); 2056 | if(OUT==0){ 2057 | while(!Serial.available()) { 2058 | } 2059 | delay(3000); 2060 | CompareInt = Serial.parseInt(); 2061 | }else if(OUT==1){ 2062 | if (TelnetServerClients[0] && TelnetServerClients[0].connected()){ 2063 | bool br=true; 2064 | int intField[10]; 2065 | int count=0; 2066 | 2067 | while(incomingByte==0 && br==true){ 2068 | if(TelnetServerClients[0].available()){ 2069 | incomingByte=TelnetServerClients[0].read(); 2070 | // out of 0-9 2071 | if(incomingByte<48 || incomingByte>57){ 2072 | br=false; 2073 | intField[count]=0; 2074 | Prn(OUT, 1,""); 2075 | }else{ 2076 | intField[count]=incomingByte-48; 2077 | Prn(OUT, 0,String(intField[count])); 2078 | count++; 2079 | incomingByte=0; 2080 | } 2081 | } 2082 | } 2083 | 2084 | count--; 2085 | CompareInt=0; 2086 | int i=1; 2087 | while(count>-1){ 2088 | CompareInt=CompareInt+intField[count]*i; 2089 | // Prn(OUT, 1, String(intField[count])+"*"+String(i)+"="+String(CompareInt) ); 2090 | i=i*10; 2091 | count--; 2092 | } 2093 | } 2094 | } 2095 | // Prn(OUT, 1, "out"+String(CompareInt) ); 2096 | } 2097 | //------------------------------------------------------------------------------------------------------- 2098 | 2099 | void EnterIntOld(int OUT){ 2100 | Prn(OUT, 0,"> "); 2101 | if(OUT==0){ 2102 | while(!Serial.available()) { 2103 | } 2104 | delay(3000); 2105 | CompareInt = Serial.parseInt(); 2106 | }else if(OUT==1){ 2107 | if (TelnetServerClients[0] && TelnetServerClients[0].connected()){ 2108 | delay(5000); 2109 | if(TelnetServerClients[0].available()){ 2110 | // while(TelnetServerClients[0].available()){ 2111 | // Prn(OUT, 1, "4" ); 2112 | // // incomingByte=TelnetServerClients[0].read(); 2113 | // } 2114 | CompareInt = TelnetServerClients[0].parseInt(); 2115 | } 2116 | } 2117 | } 2118 | Prn(OUT, 1, String(CompareInt) ); 2119 | } 2120 | 2121 | //------------------------------------------------------------------------------------------------------- 2122 | void Prn(int OUT, int LN, String STR){ 2123 | if(OUT==3){ 2124 | if(TelnetAuthorized==true){ 2125 | OUT=1; 2126 | }else{ 2127 | OUT=0; 2128 | } 2129 | } 2130 | 2131 | if(OUT==0){ 2132 | Serial.print(STR); 2133 | if(LN==1){ 2134 | Serial.println(); 2135 | } 2136 | }else if(OUT==1){ 2137 | size_t len = STR.length()+1; 2138 | // uint8_t sbuf[len]; 2139 | char sbuf[len]; 2140 | STR.toCharArray(sbuf, len); 2141 | //push data to all connected telnet clients 2142 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 2143 | if (TelnetServerClients[i] && TelnetServerClients[i].connected()){ 2144 | TelnetServerClients[i].write(sbuf, len); 2145 | // delay(1); 2146 | if(LN==1){ 2147 | TelnetServerClients[i].write(13); // CR 2148 | TelnetServerClients[i].write(10); // LF 2149 | } 2150 | } 2151 | } 2152 | } 2153 | } 2154 | 2155 | //------------------------------------------------------------------------------------------------------- 2156 | void ListCommands(int OUT){ 2157 | 2158 | #if defined(ETHERNET) 2159 | Prn(OUT, 1,""); 2160 | #if defined(XLswitch) 2161 | Prn(OUT, 1," IP XLswitch on ESP32-POE status"); 2162 | #else 2163 | Prn(OUT, 1," IP relay ESP32-GATEWAY status"); 2164 | #endif 2165 | Prn(OUT, 0," http://"); 2166 | Prn(OUT, 1, String(ETH.localIP()[0])+"."+String(ETH.localIP()[1])+"."+String(ETH.localIP()[2])+"."+String(ETH.localIP()[3]) ); 2167 | Prn(OUT, 0," ETH MAC: "); 2168 | Prn(OUT, 0, String(ETH.macAddress()[0], HEX)+"."+String(ETH.macAddress()[1], HEX)+"."+String(ETH.macAddress()[2], HEX)+"."+String(ETH.macAddress()[3], HEX)+"."+String(ETH.macAddress()[4], HEX)+"."+String(ETH.macAddress()[5], HEX) ); 2169 | Prn(OUT, 0," "); 2170 | Prn(OUT, 0, String(ETH.linkSpeed()) ); 2171 | Prn(OUT, 0,"Mbps"); 2172 | if (ETH.fullDuplex()) { 2173 | Prn(OUT, 0,", FULL_DUPLEX "); 2174 | } 2175 | #else 2176 | Prn(OUT, 1," ETHERNET OFF"); 2177 | #endif 2178 | #if defined(WIFI) 2179 | Prn(OUT, 1," ================================="); 2180 | Prn(OUT, 0," http://"); 2181 | Prn(OUT, 1, String(WiFi.localIP()[0])+"."+String(WiFi.localIP()[1])+"."+String(WiFi.localIP()[2])+"."+String(WiFi.localIP()[3]) ); 2182 | Prn(OUT, 0," dBm: "); 2183 | Prn(OUT, 1, String(WiFi.RSSI()) ); 2184 | #else 2185 | Prn(OUT, 1,"| WIFI OFF"); 2186 | #endif 2187 | if(OUT==0){ 2188 | Prn(OUT, 0," OTA "); 2189 | #if defined(EnableOTA) 2190 | Prn(OUT, 0,"enable, password: "); 2191 | Prn(OUT, 1, otaPassword); 2192 | #else 2193 | Prn(OUT, 1,"disable"); 2194 | #endif 2195 | } 2196 | if(TxUdpBuffer[2]!='n'){ 2197 | // Serial.print(" Key: "); 2198 | // Serial.println(String(key)); 2199 | if(OUT==0){ 2200 | Prn(OUT, 1," Key for telnet access:"); 2201 | Prn(OUT, 0," "); 2202 | for(int i=0; i<100; i++){ 2203 | Prn(OUT, 0, String(key[i])); 2204 | if((i+1)%10==0){ 2205 | Prn(OUT, 0," "); 2206 | } 2207 | } 2208 | Prn(OUT, 1,""); 2209 | } 2210 | Prn(OUT, 1," ================================="); 2211 | Prn(OUT, 0," Device NET-ID: 0x"); 2212 | // if(NET_ID <=0x0f){ 2213 | // Prn(OUT, 0,"0"); 2214 | // } 2215 | // Prn(OUT, 0, String(NET_ID, HEX) ); 2216 | // if(EnableGroupPrefix==false){ 2217 | Prn(OUT, 0, String(IdPrefix(NET_ID), HEX) ); 2218 | // }else{ 2219 | // Prn(OUT, 0, "_"); 2220 | // } 2221 | Prn(OUT, 0, String(IdSufix(NET_ID), HEX) ); 2222 | #if !defined(Ser2net) && !defined(XLswitch) && !defined(PCBrev04) 2223 | if(HW_BCD_SW==true){ 2224 | Prn(OUT, 0," [BCD-"); 2225 | for (int i = 0; i < 4; i++) { 2226 | Prn(OUT, 0,String(digitalRead(BCD[i])) ); 2227 | } 2228 | Prn(OUT, 0,"]"); 2229 | } 2230 | #endif 2231 | Prn(OUT, 1,""); 2232 | if(EnableGroupPrefix==true){ 2233 | Prn(OUT, 1," NOTE: If activate Multi control"); 2234 | Prn(OUT, 1," - ID prefix identifies individual controllers"); 2235 | Prn(OUT, 1," - ID sufix same at all (relay and also control) devices - one group"); 2236 | } 2237 | Prn(OUT, 1," ================================="); 2238 | } 2239 | if(EnableGroupPrefix==false){ 2240 | Prn(OUT, 0," Master IP: "); 2241 | Prn(OUT, 0, String(UdpCommand.remoteIP()[0])+"."+String(UdpCommand.remoteIP()[1])+"."+String(UdpCommand.remoteIP()[2])+"."+String(UdpCommand.remoteIP()[3]) ); 2242 | Prn(OUT, 0,":"); 2243 | Prn(OUT, 1, String(UdpCommand.remotePort()) ); 2244 | } 2245 | Prn(OUT, 0," Uptime: "); 2246 | Prn(OUT, 0, String(millis()/1000) ); 2247 | Prn(OUT, 1," second"); 2248 | 2249 | if(RebootWatchdog > 0){ 2250 | Prn(OUT, 0,"> Reboot countdown in "); 2251 | Prn(OUT, 0, String(RebootWatchdog-((millis()-WatchdogTimer)/60000)) ); 2252 | Prn(OUT, 1, " minutes"); 2253 | } 2254 | if(OutputWatchdog > 0 && OutputWatchdog<123456){ 2255 | Prn(OUT, 0,"> Clear output countdown in "); 2256 | Prn(OUT, 0, String(OutputWatchdog-((millis()-WatchdogTimer)/60000)) ); 2257 | Prn(OUT, 1," minutes"); 2258 | } 2259 | 2260 | Prn(OUT, 0," Version: "); 2261 | Prn(OUT, 1, String(REV)); 2262 | if(TxUdpBuffer[2]!='n'){ 2263 | Prn(OUT, 0, " Bank status ABC [LSBFIRST]: "); 2264 | Prn(OUT, 0, String(ShiftOutByte[0], BIN) ); 2265 | Prn(OUT, 0, " "); 2266 | Prn(OUT, 0, String(ShiftOutByte[1], BIN) ); 2267 | Prn(OUT, 0," "); 2268 | Prn(OUT, 1, String(ShiftOutByte[2], BIN) ); 2269 | } 2270 | Prn(OUT, 0, " ShiftOut GPIO [data, latch, clock]: "); 2271 | Prn(OUT, 1, String(ShiftOutDataPin)+", "+String(ShiftOutLatchPin)+", "+String(ShiftOutClockPin)); 2272 | Prn(OUT, 1,"---------------------------------------------"); 2273 | Prn(OUT, 1," You can change source, with send character:"); 2274 | if(TxUdpBuffer[2]=='m'){ 2275 | Prn(OUT, 0," [m]"); 2276 | }else{ 2277 | Prn(OUT, 0," m "); 2278 | } 2279 | Prn(OUT, 1,"- IP switch master"); 2280 | #if defined(XLswitchCIV) && defined(XLswitch) 2281 | if(TxUdpBuffer[2]=='I'){ 2282 | Prn(OUT, 0," [I]"); 2283 | }else{ 2284 | Prn(OUT, 0," I "); 2285 | } 2286 | Prn(OUT, 1,"- Icom CI-V"); 2287 | #endif 2288 | // if(TxUdpBuffer[2]=='r'){ 2289 | // Prn(OUT, 0," [r]"); 2290 | // }else{ 2291 | // Prn(OUT, 0," r "); 2292 | // } 2293 | // Prn(OUT, 1,"- Band decoder"); 2294 | // if(TxUdpBuffer[2]=='e'){ 2295 | // Prn(OUT, 0," [e]"); 2296 | // }else{ 2297 | // Prn(OUT, 0," e "); 2298 | // } 2299 | // Prn(OUT, 1,"- Meter"); 2300 | if(TxUdpBuffer[2]=='o'){ 2301 | Prn(OUT, 0," [o]"); 2302 | }else{ 2303 | Prn(OUT, 0," o "); 2304 | } 2305 | Prn(OUT, 1,"- Open Interface III"); 2306 | if(TxUdpBuffer[2]=='n'){ 2307 | Prn(OUT, 0," [n]"); 2308 | }else{ 2309 | Prn(OUT, 0," n "); 2310 | } 2311 | Prn(OUT, 1,"- none"); 2312 | Prn(OUT, 1,""); 2313 | Prn(OUT, 1," or ? list status and commands"); 2314 | if(TxUdpBuffer[2]!='n'){ 2315 | Prn(OUT, 0," w inactivity reboot watchdog "); 2316 | if(RebootWatchdog>0){ 2317 | Prn(OUT, 0,"after ["); 2318 | Prn(OUT, 0, String(RebootWatchdog) ); 2319 | Prn(OUT, 1,"] minutes"); 2320 | }else{ 2321 | Prn(OUT, 1,"[disable]"); 2322 | } 2323 | Prn(OUT, 0," W inactivity clear output watchdog "); 2324 | if(OutputWatchdog>0){ 2325 | Prn(OUT, 0,"after ["); 2326 | Prn(OUT, 0, String(OutputWatchdog) ); 2327 | Prn(OUT, 1,"] minutes"); 2328 | }else{ 2329 | Prn(OUT, 1,"[disable]"); 2330 | } 2331 | Prn(OUT, 0," < change Switch incoming UDP port ["); 2332 | Prn(OUT, 0, String(IncomingSwitchUdpPort) ); 2333 | Prn(OUT, 0,"]"); 2334 | if(IncomingSwitchUdpPort!=88){ 2335 | Prn(OUT, 0,"<-- WARNING! default is 88"); 2336 | } 2337 | Prn(OUT, 1,""); 2338 | } 2339 | if(TxUdpBuffer[2] == 'm'){ 2340 | Prn(OUT, 0," / set encoder range - now ["); 2341 | Prn(OUT, 0, String(NumberOfEncoderOutputs+1) ); 2342 | if(NumberOfEncoderOutputs>7){ 2343 | Prn(OUT, 1,"] (two bank)"); 2344 | }else{ 2345 | Prn(OUT, 1,"]"); 2346 | } 2347 | Prn(OUT, 0," % group buttons (select one from group) ["); 2348 | if(EnableGroupButton==true){ 2349 | Prn(OUT, 1,"ON]"); 2350 | Prn(OUT, 1," ! SET group buttons"); 2351 | Prn(OUT, 1," : list group buttons"); 2352 | }else{ 2353 | Prn(OUT, 1,"OFF]"); 2354 | } 2355 | } 2356 | Prn(OUT, 0," * serial debug "); 2357 | if(EnableSerialDebug==true){ 2358 | Prn(OUT, 1,"[ON]"); 2359 | }else{ 2360 | Prn(OUT, 1,"[OFF]"); 2361 | } 2362 | #if !defined(Ser2net) && !defined(XLswitch) 2363 | if(TxUdpBuffer[2]!='n'){ 2364 | Prn(OUT, 0," + net ID sufix by "); 2365 | if(HW_BCD_SW==true){ 2366 | Prn(OUT, 1,"EEPROM/[BCD switch]"); 2367 | }else{ 2368 | Prn(OUT, 1,"[EEPROM]/BCD switch"); 2369 | } 2370 | } 2371 | #endif 2372 | if(TxUdpBuffer[2]!='n'){ 2373 | Prn(OUT, 0," # network ID prefix ["); 2374 | byte ID = NET_ID; 2375 | bitClear(ID, 0); // -> 2376 | bitClear(ID, 1); 2377 | bitClear(ID, 2); 2378 | bitClear(ID, 3); 2379 | ID = ID >> 4; 2380 | if(EnableGroupPrefix==true && TxUdpBuffer[2]=='m'){ 2381 | Prn(OUT, 0,"x"); 2382 | }else{ 2383 | Prn(OUT, 0, String(ID, HEX) ); 2384 | } 2385 | Prn(OUT, 0,"] hex"); 2386 | if(TxUdpBuffer[2]=='m' && EnableGroupPrefix==true){ 2387 | Prn(OUT, 0," (set only on controllers)"); 2388 | } 2389 | Prn(OUT, 1,""); 2390 | if(HW_BCD_SW==false){ 2391 | ID = NET_ID; 2392 | bitClear(ID, 4); 2393 | bitClear(ID, 5); 2394 | bitClear(ID, 6); 2395 | bitClear(ID, 7); // <- 2396 | Prn(OUT, 0," +network ID sufix ["); 2397 | Prn(OUT, 0, String(ID, HEX) ); 2398 | Prn(OUT, 0,"] hex"); 2399 | if(TxUdpBuffer[2]=='m' && EnableGroupPrefix==true){ 2400 | Prn(OUT, 0," (multi control group - same at all)"); 2401 | } 2402 | Prn(OUT, 1,""); 2403 | } 2404 | if(TxUdpBuffer[2]=='m'){ 2405 | Prn(OUT, 0," $ group network ID prefix (multi control) ["); 2406 | if(EnableGroupPrefix==true){ 2407 | Prn(OUT, 1,"ON]"); 2408 | }else{ 2409 | Prn(OUT, 1,"OFF]"); 2410 | } 2411 | } 2412 | if(TxUdpBuffer[2]=='m' && EnableGroupPrefix==true){ 2413 | Prn(OUT, 1," . list detected IP switch (multi control)"); 2414 | } 2415 | } 2416 | #if defined(Ser2net) || defined(XLswitchCIV) 2417 | Prn(OUT, 0," ( change serial1 baudrate ["); 2418 | Prn(OUT, 0, String(SERIAL1_BAUDRATE) ); 2419 | Prn(OUT, 1,"]"); 2420 | #endif 2421 | #if defined(Ser2net) && !defined(XLswitch) 2422 | Prn(OUT, 0," ) change ser2net IP port ["); 2423 | Prn(OUT, 0, String(SerialServerIPport) ); 2424 | Prn(OUT, 1,"]"); 2425 | #endif 2426 | if(TxUdpBuffer[2]!='n'){ 2427 | Prn(OUT, 1," & send broadcast packet"); 2428 | } 2429 | if(TelnetServerClients[0].connected()){ 2430 | Prn(OUT, 0," q disconnect and close telnet [logged from "); 2431 | Prn(OUT, 0, String(TelnetServerClientAuth[0])+"."+String(TelnetServerClientAuth[1])+"."+String(TelnetServerClientAuth[2])+"."+String(TelnetServerClientAuth[3]) ); 2432 | Prn(OUT, 1,"]"); 2433 | Prn(OUT, 1," Q logout with erase your IP from memory and close telnet"); 2434 | } 2435 | Prn(OUT, 1," E erase whole eeprom (telnet key also)"); 2436 | Prn(OUT, 1," @ restart device"); 2437 | Prn(OUT, 1,"---------------------------------------------"); 2438 | } 2439 | //------------------------------------------------------------------------------------------------------- 2440 | char RandomChar(){ 2441 | int R = random(48, 122); 2442 | if(R>=58 && 64>=R){ 2443 | R=R-random(7, 10); 2444 | } 2445 | if(R>=91 && 96>=R){ 2446 | R=R+random(6, 26); 2447 | } 2448 | return char(R); 2449 | } 2450 | 2451 | //------------------------------------------------------------------------------------------------------- 2452 | 2453 | void Demo(){ 2454 | #if defined(ShiftOut) 2455 | ShiftOutByte[0]=0; 2456 | ShiftOutByte[1]=0; 2457 | ShiftOutByte[2]=0; 2458 | 2459 | for (i = 0; i < 8; i++) { 2460 | bitSet(ShiftOutByte[0], i); 2461 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 2462 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 2463 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 2464 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 2465 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 2466 | delay(50); 2467 | bitClear(ShiftOutByte[2], i); 2468 | } 2469 | for (i = 0; i < 8; i++) { 2470 | bitSet(ShiftOutByte[1], i); 2471 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 2472 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 2473 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 2474 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 2475 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 2476 | delay(50); 2477 | bitClear(ShiftOutByte[0], i); 2478 | } 2479 | for (i = 0; i < 8; i++) { 2480 | bitSet(ShiftOutByte[2], i); 2481 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 2482 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 2483 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 2484 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 2485 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 2486 | delay(50); 2487 | bitClear(ShiftOutByte[1], i); 2488 | } 2489 | for (i = 0; i < 8; i++) { 2490 | // bitSet(ShiftOutByte[0], i); 2491 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 2492 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 2493 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 2494 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 2495 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 2496 | delay(50); 2497 | bitClear(ShiftOutByte[2], i); 2498 | } 2499 | #endif 2500 | } 2501 | 2502 | // http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/ 2503 | //------------------------------------------------------------------------------------------------------- 2504 | 2505 | byte GetBoardId(){ 2506 | byte NETID = 0; 2507 | #if !defined(Ser2net) && !defined(XLswitch) && !defined(PCBrev04) 2508 | if(digitalRead(BCD[0])==0){ 2509 | NETID = NETID | (1<<0); // Set the n-th bit 2510 | } 2511 | if(digitalRead(BCD[1])==0){ 2512 | NETID = NETID | (1<<1); // Set the n-th bit 2513 | } 2514 | if(digitalRead(BCD[2])==0){ 2515 | NETID = NETID | (1<<2); // Set the n-th bit 2516 | } 2517 | if(digitalRead(BCD[3])==0){ 2518 | NETID = NETID | (1<<3); // Set the n-th bit 2519 | } 2520 | #endif 2521 | return NETID; 2522 | } 2523 | //--------------------------------------------------------------------------------------------------------- 2524 | 2525 | byte IdPrefix(byte ID){ 2526 | bitClear(ID, 0); // -> 2527 | bitClear(ID, 1); 2528 | bitClear(ID, 2); 2529 | bitClear(ID, 3); 2530 | ID = ID >> 4; 2531 | return(ID); 2532 | } 2533 | 2534 | //--------------------------------------------------------------------------------------------------------- 2535 | 2536 | byte IdSufix(byte ID){ 2537 | bitClear(ID, 4); 2538 | bitClear(ID, 5); 2539 | bitClear(ID, 6); 2540 | bitClear(ID, 7); // <- 2541 | return(ID); 2542 | } 2543 | 2544 | //------------------------------------------------------------------------------------------------------- 2545 | byte AsciiToHex(int ASCII){ 2546 | if(ASCII>=48 && ASCII<=57){ 2547 | return(ASCII-48); 2548 | }else if(ASCII>=97 && ASCII<=102){ 2549 | return(ASCII-87); 2550 | }else{ 2551 | return(255); 2552 | } 2553 | } 2554 | 2555 | //------------------------------------------------------------------------------------------------------- 2556 | /* 2557 | ID FROM TO : BROADCAST ; 2558 | ID FROM TO : CONFIRM ; 2559 | ID FROM TO : A B C ; 2560 | ID FROM TO : A B C ; 2561 | 2562 | TX 0ms:b; 2563 | RX 0sm:c; 2564 | TX 0ms:123; 2565 | RX 0sm:123; 2566 | */ 2567 | 2568 | void RX_UDP(){ 2569 | #if !defined(XLswitch) 2570 | UDPpacketSize = UdpCommand.parsePacket(); // if there's data available, read a packet 2571 | if (UDPpacketSize){ 2572 | UdpCommand.read(packetBuffer, 10); // read the packet into packetBufffer 2573 | // Print RAW 2574 | if(EnableSerialDebug==1){ 2575 | Serial.println(); 2576 | Serial.print("RXraw ["); 2577 | Serial.print(packetBuffer[0], HEX); 2578 | for(int i=1; i<8; i++){ 2579 | Serial.print(char(packetBuffer[i])); 2580 | } 2581 | Serial.print(F("] ")); 2582 | Serial.print(UdpCommand.remoteIP()); 2583 | Serial.print(":"); 2584 | Serial.print(UdpCommand.remotePort()); 2585 | Serial.println(); 2586 | } 2587 | 2588 | // ID-FROM-TO filter 2589 | if( 2590 | (EnableGroupPrefix==false 2591 | && String(packetBuffer[0], DEC).toInt()==NET_ID 2592 | && packetBuffer[1]==TxUdpBuffer[2] // FROM Switch 2593 | && packetBuffer[2]== 's' // TO 2594 | && packetBuffer[3]== B00000000 2595 | // && packetBuffer[3]== ':' 2596 | && packetBuffer[7]== ';') 2597 | || 2598 | (EnableGroupPrefix==true 2599 | && IdSufix(packetBuffer[0])==IdSufix(NET_ID) 2600 | && packetBuffer[1]==TxUdpBuffer[2] // FROM Switch 2601 | && packetBuffer[2]== 's' // TO 2602 | && packetBuffer[3]== B00000000 2603 | // && packetBuffer[3]== ':' 2604 | && packetBuffer[7]== ';') 2605 | ){ 2606 | 2607 | if( EnableGroupPrefix==true && ( 2608 | DetectedRemoteSwPort [IdPrefix(packetBuffer[0])] == 0 2609 | || (packetBuffer[4]== 'b' && packetBuffer[5]== 'r' && packetBuffer[6]== 'o') 2610 | || (packetBuffer[4]== 'c' && packetBuffer[5]== 'f' && packetBuffer[6]== 'm') 2611 | ) 2612 | ){ 2613 | IPAddress TmpAddr = UdpCommand.remoteIP(); 2614 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [0]=TmpAddr[0]; // Switch IP addres storage to array 2615 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [1]=TmpAddr[1]; 2616 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [2]=TmpAddr[2]; 2617 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [3]=TmpAddr[3]; 2618 | DetectedRemoteSwPort [hexToDecBy4bit(IdPrefix(packetBuffer[0]))]=UdpCommand.remotePort(); 2619 | if(EnableSerialDebug==1){ 2620 | Serial.print("Detect controller ID "); 2621 | Serial.print(IdPrefix(packetBuffer[0]), HEX); 2622 | Serial.print(" on IP "); 2623 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [0]); 2624 | Serial.print("."); 2625 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [1]); 2626 | Serial.print("."); 2627 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [2]); 2628 | Serial.print("."); 2629 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [3]); 2630 | Serial.print(":"); 2631 | Serial.println(DetectedRemoteSwPort [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] ); 2632 | } 2633 | if(TxUdpBuffer[2] == 'm'){ 2634 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 1); 2635 | } 2636 | } 2637 | 2638 | // RX Broadcast / CFM 2639 | if((packetBuffer[4]== 'b' && packetBuffer[5]== 'r' && packetBuffer[6]== 'o') 2640 | || (packetBuffer[4]== 'c' && packetBuffer[5]== 'f' && packetBuffer[6]== 'm') 2641 | ){ 2642 | if(EnableSerialDebug==1){ 2643 | Serial.print("RX ["); 2644 | Serial.print(packetBuffer[0], HEX); 2645 | for(int i=1; i<8; i++){ 2646 | Serial.print(char(packetBuffer[i])); 2647 | } 2648 | Serial.print(F("] ")); 2649 | Serial.print(UdpCommand.remoteIP()); 2650 | Serial.print(":"); 2651 | Serial.println(UdpCommand.remotePort()); 2652 | } 2653 | #if !defined(Ser2net) && !defined(XLswitch) && !defined(PCBrev04) 2654 | pinMode(BCD[1], OUTPUT); 2655 | digitalWrite(BCD[1], HIGH); 2656 | delay(100); 2657 | digitalWrite(BCD[1], LOW); 2658 | delay(100); 2659 | pinMode(BCD[1], INPUT); 2660 | #endif 2661 | if(packetBuffer[4]== 'b' && packetBuffer[5]== 'r' && packetBuffer[6]== 'o'){ 2662 | TxUDP('s', packetBuffer[2], 'c', 'f', 'm', 1); // 0=broadcast, 1= direct to RX IP 2663 | if(TxUdpBuffer[2] == 'm'){ 2664 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 1); 2665 | } 2666 | } 2667 | 2668 | // RX DATA 2669 | }else{ 2670 | if(EnableGroupButton==true){ 2671 | CheckGroup(); 2672 | }else{ 2673 | ShiftOutByte[0] = String(packetBuffer[4], DEC).toInt(); // Bank0 2674 | } 2675 | ShiftOutByte[1] = String(packetBuffer[5], DEC).toInt(); // Bank1 2676 | ShiftOutByte[2] = String(packetBuffer[6], DEC).toInt(); // Bank2 2677 | 2678 | // SHIFT OUT 2679 | #if defined(ShiftOut) 2680 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 2681 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 2682 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 2683 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 2684 | 2685 | // shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 2686 | // shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, B00000000); 2687 | // shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 2688 | // shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 2689 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 2690 | if(EnableSerialDebug==1){ 2691 | Serial.println("ShiftOut"); 2692 | } 2693 | #endif 2694 | 2695 | if(EnableSerialDebug==1){ 2696 | // Serial.println(); 2697 | Serial.print(F("RX [")); 2698 | Serial.print(packetBuffer[0], HEX); 2699 | for(int i=1; i<4; i++){ 2700 | Serial.print(char(packetBuffer[i])); 2701 | } 2702 | Serial.print((byte)packetBuffer[4], BIN); 2703 | Serial.print(F("|")); 2704 | Serial.print((byte)packetBuffer[5], BIN); 2705 | Serial.print(F("|")); 2706 | Serial.print((byte)packetBuffer[6], BIN); 2707 | Serial.print(F(";] ")); 2708 | Serial.print(UdpCommand.remoteIP()); 2709 | Serial.print(F(":")); 2710 | Serial.println(UdpCommand.remotePort()); 2711 | } 2712 | if(UdpCommand.remotePort() != DetectedRemoteSwPort[hexToDecBy4bit(IdPrefix(packetBuffer[0]))] && EnableGroupPrefix==true){ 2713 | // if(EnableSerialDebug==1){ 2714 | Serial.print(F("** Change ip-port ID ")); 2715 | Serial.print(IdPrefix(packetBuffer[0]), HEX); 2716 | Serial.print(F(" (OLD-")); 2717 | Serial.print(DetectedRemoteSwPort[hexToDecBy4bit(IdPrefix(packetBuffer[0]))]); 2718 | Serial.print(F(" NEW-")); 2719 | Serial.print(UdpCommand.remotePort()); 2720 | Serial.println(F(") **")); 2721 | // } 2722 | DetectedRemoteSwPort[hexToDecBy4bit(IdPrefix(packetBuffer[0]))]=UdpCommand.remotePort(); 2723 | } 2724 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 2725 | } 2726 | WatchdogTimer=millis(); 2727 | // activate 2728 | if(OutputWatchdog==123456){ 2729 | OutputWatchdog=EEPROM.readUInt(27); 2730 | } 2731 | } // filtered end 2732 | else{ 2733 | if(EnableSerialDebug==1){ 2734 | Serial.println(F(" Different NET-ID, or bad packet format")); 2735 | } 2736 | } 2737 | memset(packetBuffer, 0, sizeof(packetBuffer)); // Clear contents of Buffer 2738 | 2739 | } //end IfUdpPacketSice 2740 | #endif 2741 | } 2742 | //------------------------------------------------------------------------------------------------------- 2743 | 2744 | void RX_UDP_XLswitch(){ 2745 | #if defined(XLswitch) 2746 | UDPpacketSize = UdpCommand.parsePacket(); // if there's data available, read a packet 2747 | if (UDPpacketSize){ 2748 | UdpCommand.read(packetBuffer, 10); // read the packet into packetBufffer 2749 | // Print RAW 2750 | if(EnableSerialDebug==1){ 2751 | Serial.println(); 2752 | Serial.print("RXraw ["); 2753 | Serial.print(packetBuffer[0], HEX); 2754 | for(int i=1; i<8; i++){ 2755 | Serial.print(char(packetBuffer[i])); 2756 | } 2757 | Serial.print(F("] ")); 2758 | Serial.print(UdpCommand.remoteIP()); 2759 | Serial.print(":"); 2760 | Serial.print(UdpCommand.remotePort()); 2761 | Serial.println(); 2762 | } 2763 | 2764 | // ID-FROM-TO filter 2765 | if( 2766 | (EnableGroupPrefix==false 2767 | && String(packetBuffer[0], DEC).toInt()==NET_ID 2768 | && packetBuffer[1]==TxUdpBuffer[2] // FROM Switch 2769 | && packetBuffer[2]== 's' // TO 2770 | && packetBuffer[3]== B00000000 2771 | // && packetBuffer[3]== ':' 2772 | && packetBuffer[7]== ';') 2773 | || 2774 | (EnableGroupPrefix==true 2775 | && IdSufix(packetBuffer[0])==IdSufix(NET_ID) 2776 | && packetBuffer[1]==TxUdpBuffer[2] // FROM Switch 2777 | && packetBuffer[2]== 's' // TO 2778 | && packetBuffer[3]== B00000000 2779 | // && packetBuffer[3]== ':' 2780 | && packetBuffer[7]== ';') 2781 | ){ 2782 | 2783 | if( EnableGroupPrefix==true && ( 2784 | DetectedRemoteSwPort [IdPrefix(packetBuffer[0])] == 0 2785 | || (packetBuffer[4]== 'b' && packetBuffer[5]== 'r' && packetBuffer[6]== 'o') 2786 | || (packetBuffer[4]== 'c' && packetBuffer[5]== 'f' && packetBuffer[6]== 'm') 2787 | ) 2788 | ){ 2789 | IPAddress TmpAddr = UdpCommand.remoteIP(); 2790 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [0]=TmpAddr[0]; // Switch IP addres storage to array 2791 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [1]=TmpAddr[1]; 2792 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [2]=TmpAddr[2]; 2793 | DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [3]=TmpAddr[3]; 2794 | DetectedRemoteSwPort [hexToDecBy4bit(IdPrefix(packetBuffer[0]))]=UdpCommand.remotePort(); 2795 | if(EnableSerialDebug==1){ 2796 | Serial.print("Detect controller ID "); 2797 | Serial.print(IdPrefix(packetBuffer[0]), HEX); 2798 | Serial.print(" on IP "); 2799 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [0]); 2800 | Serial.print("."); 2801 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [1]); 2802 | Serial.print("."); 2803 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [2]); 2804 | Serial.print("."); 2805 | Serial.print(DetectedRemoteSw [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] [3]); 2806 | Serial.print(":"); 2807 | Serial.println(DetectedRemoteSwPort [hexToDecBy4bit(IdPrefix(packetBuffer[0]))] ); 2808 | } 2809 | if(TxUdpBuffer[2] == 'm'){ 2810 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 1); 2811 | } 2812 | } 2813 | 2814 | // RX Broadcast / CFM 2815 | if((packetBuffer[4]== 'b' && packetBuffer[5]== 'r' && packetBuffer[6]== 'o') 2816 | || (packetBuffer[4]== 'c' && packetBuffer[5]== 'f' && packetBuffer[6]== 'm') 2817 | ){ 2818 | if(EnableSerialDebug==1){ 2819 | Serial.print("RX ["); 2820 | Serial.print(packetBuffer[0], HEX); 2821 | for(int i=1; i<8; i++){ 2822 | Serial.print(char(packetBuffer[i])); 2823 | } 2824 | Serial.print(F("] ")); 2825 | Serial.print(UdpCommand.remoteIP()); 2826 | Serial.print(":"); 2827 | Serial.println(UdpCommand.remotePort()); 2828 | } 2829 | #if !defined(Ser2net) && !defined(XLswitch) 2830 | pinMode(BCD[1], OUTPUT); 2831 | digitalWrite(BCD[1], HIGH); 2832 | delay(100); 2833 | digitalWrite(BCD[1], LOW); 2834 | delay(100); 2835 | pinMode(BCD[1], INPUT); 2836 | #endif 2837 | if(packetBuffer[4]== 'b' && packetBuffer[5]== 'r' && packetBuffer[6]== 'o'){ 2838 | TxUDP('s', packetBuffer[2], 'c', 'f', 'm', 1); // 0=broadcast, 1= direct to RX IP 2839 | if(TxUdpBuffer[2] == 'm'){ 2840 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 1); 2841 | } 2842 | } 2843 | 2844 | // RX DATA 2845 | }else{ 2846 | if(EnableGroupButton==true){ 2847 | CheckGroup(); 2848 | }else{ 2849 | ShiftOutByte[0] = String(packetBuffer[4], DEC).toInt(); // Bank0 2850 | } 2851 | // select between A/B 2852 | if(bitRead(ShiftOutByte[0], 0)==0){ 2853 | ShiftOutByte[1] = String(packetBuffer[5], DEC).toInt(); // Bank1 2854 | ShiftOutByte[2] = String(packetBuffer[6], DEC).toInt(); // Bank2 2855 | }else{ 2856 | ShiftOutByte[3] = String(packetBuffer[5], DEC).toInt(); // Bank1 2857 | ShiftOutByte[4] = String(packetBuffer[6], DEC).toInt(); // Bank2 2858 | } 2859 | 2860 | // SHIFT OUT 2861 | #if defined(ShiftOut) 2862 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 2863 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[4]); 2864 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[3]); 2865 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 2866 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 2867 | // shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); // buttons 2868 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 2869 | if(EnableSerialDebug==1){ 2870 | Serial.println("ShiftOut"); 2871 | } 2872 | #endif 2873 | 2874 | if(EnableSerialDebug==1){ 2875 | // Serial.println(); 2876 | Serial.print(F("RX [")); 2877 | Serial.print(packetBuffer[0], HEX); 2878 | for(int i=1; i<4; i++){ 2879 | Serial.print(char(packetBuffer[i])); 2880 | } 2881 | // Serial.print((byte)packetBuffer[4], BIN); 2882 | Serial.print((byte)ShiftOutByte[1], BIN); 2883 | Serial.print(F("|")); 2884 | // Serial.print((byte)packetBuffer[5], BIN); 2885 | Serial.print((byte)ShiftOutByte[2], BIN); 2886 | Serial.print(F("|")); 2887 | // Serial.print((byte)packetBuffer[6], BIN); 2888 | Serial.print((byte)ShiftOutByte[3], BIN); 2889 | Serial.print(F("|")); 2890 | Serial.print((byte)ShiftOutByte[4], BIN); 2891 | Serial.print(F(";] ")); 2892 | Serial.print(UdpCommand.remoteIP()); 2893 | Serial.print(F(":")); 2894 | Serial.println(UdpCommand.remotePort()); 2895 | } 2896 | if(UdpCommand.remotePort() != DetectedRemoteSwPort[hexToDecBy4bit(IdPrefix(packetBuffer[0]))] && EnableGroupPrefix==true){ 2897 | // if(EnableSerialDebug==1){ 2898 | Serial.print(F("** Change ip-port ID ")); 2899 | Serial.print(IdPrefix(packetBuffer[0]), HEX); 2900 | Serial.print(F(" (OLD-")); 2901 | Serial.print(DetectedRemoteSwPort[hexToDecBy4bit(IdPrefix(packetBuffer[0]))]); 2902 | Serial.print(F(" NEW-")); 2903 | Serial.print(UdpCommand.remotePort()); 2904 | Serial.println(F(") **")); 2905 | // } 2906 | DetectedRemoteSwPort[hexToDecBy4bit(IdPrefix(packetBuffer[0]))]=UdpCommand.remotePort(); 2907 | } 2908 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 2909 | #if defined(XLswitch) 2910 | if(TxUdpBuffer[2]=='m'){ 2911 | LcdNeedRefresh=2; 2912 | } 2913 | #endif 2914 | } 2915 | WatchdogTimer=millis(); 2916 | // activate 2917 | if(OutputWatchdog==123456){ 2918 | OutputWatchdog=EEPROM.readUInt(27); 2919 | } 2920 | } // filtered end 2921 | else{ 2922 | if(EnableSerialDebug==1){ 2923 | Serial.println(F(" Different NET-ID, or bad packet format")); 2924 | } 2925 | } 2926 | memset(packetBuffer, 0, sizeof(packetBuffer)); // Clear contents of Buffer 2927 | 2928 | #if defined(XLswitch) 2929 | digitalWrite(StatusLedBPin, HIGH); 2930 | StatusLedB = true; 2931 | StatusLedBTimer[0]=millis(); 2932 | #endif 2933 | 2934 | } //end IfUdpPacketSice 2935 | #endif 2936 | } 2937 | 2938 | //------------------------------------------------------------------------------------------------------- 2939 | 2940 | void CheckGroup(){ 2941 | int ChangeBit=9; 2942 | int NumberOfChange=0; 2943 | for (int i=0; i<8; i++){ 2944 | if(bitRead(packetBuffer[4], i)!=bitRead(ShiftOutByte[0], i)){ 2945 | ChangeBit=i; 2946 | NumberOfChange++; 2947 | } 2948 | } 2949 | // Serial.print("ChangeBit|NumberOfChange "); 2950 | // Serial.print(ChangeBit+1); 2951 | // Serial.print(" "); 2952 | // Serial.println(NumberOfChange); 2953 | 2954 | ShiftOutByte[0] = String(packetBuffer[4], DEC).toInt(); // Bank0 2955 | if(NumberOfChange==1){ 2956 | // Serial.println("clearGroup"); 2957 | NumberOfChange=0; 2958 | for (int i=0; i<8; i++){ 2959 | if(GroupButton[ChangeBit]==GroupButton[i] && ChangeBit!=i){ 2960 | bitClear(ShiftOutByte[0], i); 2961 | NumberOfChange++; 2962 | // Serial.print("Bitclear "); 2963 | // Serial.println(i); 2964 | } 2965 | } 2966 | if(NumberOfChange>0){ 2967 | bitSet(ShiftOutByte[0], ChangeBit); 2968 | } 2969 | } 2970 | } 2971 | //------------------------------------------------------------------------------------------------------- 2972 | 2973 | unsigned char hexToDecBy4bit(unsigned char hex) 2974 | // convert a character representation of a hexidecimal digit into the actual hexidecimal value 2975 | { 2976 | if(hex > 0x39) hex -= 7; // adjust for hex letters upper or lower case 2977 | return(hex & 0xf); 2978 | } 2979 | 2980 | //------------------------------------------------------------------------------------------------------- 2981 | 2982 | void TxUDP(byte FROM, byte TO, byte A, byte B, byte C, int DIRECT){ 2983 | 2984 | // TxUdpBuffer[0] = NET_ID; 2985 | TxUdpBuffer[1] = FROM; 2986 | // TxUdpBuffer[2] = TO; 2987 | 2988 | // if(TxUdpBuffer[2]=='m' && ( EnableGroupPrefix==true || EnableGroupButton==true ) ){ 2989 | // TxUdpBuffer[3] = B00101101; // - multi control || GroupButton 2990 | // }else{ 2991 | // TxUdpBuffer[3] = B00111010; // : 2992 | // } 2993 | 2994 | TxUdpBuffer[3] = B00000000; 2995 | // multi control 2996 | if(TxUdpBuffer[2]=='m' && EnableGroupPrefix==true){ 2997 | bitSet(TxUdpBuffer[3], 0); 2998 | } 2999 | // group button 3000 | if(TxUdpBuffer[2]=='m' && EnableGroupButton==true){ 3001 | bitSet(TxUdpBuffer[3], 1); 3002 | } 3003 | 3004 | TxUdpBuffer[4] = A; 3005 | TxUdpBuffer[5] = B; 3006 | TxUdpBuffer[6] = C; 3007 | TxUdpBuffer[7] = B00111011; // ; 3008 | 3009 | // BROADCAST 3010 | if(A=='b' && B=='r' && C=='o'){ // b r o 3011 | if(TxUdpBuffer[2] == 'm'){ 3012 | TxUdpBuffer[6] = NumberOfEncoderOutputs; 3013 | } 3014 | // direct 3015 | if(DIRECT==0){ 3016 | RemoteSwIP = ~ETH.subnetMask() | ETH.gatewayIP(); 3017 | if(EnableSerialDebug==1){ 3018 | Serial.print(F("TX broadcast [")); 3019 | } 3020 | }else{ 3021 | RemoteSwIP = UdpCommand.remoteIP(); 3022 | if(EnableSerialDebug==1){ 3023 | Serial.print(F("TX direct [")); 3024 | } 3025 | } 3026 | UdpCommand.beginPacket(RemoteSwIP, BroadcastPort); 3027 | 3028 | // CFM 3029 | }else if(A=='c' && B=='f' && C=='m'){ // cfm 3030 | if(TxUdpBuffer[2] == 'm'){ 3031 | TxUdpBuffer[6] = NumberOfEncoderOutputs; 3032 | } 3033 | if(EnableSerialDebug==1){ 3034 | Serial.print(F("TX direct [")); 3035 | } 3036 | UdpCommand.beginPacket(UdpCommand.remoteIP(), UdpCommand.remotePort()); 3037 | 3038 | // DATA 3039 | }else{ 3040 | RemoteSwIP = UdpCommand.remoteIP(); 3041 | if(EnableSerialDebug==1){ 3042 | Serial.print(F("TX [")); 3043 | } 3044 | UdpCommand.beginPacket(RemoteSwIP, UdpCommand.remotePort()); 3045 | } 3046 | 3047 | // send 3048 | if(EnableGroupPrefix==false){ 3049 | UdpCommand.write(TxUdpBuffer, sizeof(TxUdpBuffer)); // send buffer 3050 | UdpCommand.endPacket(); 3051 | if(EnableSerialDebug==1){ 3052 | Serial.print(TxUdpBuffer[0], HEX); 3053 | Serial.print(char(TxUdpBuffer[1])); 3054 | Serial.print(char(TxUdpBuffer[2])); 3055 | Serial.print(F("|")); 3056 | Serial.print(TxUdpBuffer[3], BIN); 3057 | Serial.print(F("|")); 3058 | Serial.print(TxUdpBuffer[4], BIN); 3059 | Serial.print(F("|")); 3060 | Serial.print(TxUdpBuffer[5], BIN); 3061 | Serial.print(F("|")); 3062 | Serial.print(TxUdpBuffer[6], BIN); 3063 | Serial.print(char(TxUdpBuffer[7])); 3064 | Serial.print(F("] ")); 3065 | Serial.print(RemoteSwIP); 3066 | Serial.print(F(":")); 3067 | Serial.print(UdpCommand.remotePort()); 3068 | #if defined(WIFI) 3069 | Serial.print(" | dBm: "); 3070 | Serial.print(WiFi.RSSI()); 3071 | #endif 3072 | Serial.println(); 3073 | } 3074 | 3075 | // send EnableGroupPrefix 3076 | }else{ 3077 | // answer to RX ip 3078 | TxUdpBuffer[0]=packetBuffer[0]; // NET_ID by RX NET_ID 3079 | UdpCommand.write(TxUdpBuffer, sizeof(TxUdpBuffer)); // send buffer 3080 | UdpCommand.endPacket(); 3081 | if(EnableSerialDebug==1){ 3082 | Serial.print(TxUdpBuffer[0], HEX); 3083 | for (int i=1; i<4; i++){ 3084 | Serial.print(char(TxUdpBuffer[i])); 3085 | } 3086 | Serial.print(TxUdpBuffer[4], BIN); 3087 | Serial.print(F("|")); 3088 | Serial.print(TxUdpBuffer[5], BIN); 3089 | Serial.print(F("|")); 3090 | Serial.print(TxUdpBuffer[6], BIN); 3091 | Serial.print(char(TxUdpBuffer[7])); 3092 | Serial.print(F("] ")); 3093 | Serial.print(RemoteSwIP); 3094 | Serial.print(F(":")); 3095 | Serial.print(UdpCommand.remotePort()); 3096 | #if defined(WIFI) 3097 | Serial.print(" | dBm: "); 3098 | Serial.print(WiFi.RSSI()); 3099 | #endif 3100 | Serial.println(); 3101 | } 3102 | // send to all ip from storage 3103 | IPAddress ControllerIP = UdpCommand.remoteIP(); 3104 | for (int i=0; i<16; i++){ 3105 | if(DetectedRemoteSwPort[i]!=0){ 3106 | TxUdpBuffer[0]=IdSufix(NET_ID) | i<<4; // NET_ID by destination device 3107 | RemoteSwIP = DetectedRemoteSw[i]; 3108 | RemoteSwPort = DetectedRemoteSwPort[i]; 3109 | if(ControllerIP!=RemoteSwIP){ 3110 | UdpCommand.beginPacket(RemoteSwIP, RemoteSwPort); 3111 | UdpCommand.write(TxUdpBuffer, sizeof(TxUdpBuffer)); // send buffer 3112 | UdpCommand.endPacket(); 3113 | 3114 | if(EnableSerialDebug==1){ 3115 | Serial.print(F("TX direct ID-")); 3116 | Serial.print(i, HEX); 3117 | Serial.print(IdSufix(NET_ID), HEX); 3118 | Serial.print(F(" ")); 3119 | Serial.print(RemoteSwIP); 3120 | Serial.print(F(":")); 3121 | Serial.print(RemoteSwPort); 3122 | Serial.print(F(" [")); 3123 | Serial.print(TxUdpBuffer[0], HEX); 3124 | for (int i=1; i<8; i++){ 3125 | Serial.print(char(TxUdpBuffer[i])); 3126 | // Serial.print(F(" ")); 3127 | } 3128 | Serial.print("]"); 3129 | #if defined(WIFI) 3130 | Serial.print(" WiFi dBm: "); 3131 | Serial.print(WiFi.RSSI()); 3132 | #endif 3133 | Serial.println(); 3134 | } 3135 | }else{ 3136 | if(EnableSerialDebug==1){ 3137 | Serial.print(F("noTX - RX prefix ")); 3138 | Serial.print(i, HEX); 3139 | Serial.print(F(" ")); 3140 | Serial.print(RemoteSwIP); 3141 | Serial.print(F(":")); 3142 | Serial.println(RemoteSwPort); 3143 | } 3144 | } 3145 | } 3146 | } 3147 | // broadcast all prefix 3148 | if(A=='b' && B=='r' && C=='o' && DIRECT==0 && TxUdpBuffer[2] == 'm'){ 3149 | if(EnableSerialDebug==1){ 3150 | Serial.print("TX all prefix "); 3151 | Serial.print(RemoteSwIP); 3152 | Serial.print(":"); 3153 | Serial.print(BroadcastPort); 3154 | Serial.print(F(" [")); 3155 | Serial.print("*"); 3156 | for (int i=1; i<8; i++){ 3157 | Serial.print(char(TxUdpBuffer[i])); 3158 | // Serial.print(F(" ")); 3159 | } 3160 | Serial.println("]"); 3161 | } 3162 | Serial.print("*) "); 3163 | for (int i=0; i<16; i++){ 3164 | TxUdpBuffer[0]=IdSufix(NET_ID) | (i<<4); 3165 | if(EnableSerialDebug==1){ 3166 | Serial.print(TxUdpBuffer[0], HEX); 3167 | Serial.print(" "); 3168 | } 3169 | UdpCommand.beginPacket(RemoteSwIP, BroadcastPort); 3170 | UdpCommand.write(TxUdpBuffer, sizeof(TxUdpBuffer)); // send buffer 3171 | UdpCommand.endPacket(); 3172 | } 3173 | if(EnableSerialDebug==1){ 3174 | Serial.println(); 3175 | } 3176 | } // end b r o 3177 | 3178 | } // end EnableGroupPrefix 3179 | } 3180 | //------------------------------------------------------------------------------------------------------- 3181 | 3182 | void http(){ 3183 | // listen for incoming clients 3184 | WiFiClient client = server.available(); 3185 | if (client) { 3186 | if(EnableSerialDebug==1){ 3187 | Serial.println("WIFI New client"); 3188 | } 3189 | #if defined(XLswitch) 3190 | digitalWrite(StatusLedBPin, HIGH); 3191 | StatusLedB = true; 3192 | StatusLedBTimer[0]=millis(); 3193 | #endif 3194 | memset(linebuf,0,sizeof(linebuf)); 3195 | charcount=0; 3196 | // an http request ends with a blank line 3197 | boolean currentLineIsBlank = true; 3198 | while (client.connected()) { 3199 | if (client.available()) { 3200 | char c = client.read(); 3201 | HTTP_req += c; 3202 | // if(EnableSerialDebug==1){ 3203 | // Serial.write(c); 3204 | // } 3205 | //read char by char HTTP request 3206 | linebuf[charcount]=c; 3207 | if (charcount")); 3219 | client.println(F("")); 3220 | client.println(F("")); 3221 | client.print(F("")); 3222 | client.println(F("IP switch NET-ID ")); 3223 | if(NET_ID <=0x0f){ 3224 | client.print("0"); 3225 | } 3226 | client.println(NET_ID, HEX); 3227 | client.println(F("")); 3228 | // client.print(F("")); 3237 | client.println(F("")); 3238 | client.println(F("")); 3239 | client.println(F("")); 3240 | client.println(F("")); 3254 | String GETOUTPUT = HTTP_req.substring(7, 8); 3255 | 3256 | // // SetPin(GETOUTPUT.toInt(), !OutputPinStatus[GETOUTPUT.toInt()]); 3257 | // ReversePin(GETOUTPUT.toInt()); 3258 | if(GETOUTPUT.toInt()==1){ 3259 | TxUdpBuffer[2]='o'; 3260 | EEPROM.write(0, 'o'); // address, value 3261 | EEPROM.commit(); 3262 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 3263 | if(TxUdpBuffer[2] == 'm'){ 3264 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 3265 | } 3266 | } 3267 | if(GETOUTPUT.toInt()==2){ 3268 | TxUdpBuffer[2]='r'; 3269 | EEPROM.write(0, 'r'); // address, value 3270 | EEPROM.commit(); 3271 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 3272 | if(TxUdpBuffer[2] == 'm'){ 3273 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 3274 | } 3275 | } 3276 | if(GETOUTPUT.toInt()==3){ 3277 | TxUdpBuffer[2]='m'; 3278 | EEPROM.write(0, 'm'); // address, value 3279 | EEPROM.commit(); 3280 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 3281 | if(TxUdpBuffer[2] == 'm'){ 3282 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 3283 | } 3284 | } 3285 | // if(GETOUTPUT.toInt()==4){ 3286 | // EnableGroupPrefix=!EnableGroupPrefix; 3287 | // Serial.print("** Group sufix (multi control) ["); 3288 | // EEPROM.write(4, EnableGroupPrefix); 3289 | // EEPROM.commit(); 3290 | // if(EnableGroupPrefix==true){ 3291 | // Serial.println("ON] **"); 3292 | // }else{ 3293 | // Serial.println("OFF] **"); 3294 | // } 3295 | // // if(EnableGroupPrefix==true){ 3296 | // // // clear prefix 3297 | // // bitClear(NET_ID, 4); 3298 | // // bitClear(NET_ID, 5); 3299 | // // bitClear(NET_ID, 6); 3300 | // // bitClear(NET_ID, 7); // <- 3301 | // // } 3302 | // // if(TxUdpBuffer[2] == 'm'){ 3303 | // // TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 3304 | // // } 3305 | // Serial.println("** IP switch will be restarted **"); 3306 | // delay(1000); 3307 | // TelnetServerClients[0].stop(); 3308 | // ESP.restart(); 3309 | // } 3310 | 3311 | if(TxUdpBuffer[2]!='n'){ 3312 | // client.println("

RemoteSwitch

"); 3313 | client.println("

Output status
"); 3314 | if( EnableGroupButton==true){ 3315 | client.print(" (Group buttons ENABLE)
"); 3316 | } 3317 | for (i = 0; i < 3; i++) { 3318 | client.print("Bank "); 3319 | client.print(i+1); 3320 | client.print(" "); 3321 | for (int j = 0; j < 8; j++) { 3322 | client.print("NumberOfEncoderOutputs)) || (i==2 && (j>NumberOfEncoderOutputs-8)) ){ 3324 | // nil 3325 | client.print("g\">x"); 3326 | }else{ 3327 | if(bitRead(ShiftOutByte[i], j)==1){ 3328 | client.print("r"); 3329 | } 3330 | client.print("\">"); 3331 | // client.print(bitRead(ShiftOutByte[i], j)); 3332 | if(i==2){ 3333 | client.print(j+1+8); 3334 | }else{ 3335 | client.print(j+1); 3336 | } 3337 | // Group button 3338 | if(i==0 && EnableGroupButton==true){ 3339 | client.print(" group"); 3340 | client.print(GroupButton[j]); 3341 | } 3342 | 3343 | client.println(""); 3344 | 3345 | // button break 3346 | if(i==0 && j==3){ 3347 | client.println("
"); 3348 | client.print("Bank "); 3349 | client.print(i+1); 3350 | client.print(" "); 3351 | } 3352 | } 3353 | } 3354 | client.println("
"); 3355 | } 3356 | client.println("

"); 3357 | } 3358 | client.print("Wifi: "); 3359 | #if defined(WIFI) 3360 | client.print(""); 3361 | client.print(ssid); 3362 | client.print(" "); 3363 | client.print(WiFi.RSSI()); 3364 | client.print(" dBm, "); 3365 | client.print(WiFi.localIP()); 3366 | #else 3367 | client.print("OFF"); 3368 | #endif 3369 | 3370 | client.print(" | ETH: "); 3371 | #if defined(ETHERNET) 3372 | client.print(ETH.macAddress()); 3373 | client.print(", "); 3374 | client.print(ETH.localIP()); 3375 | #else 3376 | client.print("OFF"); 3377 | #endif 3378 | 3379 | client.print(F(" | Version: ")); 3380 | client.print(F(REV)); 3381 | client.print(F(" | Uptime: ")); 3382 | client.print(millis()/1000); 3383 | client.println(F(" s
")); 3384 | #if defined(Ser2net) && !defined(XLswitch) 3385 | client.println(F(" Ser2net: ip port ")); 3386 | client.print(SerialServerIPport); 3387 | client.print(" ⇄ baudrate "); 3388 | client.print(SERIAL1_BAUDRATE); 3389 | client.println(F(" (rx gpio16, tx gpio17)
Source: ")); 3390 | #endif 3391 | if(TxUdpBuffer[2] == 'o'){ 3392 | client.println("Open Interface III"); 3393 | }else if(TxUdpBuffer[2] == 'r'){ 3394 | client.println("Band decoder MK2"); 3395 | }else if(TxUdpBuffer[2] == 'm'){ 3396 | client.println("Manual IP switch MK2"); 3397 | }else if(TxUdpBuffer[2] == 'n'){ 3398 | client.println("none"); 3399 | } 3400 | client.println(F("
NOTICE: showing this page slows down the device response.")); 3401 | 3402 | if(TxUdpBuffer[2]=='m' && EnableGroupPrefix){ 3403 | client.println(F("
")); 3404 | client.print(" 3431 | client.println(F("\">
")); 3432 | 3433 | client.print("
");
3434 |                   client.println("List detected IP control by NET-ID prefix");
3435 |                   for (int i = 0; i < 16; i++) {
3436 |                     if(DetectedRemoteSwPort[i]!=0){
3437 |                       client.print(i, HEX);
3438 |                       client.print(F("  "));
3439 |                       client.print(DetectedRemoteSw [i] [0]);
3440 |                       client.print(F("."));
3441 |                       client.print(DetectedRemoteSw [i] [1]);
3442 |                       client.print(F("."));
3443 |                       client.print(DetectedRemoteSw [i] [2]);
3444 |                       client.print(F("."));
3445 |                       client.print(DetectedRemoteSw [i] [3]);
3446 |                       client.print(F(":"));
3447 |                       client.println(DetectedRemoteSwPort [i]);
3448 |                     }
3449 |                   }
3450 |                   client.print("
"); 3451 | } 3452 | 3453 | client.println(F("

Wiki | ")); 3454 | client.println(F("

split↗

")); 3455 | client.println(F("")); 3456 | 3457 | if(EnableSerialDebug==1){ 3458 | Serial.print(HTTP_req); 3459 | } 3460 | HTTP_req = ""; 3461 | 3462 | break; 3463 | } 3464 | if (c == '\n') { 3465 | // you're starting a new line 3466 | currentLineIsBlank = true; 3467 | // if (strstr(linebuf,"GET /h0 ") > 0){digitalWrite(GPIOS[0], HIGH);}else if (strstr(linebuf,"GET /l0 ") > 0){digitalWrite(GPIOS[0], LOW);} 3468 | // else if (strstr(linebuf,"GET /h1 ") > 0){digitalWrite(GPIOS[1], HIGH);}else if (strstr(linebuf,"GET /l1 ") > 0){digitalWrite(GPIOS[1], LOW);} 3469 | 3470 | // you're starting a new line 3471 | currentLineIsBlank = true; 3472 | memset(linebuf,0,sizeof(linebuf)); 3473 | charcount=0; 3474 | } else if (c != '\r') { 3475 | // you've gotten a character on the current line 3476 | currentLineIsBlank = false; 3477 | } 3478 | } 3479 | } 3480 | // give the web browser time to receive the data 3481 | delay(1); 3482 | 3483 | // close the connection: 3484 | client.stop(); 3485 | if(EnableSerialDebug==1){ 3486 | Serial.println("WIFI client disconnected"); 3487 | } 3488 | } 3489 | } 3490 | //------------------------------------------------------------------------------------------------------- 3491 | 3492 | void EthEvent(WiFiEvent_t event) 3493 | { 3494 | switch (event) { 3495 | // case SYSTEM_EVENT_ETH_START: 3496 | case ARDUINO_EVENT_ETH_START: 3497 | Serial.println("ETH Started"); 3498 | //set eth hostname here 3499 | ETH.setHostname("esp32-eth"); 3500 | break; 3501 | // case SYSTEM_EVENT_ETH_CONNECTED: 3502 | case ARDUINO_EVENT_ETH_CONNECTED: 3503 | Serial.println("ETH Connected"); 3504 | break; 3505 | // case SYSTEM_EVENT_ETH_GOT_IP: 3506 | case ARDUINO_EVENT_ETH_GOT_IP: 3507 | Serial.print("ETH MAC: "); 3508 | Serial.println(ETH.macAddress()); 3509 | Serial.println("==============================="); 3510 | Serial.print(" IPv4: "); 3511 | Serial.println(ETH.localIP()); 3512 | Serial.println("==============================="); 3513 | if (ETH.fullDuplex()) { 3514 | Serial.print("FULL_DUPLEX, "); 3515 | } 3516 | Serial.print(ETH.linkSpeed()); 3517 | Serial.println("Mbps"); 3518 | eth_connected = true; 3519 | #if defined(XLswitch) 3520 | digitalWrite(StatusLedAPin, HIGH); 3521 | #endif 3522 | #if !defined(Ser2net) && !defined(XLswitch) && !defined(PCBrev04) 3523 | pinMode(BCD[1], OUTPUT); 3524 | digitalWrite(BCD[1], HIGH); 3525 | delay(100); 3526 | digitalWrite(BCD[1], LOW); 3527 | delay(100); 3528 | digitalWrite(BCD[1], HIGH); 3529 | delay(100); 3530 | digitalWrite(BCD[1], LOW); 3531 | delay(100); 3532 | digitalWrite(BCD[1], HIGH); 3533 | delay(100); 3534 | delay(100); 3535 | digitalWrite(BCD[1], LOW); 3536 | pinMode(BCD[1], INPUT); 3537 | #endif 3538 | if(TxUdpBuffer[2]!='n'){ 3539 | // clear 3540 | // Serial.println("Snake&clear"); 3541 | // Demo(); 3542 | #if defined(ShiftOut) 3543 | if(RebootWatchdog > 0 && TxUdpBuffer[2]!='n'){ 3544 | Serial.println("Restore outputs..."); 3545 | digitalWrite(ShiftOutLatchPin, LOW); // když dáme latchPin na LOW mužeme do registru poslat data 3546 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, 0x01); 3547 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[2]); 3548 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[1]); 3549 | shiftOut(ShiftOutDataPin, ShiftOutClockPin, LSBFIRST, ShiftOutByte[0]); 3550 | digitalWrite(ShiftOutLatchPin, HIGH); // jakmile dáme latchPin na HIGH data se objeví na výstupu 3551 | Serial.print(" Bank status ABC [LSBFIRST]: "); 3552 | Serial.print(ShiftOutByte[0], BIN); 3553 | Serial.print(" "); 3554 | Serial.print(ShiftOutByte[1], BIN); 3555 | Serial.print(" "); 3556 | Serial.println(ShiftOutByte[2], BIN); 3557 | } 3558 | #endif 3559 | } 3560 | // EnableSerialDebug=1; 3561 | TxUDP('s', packetBuffer[2], 'b', 'r', 'o', 0); // 0=broadcast, 1= direct to RX IP 3562 | if(TxUdpBuffer[2] == 'm'){ 3563 | TxUDP('s', packetBuffer[2], ShiftOutByte[0], ShiftOutByte[1], ShiftOutByte[2], 0); 3564 | } 3565 | // EnableSerialDebug=0; 3566 | #if defined(XLswitch) 3567 | LcdNeedRefresh=100; 3568 | #endif 3569 | break; 3570 | 3571 | // case SYSTEM_EVENT_ETH_DISCONNECTED: 3572 | case ARDUINO_EVENT_ETH_DISCONNECTED: 3573 | Serial.println("ETH Disconnected"); 3574 | eth_connected = false; 3575 | #if defined(XLswitch) 3576 | digitalWrite(StatusLedAPin, LOW); 3577 | #endif 3578 | break; 3579 | // case SYSTEM_EVENT_ETH_STOP: 3580 | case ARDUINO_EVENT_ETH_STOP: 3581 | Serial.println("ETH Stopped"); 3582 | eth_connected = false; 3583 | #if defined(XLswitch) 3584 | digitalWrite(StatusLedAPin, LOW); 3585 | #endif 3586 | break; 3587 | default: 3588 | break; 3589 | } 3590 | } 3591 | //------------------------------------------------------------------------------------------------------- 3592 | 3593 | void testClient(const char * host, uint16_t port) 3594 | { 3595 | Serial.print("\nETH connecting to "); 3596 | Serial.println(host); 3597 | WiFiClient client; 3598 | if (!client.connect(host, port)) { 3599 | Serial.println("ETH connection failed"); 3600 | return; 3601 | } 3602 | client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host); 3603 | while (client.connected() && !client.available()); 3604 | while (client.available()) { 3605 | Serial.write(client.read()); 3606 | } 3607 | Serial.println("ETH closing connection\n"); 3608 | client.stop(); 3609 | } 3610 | 3611 | //------------------------------------------------------------------------------------------------------- 3612 | void TelnetAuth(){ 3613 | 3614 | switch (TelnetAuthStep) { 3615 | case 0: { 3616 | if(TelnetLoginFails>=3 && millis()-TelnetLoginFailsBanTimer[0]2 && millis()-TelnetLoginFailsBanTimer[0]>TelnetLoginFailsBanTimer[1]){ 3625 | TelnetLoginFails=0; 3626 | } 3627 | if(TelnetLoginFails<=3){ 3628 | Prn(1, 1,"Login? [y/n] "); 3629 | TelnetAuthStep++; 3630 | incomingByte=0; 3631 | } 3632 | break; } 3633 | case 1: { 3634 | // incomingByte=TelnetRX(); 3635 | if(incomingByte==121 || incomingByte==89){ 3636 | Prn(1, 1,String(char(incomingByte))); 3637 | TelnetAuthStep++; 3638 | }else if(incomingByte!=121 && incomingByte!=0){ 3639 | TelnetAuthStep=0; 3640 | } 3641 | break; } 3642 | case 2: { 3643 | AuthQ(1, 0); 3644 | TelnetAuthStepFails=0; 3645 | break; } 3646 | case 3: { 3647 | if(incomingByte==key[RandomNumber]){ 3648 | Prn(1, 1, String(char(incomingByte)) ); 3649 | AuthQ(2, 0); 3650 | }else if(incomingByte!=0 && incomingByte!=key[RandomNumber]){ 3651 | Prn(1, 1, String(char(incomingByte)) ); 3652 | AuthQ(2, 1); 3653 | } 3654 | break; } 3655 | case 4: { 3656 | if(incomingByte==key[RandomNumber]){ 3657 | Prn(1, 1, String(char(incomingByte)) ); 3658 | AuthQ(3, 0); 3659 | }else if(incomingByte!=0 && incomingByte!=key[RandomNumber]){ 3660 | Prn(1, 1, String(char(incomingByte)) ); 3661 | AuthQ(3, 1); 3662 | } 3663 | break; } 3664 | case 5: { 3665 | if(incomingByte==key[RandomNumber]){ 3666 | Prn(1, 1, String(char(incomingByte)) ); 3667 | AuthQ(4, 0); 3668 | }else if(incomingByte!=0 && incomingByte!=key[RandomNumber]){ 3669 | Prn(1, 1, String(char(incomingByte)) ); 3670 | AuthQ(4, 1); 3671 | } 3672 | break; } 3673 | case 6: { 3674 | if(incomingByte==key[RandomNumber]){ 3675 | Prn(1, 1, String(char(incomingByte)) ); 3676 | TelnetAuthStep++; 3677 | incomingByte=0; 3678 | }else if(incomingByte!=0 && incomingByte!=key[RandomNumber]){ 3679 | Prn(1, 1, String(char(incomingByte)) ); 3680 | TelnetAuthStep++; 3681 | incomingByte=0; 3682 | TelnetAuthStepFails++; 3683 | } 3684 | break; } 3685 | case 7: { 3686 | if(TelnetAuthStepFails==0){ 3687 | TelnetAuthorized = true; 3688 | TelnetServerClientAuth = TelnetServerClients[0].remoteIP(); 3689 | Prn(1, 1,"Login OK"); 3690 | ListCommands(1); 3691 | TelnetAuthStep++; 3692 | incomingByte=0; 3693 | }else{ 3694 | TelnetAuthorized = false; 3695 | TelnetServerClientAuth = {0,0,0,0}; 3696 | Prn(1, 1,"Access denied"); 3697 | TelnetAuthStep=0; 3698 | incomingByte=0; 3699 | TelnetLoginFails++; 3700 | TelnetLoginFailsBanTimer[0]=millis(); 3701 | } 3702 | EEPROM.write(37, TelnetServerClientAuth[0]); // address, value 3703 | EEPROM.write(38, TelnetServerClientAuth[1]); // address, value 3704 | EEPROM.write(39, TelnetServerClientAuth[2]); // address, value 3705 | EEPROM.write(40, TelnetServerClientAuth[3]); // address, value 3706 | EEPROM.commit(); 3707 | break; } 3708 | } 3709 | } 3710 | 3711 | //------------------------------------------------------------------------------------------------------- 3712 | 3713 | void AuthQ(int NR, bool BAD){ 3714 | Prn(1, 0,"What character is at "); 3715 | RandomNumber=random(0, strlen(key)); 3716 | Prn(1, 0, String(RandomNumber+1) ); 3717 | Prn(1, 0," position, in key? ("); 3718 | Prn(1, 0,String(NR)); 3719 | Prn(1, 1,"/4)"); 3720 | // Prn(1, 1, String(key[RandomNumber]) ); 3721 | TelnetAuthStep++; 3722 | incomingByte=0; 3723 | if(BAD==true){ 3724 | TelnetAuthStepFails++; 3725 | } 3726 | } 3727 | 3728 | //------------------------------------------------------------------------------------------------------- 3729 | void Telnet(){ 3730 | uint8_t i; 3731 | // if (wifiMulti.run() == WL_CONNECTED) { 3732 | if (eth_connected==true) { 3733 | 3734 | //check if there are any new clients 3735 | if (TelnetServer.hasClient()){ 3736 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 3737 | //find free/disconnected spot 3738 | if (!TelnetServerClients[i] || !TelnetServerClients[i].connected()){ 3739 | if(TelnetServerClients[i]) TelnetServerClients[i].stop(); 3740 | TelnetServerClients[i] = TelnetServer.available(); 3741 | if (!TelnetServerClients[i]) Serial.println("Telnet available broken"); 3742 | if(EnableSerialDebug==1){ 3743 | Serial.println(); 3744 | Serial.print("New Telnet client: "); 3745 | Serial.print(i); Serial.print(' '); 3746 | Serial.println(TelnetServerClients[i].remoteIP()); 3747 | } 3748 | break; 3749 | } 3750 | } 3751 | if (i >= MAX_SRV_CLIENTS) { 3752 | //no free/disconnected spot so reject 3753 | TelnetServer.available().stop(); 3754 | } 3755 | } 3756 | 3757 | //check clients for data 3758 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 3759 | if (TelnetServerClients[i] && TelnetServerClients[i].connected()){ 3760 | if(TelnetServerClients[i].available()){ 3761 | //get data from the telnet client and push it to the UART 3762 | // while(TelnetServerClients[i].available()) Serial_one.write(TelnetServerClients[i].read()); 3763 | if(EnableSerialDebug==1){ 3764 | Serial.println(); 3765 | Serial.print("TelnetRX "); 3766 | } 3767 | 3768 | while(TelnetServerClients[i].available()){ 3769 | incomingByte=TelnetServerClients[i].read(); 3770 | // Serial_one.write(RX); 3771 | if(EnableSerialDebug==1){ 3772 | // Serial.write(RX); 3773 | Serial.print(char(incomingByte)); 3774 | } 3775 | } 3776 | } 3777 | }else{ 3778 | if (TelnetServerClients[i]) { 3779 | TelnetServerClients[i].stop(); 3780 | TelnetAuthorized=false; 3781 | // TelnetServerClientAuth = {0,0,0,0}; 3782 | } 3783 | } 3784 | } 3785 | 3786 | //check UART for data 3787 | // if(Serial_one.available()){ 3788 | // size_t len = Serial_one.available(); 3789 | // uint8_t sbuf[len]; 3790 | // Serial_one.readBytes(sbuf, len); 3791 | // //push UART data to all connected telnet clients 3792 | // for(i = 0; i < MAX_SRV_CLIENTS; i++){ 3793 | // if (TelnetServerClients[i] && TelnetServerClients[i].connected()){ 3794 | // TelnetServerClients[i].write(sbuf, len); 3795 | // // delay(1); 3796 | // if(EnableSerialDebug==1){ 3797 | // Serial.println(); 3798 | // Serial.print("Telnet tx-"); 3799 | // Serial.write(sbuf, len); 3800 | // } 3801 | // } 3802 | // } 3803 | // } 3804 | 3805 | }else{ 3806 | if(EnableSerialDebug==1){ 3807 | Serial.println("Telnet not connected!"); 3808 | } 3809 | for(i = 0; i < MAX_SRV_CLIENTS; i++) { 3810 | if (TelnetServerClients[i]) TelnetServerClients[i].stop(); 3811 | } 3812 | delay(1000); 3813 | } 3814 | } 3815 | 3816 | //------------------------------------------------------------------------------------------------------- 3817 | 3818 | void SerialToIp(){ 3819 | #if defined(Ser2net) && !defined(XLswitch) 3820 | uint8_t i; 3821 | // if (wifiMulti.run() == WL_CONNECTED) { 3822 | if (eth_connected==true) { 3823 | //check if there are any new clients 3824 | if (SerialServer.hasClient()){ 3825 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 3826 | //find free/disconnected spot 3827 | if (!SerialServerClients[i] || !SerialServerClients[i].connected()){ 3828 | if(SerialServerClients[i]) SerialServerClients[i].stop(); 3829 | SerialServerClients[i] = SerialServer.available(); 3830 | if (!SerialServerClients[i]) Serial.println("available broken"); 3831 | if(EnableSerialDebug==1){ 3832 | Serial.println(); 3833 | Serial.print("New Ser2net client: "); 3834 | Serial.print(i); Serial.print(' '); 3835 | Serial.println(SerialServerClients[i].remoteIP()); 3836 | } 3837 | break; 3838 | } 3839 | } 3840 | if (i >= MAX_SRV_CLIENTS) { 3841 | //no free/disconnected spot so reject 3842 | SerialServer.available().stop(); 3843 | } 3844 | } 3845 | //check clients for data 3846 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 3847 | if (SerialServerClients[i] && SerialServerClients[i].connected()){ 3848 | if(SerialServerClients[i].available()){ 3849 | //get data from the telnet client and push it to the UART 3850 | // while(SerialServerClients[i].available()) Serial_one.write(SerialServerClients[i].read()); 3851 | if(EnableSerialDebug==1){ 3852 | Serial.println(); 3853 | Serial.print("rx-"); 3854 | } 3855 | 3856 | /* 3857 | Zapinaci pakety 3858 | < 2019/04/17 22:05:29.670771 length=1 from=2009 to=2009 3859 | 0d 3860 | < 2019/04/17 22:05:29.734563 length=3 from=2010 to=2012 3861 | 30 31 0d 3862 | > 2019/04/17 22:05:29.924267 length=2 from=3338 to=3339 3863 | 0d 0d 3864 | > 2019/04/17 22:05:29.972263 length=3 from=3340 to=3342 3865 | 30 31 0d 3866 | < 2019/04/17 22:05:30.086153 length=1 from=2013 to=2013 3867 | 0d 3868 | > 2019/04/17 22:05:30.249406 length=3 from=3343 to=3345 3869 | 30 31 0d 3870 | < 2019/04/17 22:05:30.261738 length=2 from=2014 to=2015 3871 | ff 0d 3872 | 3873 | Udrzovaci pakety 3874 | > 2019/04/17 21:59:00.549347 length=2 from=3319 to=3320 3875 | ff 0d 3876 | < 2019/04/17 21:59:00.559100 length=2 from=1998 to=1999 3877 | ff 0d 3878 | < 2019/04/17 21:59:02.555837 length=1 from=2000 to=2000 3879 | 0d 3880 | < 2019/04/17 21:59:04.568329 length=1 from=2001 to=2001 3881 | 0d 3882 | > 2019/04/17 21:59:05.549832 length=2 from=3321 to=3322 3883 | ff 0d 3884 | < 2019/04/17 21:59:05.558790 length=2 from=2002 to=2003 3885 | ff 0d 3886 | 3887 | Vypinaci Paket 3888 | < 2019/04/17 21:59:10.382425 length=3 from=2006 to=2008 3889 | 30 30 0d 3890 | > 2019/04/17 21:59:10.388125 length=15 from=3323 to=3337 3891 | 0d 0d 0d 0d 0d 0d 0d 0d 0d 0d 0d 0d 30 30 0d 3892 | 3893 | */ 3894 | 3895 | 3896 | while(SerialServerClients[i].available()){ 3897 | byte RX; 3898 | RX=SerialServerClients[i].read(); 3899 | Serial_one.write(RX); 3900 | if(EnableSerialDebug==1){ 3901 | Serial.write(RX); 3902 | } 3903 | } 3904 | } 3905 | } 3906 | else { 3907 | if (SerialServerClients[i]) { 3908 | SerialServerClients[i].stop(); 3909 | } 3910 | } 3911 | } 3912 | //check UART for data 3913 | if(Serial_one.available()){ 3914 | size_t len = Serial_one.available(); 3915 | uint8_t sbuf[len]; 3916 | Serial_one.readBytes(sbuf, len); 3917 | //push UART data to all connected telnet clients 3918 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 3919 | if (SerialServerClients[i] && SerialServerClients[i].connected()){ 3920 | SerialServerClients[i].write(sbuf, len); 3921 | // delay(1); 3922 | if(EnableSerialDebug==1){ 3923 | Serial.println(); 3924 | Serial.print("tx-"); 3925 | Serial.write(sbuf, len); 3926 | } 3927 | } 3928 | } 3929 | } 3930 | } 3931 | else { 3932 | if(EnableSerialDebug==1){ 3933 | Serial.println("Ser2net not connected!"); 3934 | } 3935 | for(i = 0; i < MAX_SRV_CLIENTS; i++) { 3936 | if (SerialServerClients[i]) SerialServerClients[i].stop(); 3937 | } 3938 | delay(1000); 3939 | } 3940 | #endif 3941 | } 3942 | 3943 | //------------------------------------------------------------------------------------------------------- 3944 | #if defined(XLswitch) 3945 | unsigned long testFastLines(uint16_t color1, uint16_t color2) { 3946 | unsigned long start; 3947 | int x, y, w = tft.width(), h = tft.height(); 3948 | 3949 | tft.fillScreen(ILI9341_BLACK); 3950 | start = micros(); 3951 | for(y=0; y0; i-=6) { 3966 | i2 = i / 2; 3967 | start = micros(); 3968 | tft.fillRect(cx-i2, cy-i2, i, i, color1); 3969 | t += micros() - start; 3970 | // Outlines are not included in timing results 3971 | tft.drawRect(cx-i2, cy-i2, i, i, color2); 3972 | yield(); 3973 | } 3974 | 3975 | return t; 3976 | } 3977 | 3978 | bool Get_yes_no(void){ 3979 | TS_Point p; 3980 | tft.setTextColor(ILI9341_WHITE); tft.setTextSize(3); 3981 | 3982 | tft.fillRoundRect(20,250, 100, 50,8, ILI9341_RED); 3983 | tft.setCursor(56, 265); 3984 | tft.println("NO"); 3985 | tft.drawRoundRect(20,250, 100, 50, 8, ILI9341_WHITE); 3986 | 3987 | tft.fillRoundRect(120,250, 100, 50,8, ILI9341_GREEN); 3988 | tft.setCursor(144, 265); 3989 | tft.println("YES"); 3990 | tft.drawRoundRect(120,250, 100, 50, 8, ILI9341_WHITE); 3991 | 3992 | 3993 | while (1){ 3994 | delay(50); 3995 | p = ts.getPoint(); 3996 | 3997 | if (p.z != 129){ 3998 | 3999 | 4000 | p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width()); 4001 | p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height()); 4002 | p.y = 320 - p.y; 4003 | 4004 | // tft.fillCircle(p.x, p.y, 5, ILI9341_YELLOW); 4005 | 4006 | 4007 | if ((p.y > 250) && (p.y<300)){ 4008 | 4009 | if ((p.x> 20) && (p.x < 220)){ 4010 | if (p.x>120) 4011 | { 4012 | tft.fillRoundRect(120,250, 100, 50,8, ILI9341_OLIVE); 4013 | tft.setCursor(144, 265); 4014 | tft.println("YES"); 4015 | tft.drawRoundRect(120,250, 100, 50, 8, ILI9341_WHITE); 4016 | 4017 | delay(500); 4018 | return true; 4019 | } 4020 | else{ 4021 | 4022 | tft.fillRoundRect(20,250, 100, 50,8, ILI9341_OLIVE); 4023 | tft.setCursor(56, 265); 4024 | tft.println("NO"); 4025 | tft.drawRoundRect(20,250, 100, 50, 8, ILI9341_WHITE); 4026 | 4027 | delay(500); 4028 | return false; 4029 | } 4030 | } 4031 | 4032 | } 4033 | 4034 | } 4035 | } 4036 | } 4037 | #endif 4038 | --------------------------------------------------------------------------------