From ea6b588be9f530c66207309ac0861a43b62fcb75 Mon Sep 17 00:00:00 2001 From: Dooho Yi Date: Sat, 7 May 2022 15:31:03 +0900 Subject: [PATCH] osc/ for esp8266 & makepython-esp32 + enable OSC bundle tag processing (a bug fix. endianness problem) --- osc/lib/OSC/OSCBundle.h | 77 ++++++++++++--------- osc/platformio.ini | 14 +++- osc/src/main.cpp | 145 +++++++++++++++++++++++++++++++++------- 3 files changed, 175 insertions(+), 61 deletions(-) diff --git a/osc/lib/OSC/OSCBundle.h b/osc/lib/OSC/OSCBundle.h index 4285749..beba572 100644 --- a/osc/lib/OSC/OSCBundle.h +++ b/osc/lib/OSC/OSCBundle.h @@ -2,24 +2,24 @@ Written by Yotam Mann, The Center for New Music and Audio Technologies, University of California, Berkeley. Copyright (c) 2012, 2013, The Regents of the University of California (Regents). - + Permission to use, copy, modify, distribute, and distribute modified versions of this software and its documentation without fee and without a signed licensing agreement, is hereby granted, provided that the above copyright notice, this paragraph and the following two paragraphs appear in all copies, modifications, and distributions. - + IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - + REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. - + */ @@ -43,16 +43,16 @@ private: //the number of messages in the array int numMessages; - - osctime_t timetag; - + + // osctime_t timetag; + //error codes OSCErrorCode error; - + /*============================================================================= DECODING INCOMING BYTES =============================================================================*/ - + //the decoding states for incoming bytes enum DecodeState { STANDBY, @@ -61,35 +61,36 @@ private: MESSAGE_SIZE, MESSAGE, } decodeState; - + //stores incoming bytes until they can be decoded uint8_t * incomingBuffer; int incomingBufferSize; - + //the size of the incoming message int incomingMessageSize; - + //adds a byte to the buffer void addToIncomingBuffer(uint8_t); //clears the incoming buffer void clearIncomingBuffer(); - + //decoding functions void decode(uint8_t); void decodeTimetag(); void decodeHeader(); void decodeMessage(uint8_t); - + //just a placeholder while filling OSCMessage & add(); public: + osctime_t timetag; /*============================================================================= CONSTRUCTORS / DESTRUCTOR =============================================================================*/ - + //default timetag of OSCBundle(osctime_t = zerotime); @@ -98,17 +99,17 @@ public: //clears all of the OSCMessages inside OSCBundle& empty(); - + /*============================================================================= SETTERS =============================================================================*/ - + //start a new OSC Message in the bundle OSCMessage & add(const char * address); //add with nothing in it produces an invalid osc message //copies an existing message into the bundle OSCMessage & add(OSCMessage & msg); - + template OSCBundle& setTimetag(T t){ timetag = (osctime_t) t; @@ -116,10 +117,19 @@ public: } //sets the timetag from a buffer OSCBundle& setTimetag(uint8_t * buff){ - memcpy(&timetag, buff, 8); + uint8_t bb[8]; + bb[0] = buff[3]; + bb[1] = buff[2]; + bb[2] = buff[1]; + bb[3] = buff[0]; + bb[4] = buff[7]; + bb[5] = buff[6]; + bb[6] = buff[5]; + bb[7] = buff[4]; + memcpy(&timetag, bb, 8); return *this; } - + /*============================================================================= GETTERS =============================================================================*/ @@ -127,48 +137,49 @@ public: //gets the message the matches the address string //will do regex matching OSCMessage * getOSCMessage(char * addr); - + //get message by position OSCMessage * getOSCMessage(int position); - + + /*============================================================================= MATCHING =============================================================================*/ - //if the bundle contains a message that matches the pattern, + //if the bundle contains a message that matches the pattern, //call the function callback on that message bool dispatch(const char * pattern, void (*callback)(OSCMessage&), int = 0); - + //like dispatch, but allows for partial matches //the address match offset is sent as an argument to the callback bool route(const char * pattern, void (*callback)(OSCMessage&, int), int = 0); - + /*============================================================================= SIZE =============================================================================*/ //returns the number of messages in the bundle; int size(); - + /*============================================================================= ERROR =============================================================================*/ - + bool hasError(); - + OSCErrorCode getError(); - + /*============================================================================= SENDING =============================================================================*/ - + OSCBundle& send(Print &p); - + /*============================================================================= FILLING =============================================================================*/ - + OSCBundle& fill(uint8_t incomingByte); - + OSCBundle& fill(const uint8_t * incomingBytes, int length); }; diff --git a/osc/platformio.ini b/osc/platformio.ini index cef6958..ce8fa29 100644 --- a/osc/platformio.ini +++ b/osc/platformio.ini @@ -14,8 +14,8 @@ default_envs = d1_mini_pro [env] framework = arduino upload_port = /dev/ttyUSB0 -lib_deps = - 721 +lib_deps = + arkhipenko/TaskScheduler@^3.3.0 [env:nodemcuv2] platform = espressif8266 @@ -28,3 +28,13 @@ platform = espressif8266 board = d1_mini_pro lib_deps = ${env.lib_deps} upload_speed = 460800 + +[env:makepython-esp32] +platform = espressif32 +board = esp32dev +lib_deps = + ${env.lib_deps} + adafruit/Adafruit SSD1306@^2.4.5 + adafruit/Adafruit BusIO@^1.7.3 +upload_speed = 921600 +lib_ldf_mode=deep diff --git a/osc/src/main.cpp b/osc/src/main.cpp index d85867b..33df910 100644 --- a/osc/src/main.cpp +++ b/osc/src/main.cpp @@ -1,30 +1,13 @@ // -// wirelessly connected cloud (based on ESP-NOW, a kind of LPWAN?) +// wirelessly connected cloud (based on ESP-NOW, a kind of LPWAN) // -// -// Conversation about the ROOT @ SEMA storage, Seoul -// - -// -// 2021 02 15 -// -// (part-1) esp8266 : 'osc node on esp8266' (the esp-now network nodes) -// -// this module will be an esp-now node in a group. -// like, a bird in a group of birds. -// -// esp-now @ esp8266 w/ broadcast address (FF:FF:FF:FF:FF:FF) -// always broadcasting. everyone is 'talkative'. -// - -// then, let it save a value in EEPROM (object with memory=mind?) - //======================== // -#define MY_GROUP_ID (0) -#define MY_ID (MY_GROUP_ID + 2) -#define MY_SIGN ("POSTMAN|OSC(Pd)") +#define MY_GROUP_ID (0) +#define MY_ID (MY_GROUP_ID + 2) +#define MY_SIGN ("POSTMAN|OSC(Pd)") +#define ADDRESSBOOK_TITLE ("broadcast only") // //======================== @@ -47,6 +30,10 @@ // 'HAVE_CLIENT_I2C' // --> i have a client w/ I2C i/f. enable the I2C client task. // +// 'ADDRESSBOOK_TITLE' +// --> peer list limited max. 20. +// so, we might use different address books for each node to cover a network of more than 20 nodes. +// //==================== // #define SLIPSERIAL_ACTIVE @@ -57,6 +44,8 @@ #define LED_ONTIME (1) #define LED_GAPTIME (222) // +#define SCREEN_PERIOD (200) //200ms = 5hz +// #define WIFI_CHANNEL 1 // // 'MONITORING_SERIAL' @@ -77,6 +66,8 @@ //======================== #if defined(ARDUINO_FEATHER_ESP32) // featheresp32 #define LED_PIN 13 +#elif defined(ARDUINO_ESP32_DEV) // esp32dev (MakePython ESP32 => Have NO LED) +#define LED_PIN 2 #else #define LED_PIN 2 #endif @@ -89,8 +80,14 @@ #include "../../post.h" //espnow +#if defined(ESP8266) // for esp8266 API #include #include +#elif defined(ESP32) // for esp32 API +#include +#include +#endif +AddressLibrary lib; //task #include @@ -101,6 +98,8 @@ Scheduler runner; #include SLIPEncodedSerial SLIPSerial(Serial); void swap_println(String abc) { +#if defined(ESP8266) // for esp8266 API + Serial.flush(); Serial.swap(); @@ -108,8 +107,46 @@ void swap_println(String abc) { Serial.flush(); Serial.swap(); +#endif } +#if defined(ESP32) // for esp32 API +//screen +#include +#include +#define MAKEPYTHON_ESP32_SDA 4 +#define MAKEPYTHON_ESP32_SCL 5 +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 64 // OLED display height, in pixels +#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +//-*-*-*-*-*-*-*-*-*-*-*-*- +// +//screen task +String screen_text = "(........................................."; +extern Task screen_task; +void screen() { + //clear screen + a + int line_step = 12; + int line = 0; + display.clearDisplay(); + display.setTextColor(SSD1306_WHITE); + display.setTextSize(1); + + //line1 - debug line + display.setCursor(0, line); + display.println(screen_text.c_str()); + line += line_step; + + // + display.display(); + // +} +Task screen_task(SCREEN_PERIOD, TASK_FOREVER, &screen, &runner, true); +#endif +//*-*-*-*-*-*-*-*-*-*-*-*-* + // extern Task hello_task; static int hello_delay = 0; @@ -237,13 +274,22 @@ void osc() if(!bundleIN.hasError()) { // on '/note' bundleIN.route("/note", route_note); +#if defined(ESP32) + static int a = 0; + screen_text = String(a) + " => \n" + String(bundleIN.timetag.seconds-2208988800UL) + "\n" + String(bundleIN.timetag.fractionofseconds); + a++; +#endif } } } Task osc_task(0, TASK_FOREVER, &osc, &runner, true); // -> ENABLED, at start-up. // on 'receive' +#if defined(ESP32) +void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int32_t len) { +#elif defined(ESP8266) void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) { +#endif // //MONITORING_SERIAL.write(incomingData, len); @@ -286,10 +332,27 @@ void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) { } // on 'sent' +#if defined(ESP32) +void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sendStatus) { +#elif defined(ESP8266) void onDataSent(uint8_t *mac_addr, uint8_t sendStatus) { - if (sendStatus != 0) MONITORING_SERIAL.println("Delivery failed!"); +#endif + char buff[256] = ""; + sprintf(buff, "Delivery failed! -> %02X:%02X:%02X:%02X:%02X:%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + if (sendStatus != 0) MONITORING_SERIAL.println(buff); } +#if defined(ESP32) +void lcd_text(String str) { + display.clearDisplay(); + display.setTextColor(SSD1306_WHITE); + display.setTextSize(1); + display.setCursor(0, 0); + display.println(str.c_str()); + display.display(); +} +#endif + // void setup() { @@ -300,6 +363,18 @@ void setup() { Serial.begin(57600); delay(100); +#if defined(ESP32) + //screen + Wire.begin(MAKEPYTHON_ESP32_SDA, MAKEPYTHON_ESP32_SCL); + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally + if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 + Serial.println(F("SSD1306 allocation failed")); + for (;;) + ; // Don't proceed, loop forever + } + display.clearDisplay(); +#endif + //info Serial.println(); Serial.println(); @@ -307,6 +382,7 @@ void setup() { Serial.println("-"); Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\""); Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL)); + Serial.println("- my peer book ==> \"" + String(ADDRESSBOOK_TITLE) + "\""); #if defined(HAVE_CLIENT) Serial.println("- ======== 'HAVE_CLIENT' ========"); #endif @@ -337,14 +413,31 @@ void setup() { Serial.println("Error initializing ESP-NOW"); return; } +#if defined(ESP8266) esp_now_set_self_role(ESP_NOW_ROLE_COMBO); +#endif esp_now_register_send_cb(onDataSent); esp_now_register_recv_cb(onDataReceive); // - Serial.println("- ! (esp_now_add_peer) ==> add a 'broadcast peer' (FF:FF:FF:FF:FF:FF)."); - uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - esp_now_add_peer(broadcastmac, ESP_NOW_ROLE_COMBO, 1, NULL, 0); + AddressBook * book = lib.getBookByTitle(ADDRESSBOOK_TITLE); + if (book == NULL) { + Serial.println("- ! wrong book !! : \"" + String(ADDRESSBOOK_TITLE) + "\""); while(1); + } + for (int idx = 0; idx < book->list.size(); idx++) { + Serial.println("- ! (esp_now_add_peer) ==> add a '" + book->list[idx].name + "'."); +#if defined(ESP32) + esp_now_peer_info_t peerInfo = {}; + memcpy(peerInfo.peer_addr, book->list[idx].mac, 6); + peerInfo.channel = 0; + peerInfo.encrypt = false; + esp_now_add_peer(&peerInfo); +#else + esp_now_add_peer(book->list[idx].mac, ESP_NOW_ROLE_COMBO, 1, NULL, 0); +#endif + } + // (DEBUG) fetch full peer list + { PeerLister a; a.print(); } // Serial.println("-"); Serial.println("\".-.-.-. :)\"");