diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..52b0f12 Binary files /dev/null and b/.DS_Store differ diff --git a/ESPboyLED.cpp b/ESPboyLED.cpp index bc3c56e..c0b64ca 100644 --- a/ESPboyLED.cpp +++ b/ESPboyLED.cpp @@ -84,7 +84,6 @@ void ICACHE_RAM_ATTR ESPboyLED::ledset(uint8_t rled, uint8_t gled, uint8_t bled) static uint_fast32_t i, t, c, startTime, pixel, mask, t0h, t1h, ttot; static uint8_t cpuFreq; static const uint32_t pinMask = 1<begin(*tft); + u8f->setFontMode(1); // use u8g2 none transparent mode + u8f->setBackgroundColor(TFT_BLACK); + u8f->setFontDirection(0); // left to right + u8f->setFont(u8g2_font_4x6_t_cyrillic); +#endif +} + + +uint8_t ESPboyMenuGUI::getKeys() { return (~mcp->readGPIOAB() & 255); } + + +void ESPboyMenuGUI::menuDraw(){ + static uint16_t scalingFactor; + static uint16_t previousRect = 0; + + if(menuList.menuItemsQuantity>1){ + if(menuList.menuItemsQuantity>=MENU_MAX_LINES_ONSCREEN) + scalingFactor = ((MENU_MAX_LINES_ONSCREEN*MENU_SPACE_BETWEEN_LINES-6)*1000)/(menuList.menuItemsQuantity-1); + else + scalingFactor = ((menuList.menuItemsQuantity*MENU_SPACE_BETWEEN_LINES-6)*1000)/(menuList.menuItemsQuantity-1); + } + else scalingFactor=1; + + tft->drawRect(0, previousRect*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, TFT_BLACK); + tft->fillRect(125,0, 3, 128, TFT_BLACK); + + if (menuList.menuCurrent+1 > MENU_MAX_LINES_ONSCREEN + menuList.menuOffset) { + tft->fillScreen(TFT_BLACK); + menuList.menuOffset++;} + if (menuList.menuCurrent < menuList.menuOffset) { + tft->fillScreen(TFT_BLACK); + menuList.menuOffset--;} + + if(menuList.menuItemsQuantity>=MENU_MAX_LINES_ONSCREEN) + tft->drawLine(126,0, 126, MENU_MAX_LINES_ONSCREEN*MENU_SPACE_BETWEEN_LINES-2, TFT_BLUE); + else + tft->drawLine(126,0, 126, menuList.menuItemsQuantity*MENU_SPACE_BETWEEN_LINES-2, TFT_BLUE); + + for (uint8_t i=0;; i++){ + if(i>=menuList.menuItemsQuantity || i>=MENU_MAX_LINES_ONSCREEN) break; + + #ifndef U8g2 + if(menuList.menuLine[i+menuList.menuOffset][0] == '-'){ + tft->setTextColor(menuList.menuUnselectedLineColor); + tft->drawString(&menuList.menuLine[i+menuList.menuOffset][1], 3, i*MENU_SPACE_BETWEEN_LINES+2); + } + else{ + tft->setTextColor(menuList.menuLineColor); + tft->drawString(menuList.menuLine[i+menuList.menuOffset], 3, i*MENU_SPACE_BETWEEN_LINES+2); + } + #else + if(menuList.menuLine[i+menuList.menuOffset][0] == '-'){ + u8f->setForegroundColor(menuList.menuUnselectedLineColor); + u8f->drawStr(3, i*MENU_SPACE_BETWEEN_LINES+1+GUI_FONT_HEIGHT, &menuList.menuLine[i+menuList.menuOffset][1]); + } + else { + u8f->setForegroundColor(menuList.menuLineColor); + u8f->drawStr(3, i*MENU_SPACE_BETWEEN_LINES+1+GUI_FONT_HEIGHT, menuList.menuLine[i+menuList.menuOffset]); + } + #endif + + if((i+menuList.menuOffset) == menuList.menuCurrent){ + tft->drawRect(0, i*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, menuList.menuSelectionColor); + previousRect=i;} + } + + tft->fillRect(125, (scalingFactor*menuList.menuCurrent+2)/1000, 3, 5, TFT_YELLOW); +} + + + +uint16_t ESPboyMenuGUI::menuInit(const char** menuLinesF, uint16_t menuLineColorF, uint16_t menuUnselectedLineColorF, uint16_t menuSelectionColorF){ + uint16_t count=0; + static uint8_t keyPressed; + tft->fillScreen(TFT_BLACK); + menuList.menuLine = menuLinesF; + menuList.menuLineColor = menuLineColorF; + menuList.menuUnselectedLineColor = menuUnselectedLineColorF; + menuList.menuCurrent = 0; + menuList.menuSelectionColor = menuSelectionColorF; + menuList.menuOffset=0; + while(menuLinesF[count++]); + menuList.menuItemsQuantity = count-1; + + menuDraw(); + + while(1){ + while (!getKeys())delay(50); + + keyPressed = getKeys(); + + if (keyPressed&MenuGUI_PAD_UP && menuList.menuCurrent > 0) { + menuList.menuCurrent--; + #ifdef buttonclicks + tone(SOUNDPIN,10,10); + #endif + menuDraw(); + } + if (keyPressed&MenuGUI_PAD_DOWN && menuList.menuCurrent+1 < menuList.menuItemsQuantity) { + menuList.menuCurrent++; + #ifdef buttonclicks + tone(SOUNDPIN,10,10); + #endif + menuDraw(); + } + if (keyPressed&MenuGUI_PAD_ACT && menuList.menuLine[menuList.menuCurrent][0] != '-') { + #ifdef buttonclicks + tone(SOUNDPIN,100,100); + #endif + + tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, TFT_BLACK); + delay(50); + tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, menuList.menuSelectionColor); + delay(50); + tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, TFT_BLACK); + delay(50); + tft->drawRect(0, (menuList.menuCurrent+menuList.menuOffset)*MENU_SPACE_BETWEEN_LINES, 122, MENU_SPACE_BETWEEN_LINES, menuList.menuSelectionColor); + delay(200); + + tft->fillScreen(TFT_BLACK); + return(menuList.menuCurrent+1); + } + if (keyPressed&MenuGUI_PAD_ESC){ + #ifdef buttonclicks + tone(SOUNDPIN,100,100); + #endif + tft->fillScreen(TFT_BLACK); + return(0); + } + + delay(120); + } +}; diff --git a/ESPboyMenuGUI.h b/ESPboyMenuGUI.h new file mode 100644 index 0000000..6cfd2ac --- /dev/null +++ b/ESPboyMenuGUI.h @@ -0,0 +1,75 @@ +/* +ESPboy_MenuGUI class +for www.ESPboy.com project by RomanS +https://hackaday.io/project/164830-espboy-games-iot-stem-for-education-fun +v1.0 +*/ + +//!!!!!!!!!!!!!!!!! +//#define U8g2 //if defined then using font 4x6, if commented using font 6x8 +#define buttonclicks //if defined - button are clicking but it takes more than 1kb RAM, if commented - no clicks and more free RAM +////!!!!!!!!!!!!!!!!! + + +#ifndef ESPboy_MenuGUI +#define ESPboy_MenuGUI + +#include +#include +#include +using fs::FS; + +#ifdef U8g2 + #include "U8g2_for_TFT_eSPI.h" +#endif + +#ifdef U8g2 + #define GUI_FONT_WIDTH 4 + #define GUI_FONT_HEIGHT 6 +#else + #define GUI_FONT_WIDTH 6 + #define GUI_FONT_HEIGHT 8 +#endif + +#define MENU_SPACE_BETWEEN_LINES (GUI_FONT_HEIGHT+3) +#define MENU_MAX_LINES_ONSCREEN (128/MENU_SPACE_BETWEEN_LINES) + +#define MenuGUI_PAD_LEFT 0x01 +#define MenuGUI_PAD_UP 0x02 +#define MenuGUI_PAD_DOWN 0x04 +#define MenuGUI_PAD_RIGHT 0x08 +#define MenuGUI_PAD_ACT 0x10 +#define MenuGUI_PAD_ESC 0x20 +#define MenuGUI_PAD_LFT 0x40 +#define MenuGUI_PAD_RGT 0x80 +#define MenuGUI_PAD_ANY 0xff + + +class ESPboyMenuGUI{ + +private: + Adafruit_MCP23017 *mcp; + TFT_eSPI *tft; +#ifdef U8g2 + U8g2_for_TFT_eSPI *u8f; +#endif + +struct menuStruct{ + const char **menuLine; + uint16_t menuOffset; + uint16_t menuItemsQuantity; + uint16_t menuLineColor; + uint16_t menuUnselectedLineColor; + uint16_t menuSelectionColor; + uint16_t menuCurrent; +} menuList; + +uint8_t getKeys(); +void menuDraw(); + +public: + ESPboyMenuGUI(TFT_eSPI *tftMenuGUI, Adafruit_MCP23017 *mcpMenuGUI); + uint16_t menuInit(const char** menuLinesF, uint16_t menuLineColorF, uint16_t menuUnselectedLineColorF, uint16_t menuSelectionColorF); +}; + +#endif diff --git a/ESPboyTerminalGUI.cpp b/ESPboyTerminalGUI.cpp index b0af9b2..5177220 100644 --- a/ESPboyTerminalGUI.cpp +++ b/ESPboyTerminalGUI.cpp @@ -156,9 +156,32 @@ String ESPboyTerminalGUI::getUserInput() { return (userInput); } +void ESPboyTerminalGUI::doScroll(){ + uint8_t keyState=0; + #ifdef buttonclicks + tone(SOUNDPIN, 100, 10); + #endif + toggleDisplayMode(1); + while (!(keyState & GUI_PAD_ESC)){ + delay(1); + keyState = getKeys(); + if(keyState){ + if (keyState&GUI_PAD_RGT && keybParam.renderLine) { + keybParam.renderLine--;} + if (keyState&GUI_PAD_LFT && keybParam.renderLine < consoleStringsVector.size() - GUI_MAX_STRINGS_ONSCREEN_SMALL) { + keybParam.renderLine++;} + drawConsole(0); + #ifdef buttonclicks + tone(SOUNDPIN, 100, 10); + #endif + delay(GUI_KEYB_CALL_DELAY); + } + } +} + void ESPboyTerminalGUI::printConsole(String bfrstr, uint16_t color, uint8_t ln, uint8_t noAddLine) { String toprint; - + keybParam.renderLine = 0; if(bfrstr == "") bfrstr = " "; diff --git a/ESPboyTerminalGUI.h b/ESPboyTerminalGUI.h index 580f71b..8f993c5 100644 --- a/ESPboyTerminalGUI.h +++ b/ESPboyTerminalGUI.h @@ -17,8 +17,6 @@ v2.1 #ifndef ESPboy_TerminalGUI #define ESPboy_TerminalGUI -#define CSTFTPIN 8 - #include #include #include @@ -97,6 +95,7 @@ public: uint32_t waitKeyUnpressed(); void printConsole(String bfrstr, uint16_t color, uint8_t ln, uint8_t noAddLine); String getUserInput(); + void doScroll(); void toggleDisplayMode(uint8_t mode); void drawOwnTypingLine(String typingLine, uint16_t colorLine); }; diff --git a/ESPboy_Sub1GHz.ino b/ESPboy_Sub1GHz.ino index 11a55b3..d734198 100644 --- a/ESPboy_Sub1GHz.ino +++ b/ESPboy_Sub1GHz.ino @@ -1,19 +1,60 @@ #include "ESPboyInit.h" #include "ESPboyTerminalGUI.h" +#include "ESPboyMenuGUI.h" #include "ESPboyLED.h" #include #include +#include #define CC1101riceivePin 3 #define CC1101chipSelectPin D8 #define TFTchipSelectPin 8 +#define MAX_RECORDS_TO_STORE 40 + + +const char *menuMain[] PROGMEM = { + "LISTEN", + "LISTEN & STORE", + "LIST OF STORED", + "INFO", //Number of stored, free to store + "CLEAR ALL", //clears all unprotected records + "FORMAT", //clears all records + NULL // the last element of every menu list should be NULL +}; + +const char *menuRecord[] PROGMEM = { + "SEND", //send selected record + "SHOW", + "SET REPEAT", + "RENAME", + "PROTECT", + "UNPROTECT", + "DELETE", + NULL +}; + +enum {listen_, listenstore_, listofstored_, info_, clearall_, format_}; +enum {send_, show_, setrepeat_, rename_, protect_, unprotect_, delete_}; RCSwitch mySwitch; ESPboyInit myESPboy; -ESPboyTerminalGUI* terminalGUIobj = NULL; +ESPboyTerminalGUI *terminalGUIobj = NULL; +//ESPboyMenuGUI *menuGUIobj = NULL; ESPboyLED myLED; -uint32_t counter; +char EEPROMmagicNo[4]={0xCC,0xCD,0xCE,0};//EEPROM marker of Sub1Ghz storage + +struct recordStored{ + char recordName[20]; + uint8_t protectedFlag; + uint8_t repeatNo; + uint8_t protocol; + uint32_t pulseLength; + uint32_t Bitlength; + uint32_t Value; +}; + +std::vector recordStoredVector; String bin2tri(uint32_t dec) { uint8_t pos = 0; @@ -34,114 +75,128 @@ String bin2tri(uint32_t dec) { } +void printConsoleLocal(String str, uint32_t clr, uint8_t flag1, uint8_t flag2){ + myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); + digitalWrite(CC1101chipSelectPin, HIGH); + printConsoleLocal(str, clr, flag1, flag2); + myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); + digitalWrite(CC1101chipSelectPin, LOW); +} + + + void output() { String toPrint; uint32_t *rawData; uint32_t dec, lengthval, rssi, lqi; - myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); - myESPboy.mcp.digitalWrite(CC1101chipSelectPin, LOW); - delay(10); - dec = mySwitch.getReceivedValue(); lengthval = mySwitch.getReceivedBitlength(); rssi = ELECHOUSE_cc1101.getRssi(); lqi = ELECHOUSE_cc1101.getLqi(); - myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); - myESPboy.mcp.digitalWrite(CC1101chipSelectPin, HIGH); - delay(10); - - terminalGUIobj->printConsole(" ", TFT_MAGENTA, 1, 1); - terminalGUIobj->printConsole(" ", TFT_MAGENTA, 1, 0); - terminalGUIobj->printConsole(F("-------------------------------"), TFT_YELLOW, 1, 0); - terminalGUIobj->printConsole(F("DETECTED! DECODE:"), TFT_RED, 1, 0); - terminalGUIobj->printConsole(F("-------------------------------"), TFT_YELLOW, 1, 0); + printConsoleLocal(" ", TFT_MAGENTA, 1, 1); + printConsoleLocal(" ", TFT_MAGENTA, 1, 0); + printConsoleLocal(F("-------------------------------"), TFT_YELLOW, 1, 0); + printConsoleLocal(F("DETECTED! DECODE:"), TFT_RED, 1, 0); + printConsoleLocal(F("-------------------------------"), TFT_YELLOW, 1, 0); //toPrint = "RSSI/LQI: "; //toPrint += (String)rssi; //toPrint += "/"; //toPrint += (String)lqi; - //terminalGUIobj->printConsole(toPrint, TFT_YELLOW, 1, 0); + //printConsoleLocal(toPrint, TFT_YELLOW, 1, 0); toPrint="DEC: "; toPrint+=(String)dec; toPrint+=" ("; toPrint+= (String)lengthval; toPrint+= " Bit)"; - terminalGUIobj->printConsole(toPrint, TFT_GREEN, 1, 0); + printConsoleLocal(toPrint, TFT_GREEN, 1, 0); - toPrint="BIN: "; - toPrint+= String (dec,BIN); - terminalGUIobj->printConsole(toPrint, TFT_GREEN, 1, 0); + toPrint = String (dec,BIN); + while(toPrint.length()printConsole(toPrint, TFT_WHITE, 1, 0); + printConsoleLocal(toPrint, TFT_WHITE, 1, 0); toPrint="PULSE LEN: "; toPrint+=(String)mySwitch.getReceivedDelay(); toPrint+=" ms"; - terminalGUIobj->printConsole(toPrint, TFT_WHITE, 1, 0); + printConsoleLocal(toPrint, TFT_WHITE, 1, 0); toPrint="FIRST LEV: "; if (mySwitch.getReceivedLevelInFirstTiming()) toPrint += "HIGH"; else toPrint+= "LOW"; - terminalGUIobj->printConsole(toPrint, TFT_WHITE, 1, 0); + printConsoleLocal(toPrint, TFT_WHITE, 1, 0); toPrint="PROTOCOL: "; toPrint+=(String)mySwitch.getReceivedProtocol(); - terminalGUIobj->printConsole(toPrint, TFT_WHITE, 1, 0); + printConsoleLocal(toPrint, TFT_WHITE, 1, 0); toPrint="IS INV: "; if (mySwitch.getReceivedInverted()) toPrint+="YES"; else toPrint+="NO"; - terminalGUIobj->printConsole(toPrint, TFT_WHITE, 1, 0); + printConsoleLocal(toPrint, TFT_WHITE, 1, 0); - terminalGUIobj->printConsole(F("DATA: "), TFT_YELLOW, 1, 0); + printConsoleLocal(F("DATA: "), TFT_YELLOW, 1, 0); toPrint=""; rawData = mySwitch.getReceivedRawdata(); for (unsigned int i=0; i<= lengthval*2; i++) toPrint+=((String)rawData[i]+","); toPrint = toPrint.substring(0,toPrint.length()-1); - terminalGUIobj->printConsole(toPrint, TFT_BLUE, 1, 0); + printConsoleLocal(toPrint, TFT_BLUE, 1, 0); - terminalGUIobj->printConsole(F("-------------------------------"), TFT_YELLOW, 1, 0); - terminalGUIobj->printConsole("", TFT_BLACK, 1, 0); + printConsoleLocal(F("-------------------------------"), TFT_YELLOW, 1, 0); + printConsoleLocal("", TFT_BLACK, 1, 0); - myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); - myESPboy.mcp.digitalWrite(CC1101chipSelectPin, LOW); - delay(10); } +void readEEPROM(){ + //EEPROM.get(0, eepromVar1); + }; + + +void writeEEPROM(){ + //EEPROM.put(0, eepromVar1); + //EEPROM.commit(); + }; + + void setup(){ + EEPROM.begin(4096); + myESPboy.begin("ESPboy Sub1GHz module"); myLED.begin(&myESPboy.mcp); terminalGUIobj = new ESPboyTerminalGUI(&myESPboy.tft, &myESPboy.mcp); + //menuGUIobj = new ESPboyMenuGUI(&myESPboy.tft, &myESPboy.mcp); - terminalGUIobj->printConsole(F("Sub1GHz module v1.0"), TFT_MAGENTA, 1, 0); - terminalGUIobj->printConsole("", TFT_BLACK, 1, 0); - - myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); - digitalWrite(CC1101chipSelectPin, LOW); - delay(10); - + delay(500); + printConsoleLocal(F("Sub1GHz module v1.0"), TFT_MAGENTA, 1, 0); + printConsoleLocal("", TFT_BLACK, 1, 0); + delay(5000); + ELECHOUSE_cc1101.Init(); // must be set to initialize the cc1101! - //ELECHOUSE_cc1101.setRxBW(812.50); // Set the Receive Bandwidth in kHz. Value from 58.03 to 812.50. Default is 812.50 kHz. + ELECHOUSE_cc1101.setRxBW(812.50); // Set the Receive Bandwidth in kHz. Value from 58.03 to 812.50. Default is 812.50 kHz. ELECHOUSE_cc1101.setMHZ(433.92); //The cc1101 can: 300-348 MHZ, 387-464MHZ and 779-928MHZ. mySwitch.enableReceive(CC1101riceivePin); // Receiver on interrupt 0 => that is pin #2 ELECHOUSE_cc1101.SetRx(); // set Receive on - - } - -void loop(){ +void listen_f(uint8_t storeFlag){ static String str=""; static uint8_t ledFlag = 1; - + static uint32_t counter; + + + while(!myESPboy.getKeys()){ + if (mySwitch.available()) { myLED.setRGB(0,20,0); ledFlag = 1; @@ -154,33 +209,50 @@ void loop(){ if(millis()-counter>2000){ counter=millis(); - - myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); - digitalWrite(CC1101chipSelectPin, HIGH); - delay(10); - terminalGUIobj->printConsole(str, TFT_MAGENTA, 1, 1); - myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); - digitalWrite(CC1101chipSelectPin, LOW); - + printConsoleLocal(str, TFT_MAGENTA, 1, 1); str+="."; if (str.length()>20) str=""; - - delay(10); } if(ledFlag){ ledFlag=0; myLED.setRGB(0,0,0); - - myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); - digitalWrite(CC1101chipSelectPin, HIGH); - delay(10); - terminalGUIobj->printConsole(F("Listening..."), TFT_MAGENTA, 1, 0); - terminalGUIobj->printConsole(F(""), TFT_MAGENTA, 1, 0); - myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); - digitalWrite(CC1101chipSelectPin, LOW); - delay(10); + printConsoleLocal(F("Listening..."), TFT_MAGENTA, 1, 0); + printConsoleLocal(F(""), TFT_MAGENTA, 1, 0); } - - delay(1); + delay(10); +} +} + + +void loop(){ + listen_f(1); + /*uint16_t menuItem; + myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); + digitalWrite(CC1101chipSelectPin, HIGH); + delay(10); + menuItem = menuGUIobj->menuInit(menuMain, TFT_YELLOW, TFT_BLUE, TFT_BLUE); + myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); + digitalWrite(CC1101chipSelectPin, LOW); + delay(10); + + + switch (menuItem){ + case listen_: + listen_f(0); + break; + case listenstore_: + listen_f(1); + break; + case listofstored_: + break; + case info_: + break; + case clearall_: + break; + case format_: + break; + default: + break; + }*/ } diff --git a/ESPboy_Sub1GHz2.ino b/ESPboy_Sub1GHz2.ino new file mode 100644 index 0000000..b77f62d --- /dev/null +++ b/ESPboy_Sub1GHz2.ino @@ -0,0 +1,567 @@ +/* +ESPboy Sub1Ghz inspector +for www.ESPboy.com project by RomanS +https://hackaday.io/project/164830-espboy-games-iot-stem-for-education-fun +v1.0 +*/ + +#include "ESPboyInit.h" +#include "ESPboyTerminalGUI.h" +#include "ESPboyMenuGUI.h" +#include "ESPboyLED.h" +#include +#include +#include + +#define CC1101riceivePin 3 +#define CC1101sendPin 2 +#define CC1101chipSelectPin D8 +#define TFTchipSelectPin 8 + +#define MAX_RECORDS_TO_STORE 40 +#define DEFAULT_SIGNAL_REPEAT_NUMBER 3 + +String protDecode[]={ + "Unknown", + "350 {1,31} {1,3} {3,1} false", // protocol1 + "650 {1,10} {1,2} {2,1} false", // protocol 2 + "100 {30 71} {4 11} {9 6} false", // protocol 3 + "380 {1 6} {1 3} {3 1} false", // protocol 4 + "500 {6 14} {1 2} {2 1} false", // protocol 5 + "450 {23 1} {1 2} {2 1} true"}; // protocol 6 (HT6P20B) + + +char *menuList[MAX_RECORDS_TO_STORE+1]; + +const char *menuMain[] PROGMEM = { + "LISTEN", + "LISTEN & STORE", + "LIST OF STORED", + "SAVE ALL", + "CLEAR ALL", //clears all unprotected records + NULL // the last element of every menu list should be NULL +}; + +const char *menuRecord[] PROGMEM = { + "SEND", //send selected record + "SHOW", + "SET REPEAT", + "RENAME", + "DELETE", + "CANCEL", + NULL +}; + +enum {listen_=1, listenstore_, listofstored_, saveall_, clearall_}; +enum {send_=1, show_, setrepeat_, rename_, delete_, cancel_}; + +RCSwitch mySwitch; +ESPboyInit myESPboy; +ESPboyTerminalGUI *terminalGUIobj = NULL; +ESPboyMenuGUI *menuGUIobj = NULL; +ESPboyLED myLED; + +char EEPROMmagicNo[4]={0xCC,0xCD,0xCE,0};//EEPROM marker of Sub1Ghz storage + +struct recordStored{ + char recordName[20]; + uint32_t recordRepeatno; + uint32_t recordProtocol; + uint32_t recordPulselen; + uint32_t recordBits; + uint32_t recordValue; +}; + +std::vector recordStoredVector; + +String bin2tri(uint32_t dec) { + uint8_t pos = 0; + String returnVal=""; + String bin; + + bin = String(dec,BIN); + while (pos < (bin.length()-1)) { + if (bin[pos]=='0' && bin[pos+1]=='0') returnVal += '0'; + else + if (bin[pos]=='1' && bin[pos+1]=='1') returnVal += '1'; + else + if (bin[pos]=='0' && bin[pos+1]=='1') returnVal += 'F'; + else return "NO"; + pos += 2; + } + return returnVal; +} + + +void printConsoleLocal(String str, uint32_t clr, uint8_t flag1, uint8_t flag2){ + myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); + digitalWrite(CC1101chipSelectPin, HIGH); + terminalGUIobj->printConsole(str, clr, flag1, flag2); + myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); + digitalWrite(CC1101chipSelectPin, LOW); +} + + +String getUserInputLocal(){ + String inputLocal; + myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); + digitalWrite(CC1101chipSelectPin, HIGH); + inputLocal = terminalGUIobj->getUserInput(); + myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); + digitalWrite(CC1101chipSelectPin, LOW); + return (inputLocal); +} + + + +void toggleDisplayModeLocal(uint8_t displayMode){ + myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); + digitalWrite(CC1101chipSelectPin, HIGH); + terminalGUIobj->toggleDisplayMode(displayMode); + myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); + digitalWrite(CC1101chipSelectPin, LOW); +} + + +void doScrollLocal(){ + myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); + digitalWrite(CC1101chipSelectPin, HIGH); + terminalGUIobj->doScroll(); + myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); + digitalWrite(CC1101chipSelectPin, LOW); +} + + +uint16_t menuInitLocal(const char** menuLines, uint16_t menuLineColor, uint16_t menuUnselectedLineColor, uint16_t menuSelectionColor){ + uint16_t menuItemSelected; + myESPboy.mcp.digitalWrite(TFTchipSelectPin, LOW); + digitalWrite(CC1101chipSelectPin, HIGH); + menuItemSelected = menuGUIobj->menuInit(menuLines, menuLineColor, menuUnselectedLineColor, menuSelectionColor); + myESPboy.mcp.digitalWrite(TFTchipSelectPin, HIGH); + digitalWrite(CC1101chipSelectPin, LOW); + return (menuItemSelected); +} + + +void drawDecodedSignal() { + String toPrint; + uint32_t *rawData; + uint32_t dec, lengthval, rssi, lqi; + + int16_t databuffer[64]; // get a copy of the received timings before they are overwritten + int16_t numberoftimings = 2 * mySwitch.getReceivedBitlength() + 2; + if(numberoftimings > 64) numberoftimings = 64; + for (int8_t i = 0; i < numberoftimings; i++) + databuffer[i] = mySwitch.getReceivedRawdata()[i]; + + dec = mySwitch.getReceivedValue(); + lengthval = mySwitch.getReceivedBitlength(); + rssi = ELECHOUSE_cc1101.getRssi(); + lqi = ELECHOUSE_cc1101.getLqi(); + + printConsoleLocal(" ", TFT_MAGENTA, 1, 1); + printConsoleLocal(" ", TFT_MAGENTA, 1, 0); + printConsoleLocal(F("DETECTED! DECODE:"), TFT_RED, 1, 0); + + //toPrint = "RSSI/LQI: "; + //toPrint += (String)rssi; + //toPrint += "/"; + //toPrint += (String)lqi; + //printConsoleLocal(toPrint, TFT_YELLOW, 1, 0); + + toPrint="DEC: "; + toPrint+=(String)dec; + toPrint+=" ("; + toPrint+= (String)lengthval; + toPrint+= " Bit)"; + printConsoleLocal(toPrint, TFT_GREEN, 1, 0); + + toPrint = String (dec,BIN); + while(toPrint.length() that is pin #2 + ELECHOUSE_cc1101.SetRx(); // set Receive on +} + + +void listen_f(uint8_t storeFlag){ + String str=""; + uint8_t ledFlag = 1; + uint32_t counter; + + mySwitch.resetAvailable(); + toggleDisplayModeLocal(1); + + while(!(myESPboy.getKeys()&PAD_ESC)){ + + if(myESPboy.getKeys()){ + printConsoleLocal(F("Stop"), TFT_MAGENTA, 1, 1); + printConsoleLocal(F("LGT/RGT-scroll. B-exit"), TFT_MAGENTA, 1, 0); + doScrollLocal(); + ledFlag = 1; + str=""; + } + + if (mySwitch.available()) { + myLED.setRGB(0,20,0); + ledFlag = 1; + counter=millis(); + str=""; + myESPboy.playTone(100,100); + drawDecodedSignal(); + + if (storeFlag){ + if (!mySwitch.getReceivedProtocol()){ + printConsoleLocal(F("Can't store UNKNOWN PROTOCOL"), TFT_RED, 1, 0);} + else + if(recordStoredVector.size()2000){ + myLED.setRGB(0,20,0); + counter=millis(); + printConsoleLocal(str, TFT_MAGENTA, 1, 1); + str+="."; + if (str.length()>20) str=""; + myLED.setRGB(0,0,0); + } + + if(ledFlag){ + ledFlag=0; + myLED.setRGB(0,0,0); + printConsoleLocal(F("Listening..."), TFT_MAGENTA, 1, 0); + printConsoleLocal(F(""), TFT_MAGENTA, 1, 0); + } + delay(10); + } + +printConsoleLocal(F("Stop. Exit"), TFT_MAGENTA, 1, 1); +printConsoleLocal("", TFT_MAGENTA, 1, 0); +while(myESPboy.getKeys()) delay(10); +} + + +void send_f(uint16_t selectedSignal){ + myLED.setRGB(20,0,0); + selectedSignal--; + toggleDisplayModeLocal(1); + String toPrint = F("Sending "); + toPrint += recordStoredVector[selectedSignal].recordName; + printConsoleLocal(toPrint, TFT_MAGENTA, 1, 0); + toPrint = (String)recordStoredVector[selectedSignal].recordRepeatno; + toPrint += " times"; + printConsoleLocal(toPrint, TFT_MAGENTA, 1, 0); + + ELECHOUSE_cc1101.SetTx(); // set Transmit on + mySwitch.disableReceive(); // Receiver off + mySwitch.enableTransmit(CC1101sendPin); // Transmit on + mySwitch.setRepeatTransmit(recordStoredVector[selectedSignal].recordRepeatno); // transmission repetitions. + mySwitch.setProtocol(recordStoredVector[selectedSignal].recordProtocol); // send Received Protocol + mySwitch.setPulseLength(recordStoredVector[selectedSignal].recordPulselen); // send Received Delay + mySwitch.send(recordStoredVector[selectedSignal].recordValue,recordStoredVector[selectedSignal].recordBits); // send Received value/bits + ELECHOUSE_cc1101.SetRx(); // set Receive on + mySwitch.disableTransmit(); // set Transmit off + mySwitch.enableReceive(CC1101riceivePin); // Receiver on + + printConsoleLocal(F("DONE"), TFT_MAGENTA, 1, 0); + printConsoleLocal("", TFT_MAGENTA, 1, 0); + + myLED.setRGB(0,0,0); + + while (!myESPboy.getKeys())delay(10); + while (myESPboy.getKeys())delay(10); + } + + + + +void show_f(uint16_t selectedSignal) { + String toPrint; + + selectedSignal--; + + toPrint = F("Signal: "); + toPrint+=recordStoredVector[selectedSignal].recordName; + printConsoleLocal(toPrint, TFT_GREEN, 1, 0); + + toPrint = F("Value: "); + toPrint += (String)recordStoredVector[selectedSignal].recordValue; + toPrint += (" ("); + toPrint += (String)recordStoredVector[selectedSignal].recordBits; + toPrint += (" Bit)"); + printConsoleLocal(toPrint, TFT_GREEN, 1, 0); + + toPrint = F("Repeat No: "); + toPrint += (String)recordStoredVector[selectedSignal].recordRepeatno; + printConsoleLocal(toPrint, TFT_YELLOW, 1, 0); + + toPrint = F("Pulse length: "); + toPrint += (String)recordStoredVector[selectedSignal].recordPulselen; + printConsoleLocal(toPrint, TFT_YELLOW, 1, 0); + + toPrint = F("Protocol: "); + toPrint += (String)recordStoredVector[selectedSignal].recordProtocol; + printConsoleLocal(toPrint, TFT_YELLOW, 1, 0); + printConsoleLocal(protDecode[recordStoredVector[selectedSignal].recordProtocol], TFT_WHITE, 1, 0); + + printConsoleLocal(F("DONE"), TFT_MAGENTA, 1, 0); + while (!myESPboy.getKeys())delay(10); + while (myESPboy.getKeys())delay(10); + printConsoleLocal("", TFT_MAGENTA, 1, 0); +} + + + + +void listofstored_f(){ + uint16_t selectedSignal; + uint8_t exitFlag; + uint8_t userInput; + String userInputName; + String toPrint; + +gotolabel: + + selectedSignal=0; + exitFlag=0; + userInput=0; + userInputName=""; + + for (uint16_t i=0; i DEFAULT_SIGNAL_REPEAT_NUMBER*10) userInput = DEFAULT_SIGNAL_REPEAT_NUMBER; + recordStoredVector[selectedSignal-1].recordRepeatno = userInput; + printConsoleLocal(F("DONE"), TFT_MAGENTA, 1, 0); + while (!myESPboy.getKeys())delay(10); + while (myESPboy.getKeys())delay(10); + printConsoleLocal("", TFT_MAGENTA, 1, 0); + break; + case rename_: + printConsoleLocal(F("Enter new name"), TFT_MAGENTA, 1, 0); + toPrint = F("for "); + toPrint+=recordStoredVector[selectedSignal-1].recordName; + printConsoleLocal(toPrint, TFT_MAGENTA, 1, 0); + while (!userInputName.length()) userInputName = getUserInputLocal(); + userInputName = userInputName.substring(0,18); + memcpy(&recordStoredVector[selectedSignal-1].recordName, userInputName.c_str(), userInputName.length()+1); + printConsoleLocal(F("DONE"), TFT_MAGENTA, 1, 0); + while (!myESPboy.getKeys())delay(10); + while (myESPboy.getKeys())delay(10); + printConsoleLocal("", TFT_MAGENTA, 1, 0); + break; + case delete_: + recordStoredVector.erase(recordStoredVector.begin()+(selectedSignal-1)); + while(myESPboy.getKeys()) delay(100); + if(!recordStoredVector.empty())goto gotolabel; + exitFlag=1; + break; + case cancel_: + while(myESPboy.getKeys()) delay(100); + goto gotolabel; + break; + default: + while(myESPboy.getKeys()) delay(100); + goto gotolabel; + break; + } + } + else while(myESPboy.getKeys()) delay(100); +} + + +void loop(){ + switch (menuInitLocal(menuMain, TFT_YELLOW, TFT_BLUE, TFT_BLUE)){ + case listen_: + listen_f(0); + break; + case listenstore_: + listen_f(1); + break; + case listofstored_: + if(!recordStoredVector.empty()) + listofstored_f(); + else{ + toggleDisplayModeLocal(1); + printConsoleLocal(F("Records not found"), TFT_MAGENTA, 1, 0); + while (!myESPboy.getKeys())delay(10); + while (myESPboy.getKeys())delay(10); + printConsoleLocal("", TFT_MAGENTA, 1, 0);} + break; + case saveall_: + toggleDisplayModeLocal(1); + printConsoleLocal(F("Save to EEPROM..."), TFT_MAGENTA, 1, 0); + writeEEPROMall(); + printConsoleLocal(F("DONE"), TFT_MAGENTA, 1, 0); + while (!myESPboy.getKeys())delay(10); + while (myESPboy.getKeys())delay(10); + printConsoleLocal("", TFT_MAGENTA, 1, 0); + break; + case clearall_: + toggleDisplayModeLocal(1); + printConsoleLocal(F("Clearing memory..."), TFT_MAGENTA, 1, 0); + recordStoredVector.clear(); + printConsoleLocal(F("DONE"), TFT_MAGENTA, 1, 0); + while (!myESPboy.getKeys())delay(10); + while (myESPboy.getKeys())delay(10); + printConsoleLocal("", TFT_MAGENTA, 1, 0); + break; + default: + delay(150); + break; + } +}