diff --git a/audioooo/src/main.cpp b/audioooo/src/main.cpp index 358f491..2d9570d 100644 --- a/audioooo/src/main.cpp +++ b/audioooo/src/main.cpp @@ -16,7 +16,7 @@ //======================== // #define MY_GROUP_ID (10000) -#define MY_ID (MY_GROUP_ID + 7) +#define MY_ID (MY_GROUP_ID + 2) #define MY_SIGN ("AUDIOOOO") #define ADDRESSBOOK_TITLE ("broadcast only") // @@ -135,44 +135,44 @@ String screen_filename = "/___.mp3"; extern Task screen_cmd_notify_task; bool cmd_notify = false; void screen_cmd_notify() { - if (screen_cmd_notify_task.isFirstIteration()) cmd_notify = true; - else if (screen_cmd_notify_task.isLastIteration()) cmd_notify = false; - else cmd_notify = !cmd_notify; + if (screen_cmd_notify_task.isFirstIteration()) cmd_notify = true; + else if (screen_cmd_notify_task.isLastIteration()) cmd_notify = false; + else cmd_notify = !cmd_notify; } Task screen_cmd_notify_task(500, 10, &screen_cmd_notify, &runner, false); // 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); + //clear screen + a + int line_step = 12; + int line = 0; + display.clearDisplay(); + display.setTextColor(SSD1306_WHITE); + display.setTextSize(1); - //line1 - mode line (playing / stopped) + notify mark - display.setCursor(0, line); - if (audio.isRunning()) display.println("= playing ==="); - else display.println("* stopped !:."); - if (cmd_notify) { - display.setCursor(120, line); - display.println("*"); - } - line += line_step; + //line1 - mode line (playing / stopped) + notify mark + display.setCursor(0, line); + if (audio.isRunning()) display.println("= playing ==="); + else display.println("* stopped !:."); + if (cmd_notify) { + display.setCursor(120, line); + display.println("*"); + } + line += line_step; - //line2 - filename - display.setCursor(0, line); - display.println(screen_filename.c_str()); - line += line_step; + //line2 - filename + display.setCursor(0, line); + display.println(screen_filename.c_str()); + line += line_step; - //line3 - rf. last msg. - display.setCursor(0, line); - display.println(screen_cmd.c_str()); - line += line_step; - // - display.display(); - // + //line3 - rf. last msg. + display.setCursor(0, line); + display.println(screen_cmd.c_str()); + line += line_step; + // + display.display(); + // } Task screen_task(SCREEN_PERIOD, TASK_FOREVER, &screen, &runner, true); @@ -182,93 +182,104 @@ int sample_now = 0; //0~999 //on 'start' void sample_player_start() { - //filename buffer - 8.3 naming convension! 8+1+3+1 = 13 - // + '/' root symbol 13+1 = 14 (ESP32 specific?) - char filename[14] = "/NNN.mp3"; - //search for the sound file - int note = sample_now; - int nnn = (note % 1000); // 0~999 - int nn = (note % 100); // 0~99 - filename[1] = '0' + (nnn / 100); // N__.MP3 - filename[2] = '0' + (nn / 10); // _N_.MP3 - filename[3] = '0' + (nn % 10); // __N.MP3 - //TEST - Serial.println(filename); - screen_filename = String(filename); - bool test = SD.exists(filename); - if (!test) { - Serial.println("... does not exist."); - screen_filename = screen_filename + "_!NEXIST!"; - return; - } - //start the player! - audio.connecttoSD(filename); - delay(10); + //filename buffer - 8.3 naming convension! 8+1+3+1 = 13 + // + '/' root symbol 13+1 = 14 (ESP32 specific?) + char filename[14] = "/NNN.mp3"; + //search for the sound file + int note = sample_now; + int nnn = (note % 1000); // 0~999 + int nn = (note % 100); // 0~99 + filename[1] = '0' + (nnn / 100); // N__.MP3 + filename[2] = '0' + (nn / 10); // _N_.MP3 + filename[3] = '0' + (nn % 10); // __N.MP3 + //TEST + Serial.println(filename); + screen_filename = String(filename); + bool test = SD.exists(filename); + if (!test) { + Serial.println("... does not exist."); + screen_filename = screen_filename + "_!NEXIST!"; + return; + } + //start the player! + audio.connecttoSD(filename); + delay(10); } Task sample_player_start_task(0, TASK_ONCE, &sample_player_start, &runner, false); //on 'stop' void sample_player_stop() { - //filename buffer - 8.3 naming convension! 8+1+3+1 = 13 - // + '/' root symbol 13+1 = 14 (ESP32 specific?) - char filename[14] = "/NNN.mp3"; - //search for the sound file - int note = sample_now; - int nnn = (note % 1000); // 0~999 - int nn = (note % 100); // 0~99 - filename[1] = '0' + (nnn / 100); // N__.MP3 - filename[2] = '0' + (nn / 10); // _N_.MP3 - filename[3] = '0' + (nn % 10); // __N.MP3 - //TEST - Serial.println(filename); - screen_filename = String(filename); - bool test = SD.exists(filename); - if (!test) { - Serial.println("... does not exist."); - screen_filename = screen_filename + "_!NEXIST!"; - return; - } - //stop the player. - audio.stopSong(); + //filename buffer - 8.3 naming convension! 8+1+3+1 = 13 + // + '/' root symbol 13+1 = 14 (ESP32 specific?) + char filename[14] = "/NNN.mp3"; + //search for the sound file + int note = sample_now; + int nnn = (note % 1000); // 0~999 + int nn = (note % 100); // 0~99 + filename[1] = '0' + (nnn / 100); // N__.MP3 + filename[2] = '0' + (nn / 10); // _N_.MP3 + filename[3] = '0' + (nn % 10); // __N.MP3 + //TEST + Serial.println(filename); + screen_filename = String(filename); + bool test = SD.exists(filename); + if (!test) { + Serial.println("... does not exist."); + screen_filename = screen_filename + "_!NEXIST!"; + return; + } + //stop the player. + audio.stopSong(); } Task sample_player_stop_task(0, TASK_ONCE, &sample_player_stop, &runner, false); +//looping +void looping() +{ + if (!audio.isRunning()) { + //keep sample_now as it is. + sample_player_start_task.restartDelayed(10); + } +} +Task looping_task(5000, TASK_FOREVER, &looping, &runner, false); +// Task looping_task(5000, TASK_FOREVER, &looping, &runner, true); // < with looping task .. audio will always keep playing. (non stop.) + // #if defined(REPLICATE_NOTE_REQ) Note note_now = { - -1, // int32_t id; - -1, // float pitch; - -1, // float velocity; - -1, // float onoff; - -1, // float x1; - -1, // float x2; - -1, // float x3; - -1, // float x4; - -1 // float ps; + -1, // int32_t id; + -1, // float pitch; + -1, // float velocity; + -1, // float onoff; + -1, // float x1; + -1, // float x2; + -1, // float x3; + -1, // float x4; + -1 // float ps; }; #define NEW_NOTE_TIMEOUT (3000) static unsigned long new_note_time = (-1*NEW_NOTE_TIMEOUT); void repeat() { - // - uint8_t frm_size = sizeof(Note) + 2; - uint8_t frm[frm_size]; - frm[0] = '['; - memcpy(frm + 1, (uint8_t *) ¬e_now, sizeof(Note)); - frm[frm_size - 1] = ']'; - // - // strange but following didn't work as expected. (instead, i have to send one-by-one.) - // esp_now_send(NULL, frm, frm_size); // to all peers in the list. + // + uint8_t frm_size = sizeof(Note) + 2; + uint8_t frm[frm_size]; + frm[0] = '['; + memcpy(frm + 1, (uint8_t *) ¬e_now, sizeof(Note)); + frm[frm_size - 1] = ']'; + // + // strange but following didn't work as expected. (instead, i have to send one-by-one.) + // esp_now_send(NULL, frm, frm_size); // to all peers in the list. - // so, forget about peer list -> just pick a broadcast peer to be sent. - uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - esp_now_send(broadcastmac, frm, frm_size); + // so, forget about peer list -> just pick a broadcast peer to be sent. + uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + esp_now_send(broadcastmac, frm, frm_size); - // (DEBUG) fetch full peer list - { PeerLister a; a.print(); } + // (DEBUG) fetch full peer list + { PeerLister a; a.print(); } - // - MONITORING_SERIAL.print("\nrepeat! ==> "); - MONITORING_SERIAL.println(note_now.to_string()); + // + MONITORING_SERIAL.print("\nrepeat! ==> "); + MONITORING_SERIAL.println(note_now.to_string()); } Task repeat_task(0, TASK_ONCE, &repeat, &runner, false); #endif @@ -278,304 +289,304 @@ Task repeat_task(0, TASK_ONCE, &repeat, &runner, false); extern Task hello_task; static int hello_delay = 0; void hello() { - // - 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 = sample_now; - // 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. - // - // MONITORING_SERIAL.write(frm, frm_size); - // MONITORING_SERIAL.println(" ==(esp_now_send/0)==> "); - // - if (hello_delay > 0) { - if (hello_delay < 100) hello_delay = 100; - hello_task.restartDelayed(hello_delay); - } + // + 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 = sample_now; + // 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. + // + // MONITORING_SERIAL.write(frm, frm_size); + // MONITORING_SERIAL.println(" ==(esp_now_send/0)==> "); + // + if (hello_delay > 0) { + if (hello_delay < 100) hello_delay = 100; + hello_task.restartDelayed(hello_delay); + } } Task hello_task(0, TASK_ONCE, &hello, &runner, false); //task #0 : blink led 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; - } + // + 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, false); // makepython esp32 has NO led => disabled. // on 'Note' void onNoteHandler(Note & n) { - //is it for me? - if (n.id == MY_GROUP_ID || n.id == MY_ID) { - // - screen_cmd = n.to_string(); - screen_cmd_notify_task.restart(); - // - if (n.velocity != 0 || n.onoff == 2) { - audio.setVolume(n.velocity * 21 / 127 * GAIN_MAX); // 0...127 ==> 0...21 + //is it for me? + if (n.id == MY_GROUP_ID || n.id == MY_ID) { + // + screen_cmd = n.to_string(); + screen_cmd_notify_task.restart(); + // + if (n.velocity != 0 || n.onoff == 2) { + audio.setVolume(n.velocity * 21 / 127 * GAIN_MAX); // 0...127 ==> 0...21 + } + // + if (n.onoff == 1) { + // filter out re-triggering same note while it is playing. + if (!audio.isRunning() || sample_now != n.pitch) { + sample_now = n.pitch; + sample_player_start_task.restartDelayed(10); + } + } else if (n.onoff == 0) { + sample_now = n.pitch; + sample_player_stop_task.restartDelayed(10); + } + // } - // - if (n.onoff == 1) { - // filter out re-triggering same note while it is playing. - if (!audio.isRunning() || sample_now != n.pitch) { - sample_now = n.pitch; - sample_player_start_task.restartDelayed(10); - } - } else if (n.onoff == 0) { - sample_now = n.pitch; - sample_player_stop_task.restartDelayed(10); - } - // - } } // on 'receive' void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int32_t len) { - // - // MONITORING_SERIAL.write(incomingData, len); + // + // MONITORING_SERIAL.write(incomingData, len); - // + // #if defined(HAVE_CLIENT) - Serial.write(incomingData, len); // we pass it over to the client. + Serial.write(incomingData, len); // we pass it over to the client. #endif - // open => identify => use. - if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) { - Hello hello(""); - memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello)); - // - MONITORING_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); - - //is it for me? - if (note.id == MY_GROUP_ID || note.id == MY_ID) { - hello_delay = note.ps; - if (hello_delay > 0 && hello_task.isEnabled() == false) { - hello_task.restart(); - } + // open => identify => use. + if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) { + Hello hello(""); + memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello)); + // + MONITORING_SERIAL.println(hello.to_string()); + // } - MONITORING_SERIAL.println(note.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); + + //is it for me? + if (note.id == MY_GROUP_ID || note.id == MY_ID) { + hello_delay = note.ps; + if (hello_delay > 0 && hello_task.isEnabled() == false) { + hello_task.restart(); + } + } + + MONITORING_SERIAL.println(note.to_string()); #if defined(REPLICATE_NOTE_REQ) - if (millis() - new_note_time > NEW_NOTE_TIMEOUT) { - note_now = note; - repeat_task.restart(); - new_note_time = millis(); - } + if (millis() - new_note_time > NEW_NOTE_TIMEOUT) { + note_now = note; + repeat_task.restart(); + new_note_time = millis(); + } #endif - } + } } // on 'sent' void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sendStatus) { - // well, i think this cb should be called once for EVERY single TX attempts, - // but in reality, it doesn't get called that often. - // i think this is sorta bug. but have no clear clue. - MONITORING_SERIAL.printf("* delivery attempt! ~~~>>> %02X:%02X:%02X:%02X:%02X:%02X\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); - if (sendStatus != 0) MONITORING_SERIAL.printf("* ==>> FAILED :(\n\n"); + // well, i think this cb should be called once for EVERY single TX attempts, + // but in reality, it doesn't get called that often. + // i think this is sorta bug. but have no clear clue. + MONITORING_SERIAL.printf("* delivery attempt! ~~~>>> %02X:%02X:%02X:%02X:%02X:%02X\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); + if (sendStatus != 0) MONITORING_SERIAL.printf("* ==>> FAILED :(\n\n"); } // SD TEST void printDirectory(File dir, int numTabs) { - // char filename[256] = ""; - while(true) { - File entry = dir.openNextFile(); - if (!entry) { - // no more files - break; + // char filename[256] = ""; + while(true) { + File entry = dir.openNextFile(); + if (!entry) { + // no more files + break; + } + for (uint8_t i=0; i \"" + String(MY_SIGN) + "\""); - Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL)); - Serial.println("- my peer book ==> \"" + String(ADDRESSBOOK_TITLE) + "\""); + //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("- my peer book ==> \"" + String(ADDRESSBOOK_TITLE) + "\""); #if defined(HAVE_CLIENT) - Serial.println("- ======== 'HAVE_CLIENT' ========"); + Serial.println("- ======== 'HAVE_CLIENT' ========"); #endif #if defined(SERIAL_SWAP) - Serial.println("- ======== 'SERIAL_SWAP' ========"); + Serial.println("- ======== 'SERIAL_SWAP' ========"); #endif #if defined(DISABLE_AP) - Serial.println("- ======== 'DISABLE_AP' ========"); + Serial.println("- ======== 'DISABLE_AP' ========"); #endif #if defined(HAVE_CLIENT_I2C) - Serial.println("- ======== 'HAVE_CLIENT_I2C' ========"); + Serial.println("- ======== 'HAVE_CLIENT_I2C' ========"); #endif #if defined(REPLICATE_NOTE_REQ) - Serial.println("- ======== 'REPLICATE_NOTE_REQ' ========"); + Serial.println("- ======== 'REPLICATE_NOTE_REQ' ========"); #endif - Serial.println("-"); + Serial.println("-"); - //wifi - WiFiMode_t node_type = WIFI_AP_STA; + //wifi + WiFiMode_t node_type = WIFI_AP_STA; #if defined(DISABLE_AP) - // system_phy_set_max_tpw(0); - WiFi.setTxPower(WIFI_POWER_MINUS_1dBm); // Set WiFi RF power output to lowest level - node_type = WIFI_STA; + // system_phy_set_max_tpw(0); + WiFi.setTxPower(WIFI_POWER_MINUS_1dBm); // Set WiFi RF power output to lowest level + node_type = WIFI_STA; #endif - WiFi.mode(node_type); + WiFi.mode(node_type); - //esp-now - if (esp_now_init() != 0) { - Serial.println("Error initializing ESP-NOW"); - return; - } - 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_peer_info_t peerInfo = {}; - // memcpy(peerInfo.peer_addr, broadcastmac, 6); - // peerInfo.channel = 0; - // peerInfo.encrypt = false; - // esp_now_add_peer(&peerInfo); + //esp-now + if (esp_now_init() != 0) { + Serial.println("Error initializing ESP-NOW"); + return; + } + 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_peer_info_t peerInfo = {}; + // memcpy(peerInfo.peer_addr, broadcastmac, 6); + // peerInfo.channel = 0; + // peerInfo.encrypt = false; + // esp_now_add_peer(&peerInfo); - 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 + "'."); - 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); - } - // (DEBUG) fetch full peer list - { PeerLister a; a.print(); } - // - Serial.println("-"); - Serial.println("\".-.-.-. :)\""); - Serial.println(); + 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 + "'."); + 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); + } + // (DEBUG) fetch full peer list + { PeerLister a; a.print(); } + // + Serial.println("-"); + Serial.println("\".-.-.-. :)\""); + Serial.println(); } void loop() { - // - audio.loop(); - // - runner.execute(); - // + // + audio.loop(); + // + runner.execute(); + // }