acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

separate_audiotask.ino (5418B)

      1 #include "Arduino.h"
      2 #include "WiFi.h"
      3 #include "Audio.h"
      4 
      5 // Digital I/O used
      6 #define I2S_DOUT      25
      7 #define I2S_BCLK      27
      8 #define I2S_LRC       26
      9 
     10 Audio audio;
     11 
     12 String ssid =     "******";
     13 String password = "******";
     14 
     15 //****************************************************************************************
     16 //                                   A U D I O _ T A S K                                 *
     17 //****************************************************************************************
     18 
     19 struct audioMessage{
     20     uint8_t     cmd;
     21     const char* txt;
     22     uint32_t    value;
     23     uint32_t    ret;
     24 } audioTxMessage, audioRxMessage;
     25 
     26 enum : uint8_t { SET_VOLUME, GET_VOLUME, CONNECTTOHOST, CONNECTTOSD };
     27 
     28 QueueHandle_t audioSetQueue = NULL;
     29 QueueHandle_t audioGetQueue = NULL;
     30 
     31 void CreateQueues(){
     32     audioSetQueue = xQueueCreate(10, sizeof(struct audioMessage));
     33     audioGetQueue = xQueueCreate(10, sizeof(struct audioMessage));
     34 }
     35 
     36 void audioTask(void *parameter) {
     37     CreateQueues();
     38     if(!audioSetQueue || !audioGetQueue){
     39         log_e("queues are not initialized");
     40         while(true){;}  // endless loop
     41     }
     42 
     43     struct audioMessage audioRxTaskMessage;
     44     struct audioMessage audioTxTaskMessage;
     45 
     46     audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
     47     audio.setVolume(15); // 0...21
     48 
     49     while(true){
     50         if(xQueueReceive(audioSetQueue, &audioRxTaskMessage, 1) == pdPASS) {
     51             if(audioRxTaskMessage.cmd == SET_VOLUME){
     52                 audioTxTaskMessage.cmd = SET_VOLUME;
     53                 audio.setVolume(audioRxTaskMessage.value);
     54                 audioTxTaskMessage.ret = 1;
     55                 xQueueSend(audioGetQueue, &audioTxTaskMessage, portMAX_DELAY);
     56             }
     57             else if(audioRxTaskMessage.cmd == CONNECTTOHOST){
     58                 audioTxTaskMessage.cmd = CONNECTTOHOST;
     59                 audioTxTaskMessage.ret = audio.connecttohost(audioRxTaskMessage.txt);
     60                 xQueueSend(audioGetQueue, &audioTxTaskMessage, portMAX_DELAY);
     61             }
     62             else if(audioRxTaskMessage.cmd == CONNECTTOSD){
     63                 audioTxTaskMessage.cmd = CONNECTTOSD;
     64                 audioTxTaskMessage.ret = audio.connecttoSD(audioRxTaskMessage.txt);
     65                 xQueueSend(audioGetQueue, &audioTxTaskMessage, portMAX_DELAY);
     66             }
     67             else if(audioRxTaskMessage.cmd == GET_VOLUME){
     68                 audioTxTaskMessage.cmd = GET_VOLUME;
     69                 audioTxTaskMessage.ret = audio.getVolume();
     70                 xQueueSend(audioGetQueue, &audioTxTaskMessage, portMAX_DELAY);
     71             }
     72             else{
     73                 log_i("error");
     74             }
     75         }
     76         audio.loop();
     77     }
     78 }
     79 
     80 void audioInit() {
     81     xTaskCreatePinnedToCore(
     82         audioTask,             /* Function to implement the task */
     83         "audioplay",           /* Name of the task */
     84         5000,                  /* Stack size in words */
     85         NULL,                  /* Task input parameter */
     86         2 | portPRIVILEGE_BIT, /* Priority of the task */
     87         NULL,                  /* Task handle. */
     88         1                      /* Core where the task should run */
     89     );
     90 }
     91 
     92 audioMessage transmitReceive(audioMessage msg){
     93     xQueueSend(audioSetQueue, &msg, portMAX_DELAY);
     94     if(xQueueReceive(audioGetQueue, &audioRxMessage, portMAX_DELAY) == pdPASS){
     95         if(msg.cmd != audioRxMessage.cmd){
     96             log_e("wrong reply from message queue");
     97         }
     98     }
     99     return audioRxMessage;
    100 }
    101 
    102 void audioSetVolume(uint8_t vol){
    103     audioTxMessage.cmd = SET_VOLUME;
    104     audioTxMessage.value = vol;
    105     audioMessage RX = transmitReceive(audioTxMessage);
    106 }
    107 
    108 uint8_t audioGetVolume(){
    109     audioTxMessage.cmd = GET_VOLUME;
    110     audioMessage RX = transmitReceive(audioTxMessage);
    111     return RX.ret;
    112 }
    113 
    114 bool audioConnecttohost(const char* host){
    115     audioTxMessage.cmd = CONNECTTOHOST;
    116     audioTxMessage.txt = host;
    117     audioMessage RX = transmitReceive(audioTxMessage);
    118     return RX.ret;
    119 }
    120 
    121 bool audioConnecttoSD(const char* filename){
    122     audioTxMessage.cmd = CONNECTTOSD;
    123     audioTxMessage.txt = filename;
    124     audioMessage RX = transmitReceive(audioTxMessage);
    125     return RX.ret;
    126 }
    127 
    128 //****************************************************************************************
    129 //                                   S E T U P                                           *
    130 //****************************************************************************************
    131 
    132 void setup() {
    133     Serial.begin(115200);
    134     WiFi.begin(ssid.c_str(), password.c_str());
    135     while (WiFi.status() != WL_CONNECTED) delay(1500);
    136 
    137     audioInit();
    138 
    139     audioConnecttohost("http://mp3.ffh.de/radioffh/hqlivestream.mp3");
    140     audioSetVolume(15);
    141     log_i("current volume is: %d", audioGetVolume());
    142 }
    143 
    144 //****************************************************************************************
    145 //                                   L O O P                                             *
    146 //****************************************************************************************
    147 
    148 void loop(){
    149     // your own code here
    150 }
    151 //*****************************************************************************************
    152 //                                  E V E N T S                                           *
    153 //*****************************************************************************************
    154 
    155 void audio_info(const char *info){
    156     Serial.print("info        "); Serial.println(info);
    157 }
    158