diff --git a/faa_osc/faa_osc.ino b/faa_osc/faa_osc.ino new file mode 100644 index 0000000..b9ad1cd --- /dev/null +++ b/faa_osc/faa_osc.ino @@ -0,0 +1,262 @@ +// +// wirelessly connected cloud (based on a LPWAN, called ESP-NOW) +// + +// +// puredata gathering @ ururu.cloud, Seoul +// + +// +// 2024 02 17 +// +// 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'. +// + +//======================== +#define MY_GROUP_ID (0) +#define MY_ID (MY_GROUP_ID + 2) +#define MY_SIGN ("POSTMAN|OSC(Pd)") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//osc +#include +#include +SLIPEncodedSerial SLIPSerial(Serial); +void swap_println(String abc) { + Serial.flush(); + + Serial.swap(); + Serial.println(abc); + Serial.flush(); + + Serial.swap(); +} + +//-*-*-*-*-*-*-*-*-*-*-*-*- +//task #1 : osc processing +void route_note(OSCMessage& msg, int offset) { + //swap_println("got route_note!"); + // matches will happen in the order. that the bundle is packed. + static Note note; + // (1) --> /onoff + if (msg.fullMatch("/onoff", offset)) { + // + note.clear(); + // + note.onoff = msg.getFloat(0); + // if (note.onoff != 0) note.onoff = 1; + } + // (2) --> /velocity + if (msg.fullMatch("/velocity", offset)) { + note.velocity = msg.getFloat(0); + } + // (3) --> /pitch + if (msg.fullMatch("/pitch", offset)) { + note.pitch = msg.getFloat(0); + } + // (4) --> /id + if (msg.fullMatch("/id", offset)) { + note.id = msg.getInt(0); + } + // (5) --> /x + if (msg.fullMatch("/x", offset)) { + note.x1 = msg.getFloat(0); + note.x2 = msg.getFloat(1); + note.x3 = msg.getFloat(2); + note.x4 = msg.getFloat(3); + note.ps = msg.getFloat(4); + // + uint8_t frm_size = sizeof(Note) + 2; + uint8_t frm[frm_size]; + frm[0] = '['; + memcpy(frm + 1, (uint8_t *) ¬e, sizeof(Note)); + frm[frm_size - 1] = ']'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // + Serial.write(frm, frm_size); + Serial.println(" ==(esp_now_send/0)==> "); + // + } +} +extern Task osc_task; +void osc() +{ + //osc + OSCBundle bundleIN; + int size; + if (SLIPSerial.available()) { + while(!SLIPSerial.endofPacket()) { + if( (size = SLIPSerial.available()) > 0) { + while(size--) { + bundleIN.fill(SLIPSerial.read()); + } + } + } + if(!bundleIN.hasError()) { + // on '/note' + bundleIN.route("/note", route_note); + } + } +} +Task osc_task(0, TASK_FOREVER, &osc, &runner, true); // -> ENABLED, at start-up. +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//task #0 : blink led +#define LED_PERIOD (11111) +#define LED_ONTIME (1) +#define LED_GAPTIME (222) +#define LED_PIN 2 +extern Task blink_task; +void blink() { + // + static int count = 0; + count++; + // + switch (count % 4) { + case 0: + digitalWrite(LED_PIN, LOW); // first ON + blink_task.delay(LED_ONTIME); + break; + case 1: + digitalWrite(LED_PIN, HIGH); // first OFF + blink_task.delay(LED_GAPTIME); + break; + case 2: + digitalWrite(LED_PIN, LOW); // second ON + blink_task.delay(LED_ONTIME); + break; + case 3: + digitalWrite(LED_PIN, HIGH); // second OFF + blink_task.delay(LED_PERIOD - 2* LED_ONTIME - LED_GAPTIME); + break; + } +} +Task blink_task(0, TASK_FOREVER, &blink, &runner, true); // -> ENABLED, at start-up. + +// on 'receive' +void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) { + + // + Serial.write(incomingData, len); + + // open => identify => use. + if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) { + Hello hello(""); + memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello)); + // + Serial.println(hello.to_string()); + // + OSCMessage osc("/hello"); + osc.add(hello.id); + osc.add(hello.h1); + osc.add(hello.h2); + osc.add(hello.h3); + osc.add(hello.h4); + // + SLIPSerial.beginPacket(); + osc.send(SLIPSerial); + SLIPSerial.endPacket(); + osc.empty(); + // + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + // + Serial.println(note.to_string()); + } +} + +// on 'sent' +void onDataSent(uint8_t *mac, uint8_t sendStatus) { + char buff[256] = ""; + sprintf(buff, "Delivery failed! -> %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + if (sendStatus != 0) Serial.println(buff); +} + +// +void setup() { + + //led + pinMode(LED_PIN, OUTPUT); + + //serial + Serial.begin(57600); + delay(100); + + //info + Serial.println(); + Serial.println(); + Serial.println("\"hi, i m your postman.\""); + 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("-"); + + //wifi - disabled + system_phy_set_max_tpw(0); + WiFiMode_t node_type = WIFI_STA; + WiFi.mode(node_type); + + //esp-now + if (esp_now_init() != 0) { + Serial.println("Error initializing ESP-NOW"); + return; + } + esp_now_set_self_role(ESP_NOW_ROLE_COMBO); + 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); + + Serial.println("-"); + Serial.println("\".-.-.-. :)\""); + Serial.println(); + + // a proper say goodbye. + Serial.println("\"bye, this 'Serial' will be occupied by SLIPSerial. find me @ Serial1 (TX only)\""); + Serial.println("-"); + Serial.println("\".-.-.-. :)\""); + delay(1000); // flush out unsent serial messages. + + // start SLIPSerial on Serial + SLIPSerial.begin(57600); + + // clear out any leftover debris. (this will let LED blinking works from initialization.) + while(SLIPSerial.available() > 0) { + SLIPSerial.read(); + } +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_osc/message.h b/faa_osc/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_osc/message.h @@ -0,0 +1,111 @@ +#pragma once + +//message type Note : '[' + Note + ']' +struct Note { + // + int32_t id; + float pitch; + float velocity; + float onoff; + float x1; + float x2; + float x3; + float x4; + float ps; + // + Note() { + id = 0; + pitch = 0; + velocity = 0; + onoff = 0; + x1 = 0; + x2 = 0; + x3 = 0; + x4 = 0; + ps = 0; + } + Note(int32_t id_, float pitch_, float velocity_, float onoff_, float x1_, float x2_, float x3_, float x4_, float ps_) + { + id = id_; + pitch = pitch_; + velocity = velocity_; + onoff = onoff_; + x1 = x1_; + x2 = x2_; + x3 = x3_; + x4 = x4_; + ps = ps_; + } + // + void clear() { + id = 0; + pitch = 0; + velocity = 0; + onoff = 0; + x1 = 0; + x2 = 0; + x3 = 0; + x4 = 0; + ps = 0; + } + // + String to_string() { + String str = ""; + str += "( id=" + String(id); + str += ", pitch=" + String(pitch); + str += ", velocity=" + String(velocity); + str += ", onoff=" + String(onoff); + str += ", x1=" + String(x1); + str += ", x2=" + String(x2); + str += ", x3=" + String(x3); + str += ", x4=" + String(x4); + str += ", ps=" + String(ps); + str += " )"; + return str; + } +}; + +//message type Hello : '{' + Hello + '}' +#define SIGNATURE_LENGTH (20) +#define SIGNATURE_BUFF_LEN (SIGNATURE_LENGTH + 1) +struct Hello { + char sign[SIGNATURE_BUFF_LEN]; + int32_t id; + uint32_t mac32; + float h1; + float h2; + float h3; + float h4; + // + Hello(String sign_, int32_t id_ = 0, uint32_t mac32_ = 0, float h1_ = 0, float h2_ = 0, float h3_ = 0, float h4_ = 0) { + id = id_; + mac32 = mac32_; + h1 = h1_; + h2 = h2_; + h3 = h3_; + h4 = h4_; + sign_.toCharArray(sign, SIGNATURE_BUFF_LEN); + } + void clear() { + id = 0; + mac32 = 0; + h1 = 0; + h2 = 0; + h3 = 0; + h4 = 0; + sign[0] = '\0'; + } + // + String to_string() { + String str = ""; + str += "( id=" + String(id); + str += ", mac32=0x" + String(mac32, HEX); + str += ", sign=\"" + String(sign) + "\""; + str += ", h1=" + String(h1); + str += ", h2=" + String(h2); + str += ", h3=" + String(h3); + str += ", h4=" + String(h4); + str += " )"; + return str; + } +}; diff --git a/faa_osc/sketch.yaml b/faa_osc/sketch.yaml new file mode 100644 index 0000000..ee52428 --- /dev/null +++ b/faa_osc/sketch.yaml @@ -0,0 +1,2 @@ +default_fqbn: esp8266:esp8266:nodemcuv2:baud=460800 +default_port: /dev/tty.SLAB_USBtoUART