osc/ for esp8266 & makepython-esp32

+ enable OSC bundle tag processing (a bug fix. endianness problem)
This commit is contained in:
Dooho Yi 2022-05-07 15:31:03 +09:00
parent 345ec0925e
commit ea6b588be9
3 changed files with 175 additions and 61 deletions

View file

@ -2,24 +2,24 @@
Written by Yotam Mann, The Center for New Music and Audio Technologies, Written by Yotam Mann, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2012, 2013, The Regents of University of California, Berkeley. Copyright (c) 2012, 2013, The Regents of
the University of California (Regents). the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies, notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions. modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/ */
@ -43,16 +43,16 @@ private:
//the number of messages in the array //the number of messages in the array
int numMessages; int numMessages;
osctime_t timetag; // osctime_t timetag;
//error codes //error codes
OSCErrorCode error; OSCErrorCode error;
/*============================================================================= /*=============================================================================
DECODING INCOMING BYTES DECODING INCOMING BYTES
=============================================================================*/ =============================================================================*/
//the decoding states for incoming bytes //the decoding states for incoming bytes
enum DecodeState { enum DecodeState {
STANDBY, STANDBY,
@ -61,35 +61,36 @@ private:
MESSAGE_SIZE, MESSAGE_SIZE,
MESSAGE, MESSAGE,
} decodeState; } decodeState;
//stores incoming bytes until they can be decoded //stores incoming bytes until they can be decoded
uint8_t * incomingBuffer; uint8_t * incomingBuffer;
int incomingBufferSize; int incomingBufferSize;
//the size of the incoming message //the size of the incoming message
int incomingMessageSize; int incomingMessageSize;
//adds a byte to the buffer //adds a byte to the buffer
void addToIncomingBuffer(uint8_t); void addToIncomingBuffer(uint8_t);
//clears the incoming buffer //clears the incoming buffer
void clearIncomingBuffer(); void clearIncomingBuffer();
//decoding functions //decoding functions
void decode(uint8_t); void decode(uint8_t);
void decodeTimetag(); void decodeTimetag();
void decodeHeader(); void decodeHeader();
void decodeMessage(uint8_t); void decodeMessage(uint8_t);
//just a placeholder while filling //just a placeholder while filling
OSCMessage & add(); OSCMessage & add();
public: public:
osctime_t timetag;
/*============================================================================= /*=============================================================================
CONSTRUCTORS / DESTRUCTOR CONSTRUCTORS / DESTRUCTOR
=============================================================================*/ =============================================================================*/
//default timetag of //default timetag of
OSCBundle(osctime_t = zerotime); OSCBundle(osctime_t = zerotime);
@ -98,17 +99,17 @@ public:
//clears all of the OSCMessages inside //clears all of the OSCMessages inside
OSCBundle& empty(); OSCBundle& empty();
/*============================================================================= /*=============================================================================
SETTERS SETTERS
=============================================================================*/ =============================================================================*/
//start a new OSC Message in the bundle //start a new OSC Message in the bundle
OSCMessage & add(const char * address); OSCMessage & add(const char * address);
//add with nothing in it produces an invalid osc message //add with nothing in it produces an invalid osc message
//copies an existing message into the bundle //copies an existing message into the bundle
OSCMessage & add(OSCMessage & msg); OSCMessage & add(OSCMessage & msg);
template <typename T> template <typename T>
OSCBundle& setTimetag(T t){ OSCBundle& setTimetag(T t){
timetag = (osctime_t) t; timetag = (osctime_t) t;
@ -116,10 +117,19 @@ public:
} }
//sets the timetag from a buffer //sets the timetag from a buffer
OSCBundle& setTimetag(uint8_t * buff){ 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; return *this;
} }
/*============================================================================= /*=============================================================================
GETTERS GETTERS
=============================================================================*/ =============================================================================*/
@ -127,48 +137,49 @@ public:
//gets the message the matches the address string //gets the message the matches the address string
//will do regex matching //will do regex matching
OSCMessage * getOSCMessage(char * addr); OSCMessage * getOSCMessage(char * addr);
//get message by position //get message by position
OSCMessage * getOSCMessage(int position); OSCMessage * getOSCMessage(int position);
/*============================================================================= /*=============================================================================
MATCHING 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 //call the function callback on that message
bool dispatch(const char * pattern, void (*callback)(OSCMessage&), int = 0); bool dispatch(const char * pattern, void (*callback)(OSCMessage&), int = 0);
//like dispatch, but allows for partial matches //like dispatch, but allows for partial matches
//the address match offset is sent as an argument to the callback //the address match offset is sent as an argument to the callback
bool route(const char * pattern, void (*callback)(OSCMessage&, int), int = 0); bool route(const char * pattern, void (*callback)(OSCMessage&, int), int = 0);
/*============================================================================= /*=============================================================================
SIZE SIZE
=============================================================================*/ =============================================================================*/
//returns the number of messages in the bundle; //returns the number of messages in the bundle;
int size(); int size();
/*============================================================================= /*=============================================================================
ERROR ERROR
=============================================================================*/ =============================================================================*/
bool hasError(); bool hasError();
OSCErrorCode getError(); OSCErrorCode getError();
/*============================================================================= /*=============================================================================
SENDING SENDING
=============================================================================*/ =============================================================================*/
OSCBundle& send(Print &p); OSCBundle& send(Print &p);
/*============================================================================= /*=============================================================================
FILLING FILLING
=============================================================================*/ =============================================================================*/
OSCBundle& fill(uint8_t incomingByte); OSCBundle& fill(uint8_t incomingByte);
OSCBundle& fill(const uint8_t * incomingBytes, int length); OSCBundle& fill(const uint8_t * incomingBytes, int length);
}; };

View file

@ -14,8 +14,8 @@ default_envs = d1_mini_pro
[env] [env]
framework = arduino framework = arduino
upload_port = /dev/ttyUSB0 upload_port = /dev/ttyUSB0
lib_deps = lib_deps =
721 arkhipenko/TaskScheduler@^3.3.0
[env:nodemcuv2] [env:nodemcuv2]
platform = espressif8266 platform = espressif8266
@ -28,3 +28,13 @@ platform = espressif8266
board = d1_mini_pro board = d1_mini_pro
lib_deps = ${env.lib_deps} lib_deps = ${env.lib_deps}
upload_speed = 460800 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

View file

@ -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?)
//============<identities>============ //============<identities>============
// //
#define MY_GROUP_ID (0) #define MY_GROUP_ID (0)
#define MY_ID (MY_GROUP_ID + 2) #define MY_ID (MY_GROUP_ID + 2)
#define MY_SIGN ("POSTMAN|OSC(Pd)") #define MY_SIGN ("POSTMAN|OSC(Pd)")
#define ADDRESSBOOK_TITLE ("broadcast only")
// //
//============</identities>============ //============</identities>============
@ -47,6 +30,10 @@
// 'HAVE_CLIENT_I2C' // 'HAVE_CLIENT_I2C'
// --> i have a client w/ I2C i/f. enable the I2C client task. // --> 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.
//
//==========</list-of-configurations>========== //==========</list-of-configurations>==========
// //
#define SLIPSERIAL_ACTIVE #define SLIPSERIAL_ACTIVE
@ -57,6 +44,8 @@
#define LED_ONTIME (1) #define LED_ONTIME (1)
#define LED_GAPTIME (222) #define LED_GAPTIME (222)
// //
#define SCREEN_PERIOD (200) //200ms = 5hz
//
#define WIFI_CHANNEL 1 #define WIFI_CHANNEL 1
// //
// 'MONITORING_SERIAL' // 'MONITORING_SERIAL'
@ -77,6 +66,8 @@
//============<board-specifics>============ //============<board-specifics>============
#if defined(ARDUINO_FEATHER_ESP32) // featheresp32 #if defined(ARDUINO_FEATHER_ESP32) // featheresp32
#define LED_PIN 13 #define LED_PIN 13
#elif defined(ARDUINO_ESP32_DEV) // esp32dev (MakePython ESP32 => Have NO LED)
#define LED_PIN 2
#else #else
#define LED_PIN 2 #define LED_PIN 2
#endif #endif
@ -89,8 +80,14 @@
#include "../../post.h" #include "../../post.h"
//espnow //espnow
#if defined(ESP8266) // for esp8266 API
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <espnow.h> #include <espnow.h>
#elif defined(ESP32) // for esp32 API
#include <WiFi.h>
#include <esp_now.h>
#endif
AddressLibrary lib;
//task //task
#include <TaskScheduler.h> #include <TaskScheduler.h>
@ -101,6 +98,8 @@ Scheduler runner;
#include <SLIPEncodedSerial.h> #include <SLIPEncodedSerial.h>
SLIPEncodedSerial SLIPSerial(Serial); SLIPEncodedSerial SLIPSerial(Serial);
void swap_println(String abc) { void swap_println(String abc) {
#if defined(ESP8266) // for esp8266 API
Serial.flush(); Serial.flush();
Serial.swap(); Serial.swap();
@ -108,8 +107,46 @@ void swap_println(String abc) {
Serial.flush(); Serial.flush();
Serial.swap(); Serial.swap();
#endif
} }
#if defined(ESP32) // for esp32 API
//screen
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#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; extern Task hello_task;
static int hello_delay = 0; static int hello_delay = 0;
@ -237,13 +274,22 @@ void osc()
if(!bundleIN.hasError()) { if(!bundleIN.hasError()) {
// on '/note' // on '/note'
bundleIN.route("/note", route_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. Task osc_task(0, TASK_FOREVER, &osc, &runner, true); // -> ENABLED, at start-up.
// on 'receive' // 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) { void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
#endif
// //
//MONITORING_SERIAL.write(incomingData, len); //MONITORING_SERIAL.write(incomingData, len);
@ -286,10 +332,27 @@ void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
} }
// on 'sent' // 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) { 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() { void setup() {
@ -300,6 +363,18 @@ void setup() {
Serial.begin(57600); Serial.begin(57600);
delay(100); 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 //info
Serial.println(); Serial.println();
Serial.println(); Serial.println();
@ -307,6 +382,7 @@ void setup() {
Serial.println("-"); Serial.println("-");
Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\""); 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("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL));
Serial.println("- my peer book ==> \"" + String(ADDRESSBOOK_TITLE) + "\"");
#if defined(HAVE_CLIENT) #if defined(HAVE_CLIENT)
Serial.println("- ======== 'HAVE_CLIENT' ========"); Serial.println("- ======== 'HAVE_CLIENT' ========");
#endif #endif
@ -337,14 +413,31 @@ void setup() {
Serial.println("Error initializing ESP-NOW"); Serial.println("Error initializing ESP-NOW");
return; return;
} }
#if defined(ESP8266)
esp_now_set_self_role(ESP_NOW_ROLE_COMBO); esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
#endif
esp_now_register_send_cb(onDataSent); esp_now_register_send_cb(onDataSent);
esp_now_register_recv_cb(onDataReceive); 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("-");
Serial.println("\".-.-.-. :)\""); Serial.println("\".-.-.-. :)\"");