commit 9dd58c93abf5b79b79bb6b4d5d825bfe4ced3cdc Author: Dooho Yi Date: Thu Aug 22 15:55:57 2024 +0900 init diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..8df925a Binary files /dev/null and b/.DS_Store differ diff --git a/faa_button/faa_button.ino b/faa_button/faa_button.ino new file mode 100644 index 0000000..141afc9 --- /dev/null +++ b/faa_button/faa_button.ino @@ -0,0 +1,210 @@ +// +// 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 (2000) +#define MY_ID (MY_GROUP_ID + 1) +#define MY_SIGN ("BUTTON") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +int button_pin = D5; +bool button_pullup = false; +void button_read() { + // + if (button_pullup) pinMode(button_pin, INPUT_PULLUP); + else pinMode(button_pin, INPUT); + // + static int last = 0; + int val = digitalRead(button_pin); + if (val != last) { + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = val; + // hello.h3 = 0; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // + last = val; + } +} +Task button_read_task(1, TASK_FOREVER, &button_read, &runner, true); // every 1 ms + +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + if (n.velocity < 0) n.velocity = 0; + // + if (n.pitch < 0) n.pitch = 0; + button_pin = n.pitch; + //button_pullup + if (n.onoff == 1) { + button_pullup = true; + } else { + button_pullup = false; + } + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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(115200); + 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(); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_button/message.h b/faa_button/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_button/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_input/faa_input.ino b/faa_input/faa_input.ino new file mode 100644 index 0000000..a960dbf --- /dev/null +++ b/faa_input/faa_input.ino @@ -0,0 +1,263 @@ +// +// 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 (6000) +#define MY_ID (MY_GROUP_ID + 601) +#define MY_SIGN ("INP") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +void port_read() { + // + byte port = 0; + static byte port_prev = 0; + port = 0x00; + port |= (digitalRead(D1) ? 0x01 : 0x00); + port |= (digitalRead(D2) ? 0x02 : 0x00); + port |= (digitalRead(D3) ? 0x04 : 0x00); // by design, PULLED-UP, being used for the FLASH button. + port |= (0 ? 0x08 : 0x00); // LED pin, skip + port |= (digitalRead(D5) ? 0x10 : 0x00); + port |= (digitalRead(D6) ? 0x20 : 0x00); + port |= (digitalRead(D7) ? 0x40 : 0x00); + port |= (digitalRead(D8) ? 0x80 : 0x00); + if (port_prev != port) { + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 1; // 1:digital + hello.h3 = port; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // + port_prev = port; + } +} +Task port_read_task(1, TASK_FOREVER, &port_read, &runner, true); // every 1 ms +// +void volume_read() { + // + int val = analogRead(A0); + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 2; //2: analog + hello.h3 = val; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // +} +Task volume_read_task(50, TASK_FOREVER, &volume_read, &runner, false); // every 50 ms +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + if (n.velocity < 0) n.velocity = 0; + if (n.pitch < 0) n.pitch = 0; + // + // volume: task control + if (n.velocity == 0) { + //schedule 1 read + volume_read_task.disable(); + volume_read_task.setIterations(1); + volume_read_task.restart(); + } else { + // limiting max. speed. + if (n.velocity < 20) n.velocity = 20; + volume_read_task.setIterations(TASK_FOREVER); + volume_read_task.setInterval(n.velocity); + volume_read_task.restart(); + } + //button: pullup on/off + if (n.onoff == 1) { + pinMode(n.pitch, INPUT_PULLUP); + } else { + pinMode(n.pitch, INPUT); + } + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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); + + //input + pinMode(D1, INPUT); + pinMode(D2, INPUT); + pinMode(D3, INPUT); + // + pinMode(D5, INPUT); + pinMode(D6, INPUT); + pinMode(D7, INPUT); + pinMode(D8, INPUT); + + //serial + Serial.begin(115200); + 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(); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_input/message.h b/faa_input/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_input/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_input/sketch.yaml b/faa_input/sketch.yaml new file mode 100644 index 0000000..ee52428 --- /dev/null +++ b/faa_input/sketch.yaml @@ -0,0 +1,2 @@ +default_fqbn: esp8266:esp8266:nodemcuv2:baud=460800 +default_port: /dev/tty.SLAB_USBtoUART diff --git a/faa_piano/faa_piano.ino b/faa_piano/faa_piano.ino new file mode 100644 index 0000000..4a42fc8 --- /dev/null +++ b/faa_piano/faa_piano.ino @@ -0,0 +1,263 @@ +// +// 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 (7000) +#define MY_ID (MY_GROUP_ID + 101) +#define MY_SIGN ("PIANO") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +void port_read() { + // + byte port = 0; + static byte port_prev = 0; + port = 0x00; + port |= (digitalRead(D1) ? 0x01 : 0x00); + port |= (digitalRead(D2) ? 0x02 : 0x00); + port |= (digitalRead(D3) ? 0x04 : 0x00); // by design, PULLED-UP, being used for the FLASH button. + port |= (0 ? 0x08 : 0x00); // LED pin, skip + port |= (digitalRead(D5) ? 0x10 : 0x00); + port |= (digitalRead(D6) ? 0x20 : 0x00); + port |= (digitalRead(D7) ? 0x40 : 0x00); + port |= (digitalRead(D8) ? 0x80 : 0x00); + if (port_prev != port) { + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 1; // 1:digital + hello.h3 = port; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // + port_prev = port; + } +} +Task port_read_task(1, TASK_FOREVER, &port_read, &runner, true); // every 1 ms +// +void volume_read() { + // + int val = analogRead(A0); + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 2; //2: analog + hello.h3 = val; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // +} +Task volume_read_task(50, TASK_FOREVER, &volume_read, &runner, false); // every 50 ms +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + if (n.velocity < 0) n.velocity = 0; + if (n.pitch < 0) n.pitch = 0; + // + // volume: task control + if (n.velocity == 0) { + //schedule 1 read + volume_read_task.disable(); + volume_read_task.setIterations(1); + volume_read_task.restart(); + } else { + // limiting max. speed. + if (n.velocity < 20) n.velocity = 20; + volume_read_task.setIterations(TASK_FOREVER); + volume_read_task.setInterval(n.velocity); + volume_read_task.restart(); + } + //button: pullup on/off + if (n.onoff == 1) { + pinMode(n.pitch, INPUT_PULLUP); + } else { + pinMode(n.pitch, INPUT); + } + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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); + + //input + pinMode(D1, INPUT); + pinMode(D2, INPUT); + pinMode(D3, INPUT); + // + pinMode(D5, INPUT); + pinMode(D6, INPUT); + pinMode(D7, INPUT); + pinMode(D8, INPUT); + + //serial + Serial.begin(115200); + 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(); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_piano/message.h b/faa_piano/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_piano/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_remix/faa_remix.ino b/faa_remix/faa_remix.ino new file mode 100644 index 0000000..fca8db3 --- /dev/null +++ b/faa_remix/faa_remix.ino @@ -0,0 +1,283 @@ +// +// 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 (6000) +#define MY_ID (MY_GROUP_ID + 601) +#define MY_SIGN ("REMIX") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +// my tasks + +// buzzer a.k.a. 'tone' +int tonepin = D6; +int tonefreq = 0; +void set_tone() { + tone(tonepin, tonefreq); + Serial.print("set_tone:"); + Serial.println(tonefreq); +} +Task set_tone_task(0, TASK_ONCE, &set_tone, &runner, false); + +// digital read a.k.a. 'port_read' +void port_read() { + // + byte port = 0; + static byte port_prev = 0; + port = 0x00; + port |= (digitalRead(D1) ? 0x01 : 0x00); + port |= (digitalRead(D2) ? 0x02 : 0x00); + port |= (digitalRead(D3) ? 0x04 : 0x00); // by design, PULLED-UP, being used for the FLASH button. + port |= (0 ? 0x08 : 0x00); // LED pin, skip + port |= (digitalRead(D5) ? 0x10 : 0x00); + port |= (digitalRead(D6) ? 0x20 : 0x00); + port |= (digitalRead(D7) ? 0x40 : 0x00); + port |= (digitalRead(D8) ? 0x80 : 0x00); + if (port_prev != port) { + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 1; // 1:digital + hello.h3 = port; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // + port_prev = port; + } +} +Task port_read_task(1, TASK_FOREVER, &port_read, &runner, false); // every 1 ms + +// analog read a.k.a. 'volume' +void volume_read() { + // + int val = analogRead(A0); + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 2; //2: analog + hello.h3 = val; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // +} +Task volume_read_task(50, TASK_FOREVER, &volume_read, &runner, false); // every 50 ms +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + // tone: buzz output + if (n.pitch < 0) n.pitch = 0; + tonefreq = n.pitch; + set_tone_task.restartDelayed(10); + // + // volume: task control + if (n.velocity < 0) n.velocity = 0; + if (n.velocity == 0) { + //schedule 1 read + volume_read_task.disable(); + volume_read_task.setIterations(1); + volume_read_task.restart(); + } else { + // limiting max. speed. + if (n.velocity < 20) n.velocity = 20; + volume_read_task.setIterations(TASK_FOREVER); + volume_read_task.setInterval(n.velocity); + volume_read_task.restart(); + } + // //button: pullup on/off + // if (n.onoff == 1) { + // pinMode(n.pitch, INPUT_PULLUP); + // } else { + // pinMode(n.pitch, INPUT); + // } + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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); + + //input (== 'port_read' preset) + pinMode(D1, INPUT); + pinMode(D2, INPUT); + pinMode(D3, INPUT); + // + pinMode(D5, INPUT); + pinMode(D6, INPUT); + pinMode(D7, INPUT); + pinMode(D8, INPUT); + + //pwm freq. + analogWriteFreq(40000); + + //serial + Serial.begin(115200); + 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(); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_remix/message.h b/faa_remix/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_remix/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_remix/sketch.yaml b/faa_remix/sketch.yaml new file mode 100644 index 0000000..ee52428 --- /dev/null +++ b/faa_remix/sketch.yaml @@ -0,0 +1,2 @@ +default_fqbn: esp8266:esp8266:nodemcuv2:baud=460800 +default_port: /dev/tty.SLAB_USBtoUART diff --git a/faa_roller/faa_roller.ino b/faa_roller/faa_roller.ino new file mode 100644 index 0000000..c792068 --- /dev/null +++ b/faa_roller/faa_roller.ino @@ -0,0 +1,283 @@ +// +// 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 (4000) +#define MY_ID (MY_GROUP_ID + 1) +#define MY_SIGN ("ROLLER2") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +// servo +#define MOTOR_1A (D6) +#define MOTOR_1B (D5) +// my tasks +int speed = 0; +bool isactive = false; +void set_speed() { + int r = speed; + // + if (r >= 0) { + digitalWrite(MOTOR_1A, LOW); + analogWrite(MOTOR_1B, r); + } else { + digitalWrite(MOTOR_1B, LOW); + analogWrite(MOTOR_1A, r*(-1)); + } + Serial.print("set_speed:"); + Serial.println(r); + isactive = true; +} +Task set_speed_task(0, TASK_ONCE, &set_speed, &runner, false); +// +void rest() { + analogWrite(MOTOR_1A, LOW); + analogWrite(MOTOR_1B, LOW); + isactive = false; +} +Task rest_task(0, TASK_ONCE, &rest, &runner, false); +// +uint8_t watch_counter = 0; +void watcher() { + if (isactive) { + if (watch_counter > 3) { + rest_task.restartDelayed(10); + watch_counter = 0; + } else { + watch_counter++; + } + } +} +Task watcher_task(1000, TASK_FOREVER, &watcher, &runner, true); +// +#define MOTOR_2A (D3) +#define MOTOR_2B (D2) +// my tasks +int speed2 = 0; +bool isactive2 = false; +void set_speed2() { + int r = speed2; + // + if (r >= 0) { + digitalWrite(MOTOR_2A, LOW); + analogWrite(MOTOR_2B, r); + } else { + digitalWrite(MOTOR_2B, LOW); + analogWrite(MOTOR_2A, r*(-1)); + } + Serial.print("set_speed2:"); + Serial.println(r); + isactive2 = true; +} +Task set_speed2_task(0, TASK_ONCE, &set_speed2, &runner, false); +// +void rest2() { + analogWrite(MOTOR_2A, LOW); + analogWrite(MOTOR_2B, LOW); + isactive2 = false; +} +Task rest2_task(0, TASK_ONCE, &rest2, &runner, false); +// +uint8_t watch2_counter = 0; +void watcher2() { + if (isactive2) { + if (watch2_counter > 3) { + rest2_task.restartDelayed(10); + watch2_counter = 0; + } else { + watch2_counter++; + } + } +} +Task watcher2_task(1000, TASK_FOREVER, &watcher2, &runner, true); +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + if (n.pitch == 0) { + speed = n.velocity; + // + if (n.onoff == 1) { + set_speed_task.restartDelayed(10); + watch_counter = 0; + } else if (n.onoff == 0) { + rest_task.restartDelayed(10); + } else if (n.onoff == 2) { + set_speed_task.restartDelayed(10); + rest_task.restartDelayed(10 + n.x1); + } + } + // + else if (n.pitch == 1) { + speed2 = n.velocity; + // + if (n.onoff == 1) { + set_speed2_task.restartDelayed(10); + watch2_counter = 0; + } else if (n.onoff == 0) { + rest2_task.restartDelayed(10); + } else if (n.onoff == 2) { + set_speed2_task.restartDelayed(10); + rest2_task.restartDelayed(10 + n.x1); + } + } + // + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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); + + //pwm freq. + analogWriteFreq(40000); + + //serial + Serial.begin(115200); + 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(); + + //random seed + randomSeed(analogRead(0)); + + //tasks + rest_task.restartDelayed(500); + rest2_task.restartDelayed(500); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_roller/message.h b/faa_roller/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_roller/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_taak/faa_taak.ino b/faa_taak/faa_taak.ino new file mode 100644 index 0000000..48b8144 --- /dev/null +++ b/faa_taak/faa_taak.ino @@ -0,0 +1,194 @@ +// +// 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 (1000) +#define MY_ID (MY_GROUP_ID + 1) +#define MY_SIGN ("TAAK") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +int taak_pin = D6; +void taak_on() { + pinMode(taak_pin, OUTPUT); + Serial.println("taak_on!"); + digitalWrite(taak_pin, HIGH); +} +Task taak_on_task(0, TASK_ONCE, &taak_on, &runner, false); + +void taak_off() { + pinMode(taak_pin, OUTPUT); + Serial.println("taak_off!"); + digitalWrite(taak_pin, LOW); +} +Task taak_off_task(0, TASK_ONCE, &taak_off, &runner, false); +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + if (n.velocity < 0) n.velocity = 0; + // + if (n.pitch < 0) n.pitch = 0; + taak_pin = n.pitch; + //taak_on && taak_off + if (n.velocity == 0) { + if (n.onoff == 1) { + taak_on_task.restartDelayed(10); + } else { + taak_off_task.restartDelayed(10); + } + } + //taak_hit + else { + taak_on_task.restartDelayed(10); + taak_off_task.restartDelayed(10 + n.velocity * 2); + } + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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(115200); + 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(); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_taak/message.h b/faa_taak/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_taak/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_tone/faa_tone.ino b/faa_tone/faa_tone.ino new file mode 100644 index 0000000..4a42fc8 --- /dev/null +++ b/faa_tone/faa_tone.ino @@ -0,0 +1,263 @@ +// +// 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 (7000) +#define MY_ID (MY_GROUP_ID + 101) +#define MY_SIGN ("PIANO") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +void port_read() { + // + byte port = 0; + static byte port_prev = 0; + port = 0x00; + port |= (digitalRead(D1) ? 0x01 : 0x00); + port |= (digitalRead(D2) ? 0x02 : 0x00); + port |= (digitalRead(D3) ? 0x04 : 0x00); // by design, PULLED-UP, being used for the FLASH button. + port |= (0 ? 0x08 : 0x00); // LED pin, skip + port |= (digitalRead(D5) ? 0x10 : 0x00); + port |= (digitalRead(D6) ? 0x20 : 0x00); + port |= (digitalRead(D7) ? 0x40 : 0x00); + port |= (digitalRead(D8) ? 0x80 : 0x00); + if (port_prev != port) { + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 1; // 1:digital + hello.h3 = port; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // + port_prev = port; + } +} +Task port_read_task(1, TASK_FOREVER, &port_read, &runner, true); // every 1 ms +// +void volume_read() { + // + int val = analogRead(A0); + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = 2; //2: analog + hello.h3 = val; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // +} +Task volume_read_task(50, TASK_FOREVER, &volume_read, &runner, false); // every 50 ms +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + if (n.velocity < 0) n.velocity = 0; + if (n.pitch < 0) n.pitch = 0; + // + // volume: task control + if (n.velocity == 0) { + //schedule 1 read + volume_read_task.disable(); + volume_read_task.setIterations(1); + volume_read_task.restart(); + } else { + // limiting max. speed. + if (n.velocity < 20) n.velocity = 20; + volume_read_task.setIterations(TASK_FOREVER); + volume_read_task.setInterval(n.velocity); + volume_read_task.restart(); + } + //button: pullup on/off + if (n.onoff == 1) { + pinMode(n.pitch, INPUT_PULLUP); + } else { + pinMode(n.pitch, INPUT); + } + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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); + + //input + pinMode(D1, INPUT); + pinMode(D2, INPUT); + pinMode(D3, INPUT); + // + pinMode(D5, INPUT); + pinMode(D6, INPUT); + pinMode(D7, INPUT); + pinMode(D8, INPUT); + + //serial + Serial.begin(115200); + 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(); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_tone/message.h b/faa_tone/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_tone/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_volume/faa_volume.ino b/faa_volume/faa_volume.ino new file mode 100644 index 0000000..21db0cb --- /dev/null +++ b/faa_volume/faa_volume.ino @@ -0,0 +1,221 @@ +// +// 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 (6000) +#define MY_ID (MY_GROUP_ID + 1) +#define MY_SIGN ("VOLUME") +//======================== + +//======================== +#define WIFI_CHANNEL 1 +//======================= + +//arduino +#include + +//message types +#include "message.h" + +//espnow +#include +#include + +//task +#include +Scheduler runner; + +//-*-*-*-*-*-*-*-*-*-*-*-*- +int volume_pin = A0; +bool volume_pullup = false; +void volume_read() { + // + if (volume_pullup) pinMode(volume_pin, INPUT_PULLUP); + else pinMode(volume_pin, INPUT); + // + int val = analogRead(volume_pin); + // + Serial.println(val); + // + byte mac[6]; + WiFi.macAddress(mac); + uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; + // + Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello' + // and you can append some floats + static int count = 0; + count++; + hello.h1 = (count % 1000); + hello.h2 = val; + // hello.h3 = 0; + // hello.h4 = 0; + // + uint8_t frm_size = sizeof(Hello) + 2; + uint8_t frm[frm_size]; + frm[0] = '{'; + memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); + frm[frm_size - 1] = '}'; + // + esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // +} +Task volume_read_task(50, TASK_FOREVER, &volume_read, &runner, true); // every 1 ms + +//*-*-*-*-*-*-*-*-*-*-*-*-* + +//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 'Note' +void onNoteHandler(Note & n) { + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + if (n.pitch < 0) n.pitch = 0; + volume_pin = n.pitch; //useless: for esp8266, A0 is only one adc. + // + if (n.velocity < 0) n.velocity = 0; + // + if (n.velocity == 0) { + //schedule 1 read + volume_read_task.disable(); + volume_read_task.setIterations(1); + volume_read_task.restart(); + } else { + // limiting max. speed. + if (n.velocity < 20) n.velocity = 20; + volume_read_task.setIterations(TASK_FOREVER); + volume_read_task.setInterval(n.velocity); + volume_read_task.restart(); + } + //pull-up on/off + if (n.onoff == 1) { + volume_pullup = true; //useless: for esp8266, A0 no support for internal pull-up ? + } else { + volume_pullup = false; + } + } +} + +// 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()); + } + + // open => identify => use. + if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { + Note note; + memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); + onNoteHandler(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(115200); + 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(); +} + +void loop() { + // + runner.execute(); + // +} diff --git a/faa_volume/message.h b/faa_volume/message.h new file mode 100644 index 0000000..8e2b9ab --- /dev/null +++ b/faa_volume/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; + } +};