Changeset - 755225bf1731
[Not reviewed]
default
0 4 0
Ethan Zonca - 8 months ago 2024-09-29 20:02:43
ez@ethanzonca.com
Add discovery message TX. Add unused nonvolatile flash saving.
4 files changed with 25 insertions and 11 deletions:
0 comments (0 inline, 0 general)
components/esp-osc/esp_osc.c
Show inline comments
 
#include <stdio.h>
 
#include <stdarg.h>
 
#include <esp_log.h>
 
#include "esp_osc.h"
 

	
 
#define TAG "esp-osc"
 

	
 
bool esp_osc_init(esp_osc_client_t *client, uint16_t buf_len, uint16_t port) {
 
  // free existing memory
 
  if (client->sbuf != NULL) {
 
    free(client->sbuf);
 
  }
 
  if (client->rbuf != NULL) {
 
    free(client->rbuf);
 
  }
 

	
 
  // allocate memory
 
  client->sbuf = malloc(buf_len);
 
  client->rbuf = malloc(buf_len);
 
  client->len = buf_len;
 

	
 
  // close existing socket
 
  if (client->socket != 0) {
 
    close(client->socket);
 
    client->socket = 0;
 
  }
 

	
 
  // create socket
 
  client->socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
 
  if (client->socket <= 0) {
 
    ESP_LOGE(TAG, "failed to create socket (%d)", errno);
 
    client->socket = 0;
 
    return false;
 
  }
 

	
 
  // bind socket if port is available
 
  if (port > 0) {
 
    struct sockaddr_in addr = {0};
 
    addr.sin_addr.s_addr = inet_addr("0.0.0.0");
 
    addr.sin_family = AF_INET;
 
    addr.sin_port = htons(port);
 
    if (bind(client->socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
 
      ESP_LOGE(TAG, "failed to bind socket (%d)", errno);
 
      return false;
 
    }
 
  }
 

	
 
  return true;
 
}
 

	
 
esp_osc_target_t esp_osc_target(const char *address, uint16_t port) {
 
  // prepare target
 
  esp_osc_target_t target = {0};
 
  target.addr.sin_addr.s_addr = inet_addr(address);
 
  target.addr.sin_family = AF_INET;
 
  target.addr.sin_port = htons(port);
 

	
 
  return target;
 
}
 

	
 
bool esp_osc_send(esp_osc_client_t *client, esp_osc_target_t *target, const char *topic, const char *format, ...) {
 
  // send message
 
  va_list args;
 
  va_start(args, format);
 
  bool ret = esp_osc_send_v(client, target, topic, format, args);
 
  va_end(args);
 

	
 
  return ret;
 
}
 

	
 
bool esp_osc_send_v(esp_osc_client_t *client, esp_osc_target_t *target, const char *topic, const char *format,
 
                    va_list args) {
 
  // prepare message
 
  uint32_t length = tosc_vwrite((char *)client->sbuf, client->len, topic, format, args);
 

	
 
  // send message
 
  if (sendto(client->socket, client->sbuf, length, 0, (struct sockaddr *)&target->addr, sizeof(target->addr)) < 0) {
 
    ESP_LOGE(TAG, "failed to send message (%d)", errno);
 
    return false;
 
  }
 
  else{
 
    ESP_LOGI(TAG, "sent message OK");
 
    // ESP_LOGI(TAG, "sent message OK");
 
  }
 

	
 
  return true;
 
}
 

	
 
bool esp_osc_receive(esp_osc_client_t *client, esp_osc_callback_t callback) {
 
  // prepare values
 
  esp_osc_value_t values[32];
 

	
 
  for (;;) {
 
    // receive message
 
    ssize_t ret = recvfrom(client->socket, client->rbuf, client->len, 0, NULL, NULL);
 
    if (ret < 0) {
 
      ESP_LOGE(TAG, "failed to receive message (%d)", errno);
 
      return false;
 
    } else if (ret > client->len) {
 
      ESP_LOGE(TAG, "discard too long message (%d)", ret);
 
      return false;
 
    }
 

	
 
    // check bundle
 
    if (tosc_isBundle(client->rbuf)) {
 
      ESP_LOGE(TAG, "discard unsupported bundle");
 
      return false;
 
    }
 

	
 
    // parse message
 
    tosc_message msg = {0};
 
    int res = tosc_parseMessage(&msg, client->rbuf, client->len);
 
    if (res < 0) {
 
      ESP_LOGE(TAG, "failed to parse message (%d)", res);
 
      return false;
 
    }
 

	
 
    // get format
 
    char *fmt = tosc_getFormat(&msg);
 

	
 
    // get size
 
    size_t size = strlen(fmt);
 
    if (size > sizeof(values)) {
 
      ESP_LOGE(TAG, "message has too many values (%d)", size);
 
      return false;
 
    }
 

	
 
    // parse values
 
    for (size_t i = 0; i < size; i++) {
 
      switch (fmt[i]) {
 
        case 'i':
 
          values[i].i = tosc_getNextInt32(&msg);
 
          break;
 
        case 'h':
 
          values[i].h = tosc_getNextInt64(&msg);
 
          break;
 
        case 'f':
 
          values[i].f = tosc_getNextFloat(&msg);
 
          break;
 
        case 'd':
 
          values[i].d = tosc_getNextDouble(&msg);
 
          break;
 
        case 's':
 
          values[i].s = tosc_getNextString(&msg);
 
          break;
 
        case 'b':
 
          tosc_getNextBlob(&msg, &values[i].b, &values[i].bl);
 
          break;
 
      }
 
    }
 

	
 
    // call callback
 
    if (!callback(tosc_getAddress(&msg), fmt, values)) {
 
      return false;
 
    }
 
  }
 

	
 
  return true;
 
}
main/CMakeLists.txt
Show inline comments
 
idf_component_register(SRCS "main.c" "wifi.c" "usb_cdc.c" "can.c" "display.c" "display_gui.c" "osc_control.c" "ledstrip.c" "squeeze.c"
 
idf_component_register(SRCS "main.c" "wifi.c" "usb_cdc.c" "can.c" "display.c" "display_gui.c" "osc_control.c" "ledstrip.c" "squeeze.c" "flash.c"
 
                       INCLUDE_DIRS .)
main/main.c
Show inline comments
 
//
 
// Protofusion ESP32S3 Template
 
//
 

	
 
#include <stdint.h>
 
#include "freertos/FreeRTOS.h"
 
#include "freertos/task.h"
 
#include "freertos/event_groups.h"
 
#include "esp_system.h"
 
#include "esp_event.h"
 
#include "esp_log.h"
 
#include "nvs_flash.h"
 
#include "flash.h"
 
#include "sdkconfig.h"
 
#include "osc_control.h"
 
#include "wifi.h"
 
// #include "usb_cdc.h"
 
#include "can.h"
 
#include "display.h"
 
#include "squeeze.h"
 
#include "ledstrip.h"
 
#include "display_gui.h"
 

	
 

	
 
// Private variables
 
static const char *TAG = "main";
 

	
 

	
 
// Application entry point
 
void app_main(void)
 
{
 
    // Initialize usb-cdc interface
 
    // usb_cdc_init();
 

	
 
    // Initialize NVS
 
    esp_err_t ret = nvs_flash_init();
 
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
 
        ESP_ERROR_CHECK(nvs_flash_erase());
 
        ret = nvs_flash_init();
 
    }
 
    ESP_ERROR_CHECK(ret);
 
    // esp_err_t ret = nvs_flash_init();
 
    // if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
 
    //     ESP_ERROR_CHECK(nvs_flash_erase());
 
    //     ret = nvs_flash_init();
 
    // }
 
    // ESP_ERROR_CHECK(ret);
 

	
 
    flash_init();
 
    int32_t cnt = flash_read("count");
 
    cnt += 1;
 
    flash_write("count", cnt);
 

	
 

	
 
    // Initialize display
 
    display_init();
 
    display_gui_homescreen();
 

	
 
    // Connect to wifi
 
    wifi_init();
 

	
 
    ledstrip_init();
 

	
 
    squeeze_init();
 

	
 
    // Initialize OSC
 
    osc_init();
 

	
 

	
 
    // Initialize canbus
 
    //can_init();
 

	
 
    // Start tasks
 
    xTaskCreatePinnedToCore(ledstrip_refresh,    "ledstrip_refresh", 4096, NULL, 10, NULL, 1);
 
    xTaskCreatePinnedToCore(display_gui_process, "display_gui_process", 4096, NULL, 10, NULL, 1);
 

	
 

	
 
    while(1)
 
    {
 
        // ESP_LOGI(TAG, "cnt=%ld\n", cnt);
 

	
 
        vTaskDelay(pdMS_TO_TICKS(100));
 
    }
 

	
 
}
main/osc_control.c
Show inline comments
 
#include "osc_control.h"
 
#include <stdlib.h>
 
#include <esp_event.h>
 
#include <esp_log.h>
 
#include <esp_wifi.h>
 
#include "display.h"
 
#include <esp_osc.h>
 
#include "ledstrip.h"
 
#include "squeeze.h"
 

	
 

	
 
static const char *TAG = "osc_control";
 

	
 
#define FLAMESPEWER_IP "192.168.1.25"
 

	
 
static esp_osc_client_t client;
 

	
 
// static void sender()
 
// {
 
//   // select targets
 
//   esp_osc_target_t targets[2] = {
 
//       esp_osc_target("127.0.0.1", 9000),
 
//   };
 

	
 
//   for(;;) {
 
//     // delay
 
//     vTaskDelay(1000 / portTICK_PERIOD_MS);
 

	
 
//     // send messages
 
//     for (size_t i = 0; i < 2; i++) {
 
//       esp_osc_send(&client, &targets[i], "test", "ihfdsb", 42, (int64_t)84, 3.14f, 6.28, "foo", 3, "bar");
 
//     }
 
//   }
 
// }
 

	
 
static uint8_t r_man = 0;
 
static uint8_t g_man = 0;
 
static uint8_t b_man = 0;
 

	
 

	
 
static bool callback(const char *topic, const char *format, esp_osc_value_t *values)
 
{
 
  // log message
 
  ESP_LOGI(TAG, "got message: %s (%s)", topic, format);
 

	
 

	
 
  char out[512] = {0};
 
  snprintf(out, 128, "topic: %s\nformat: %s\n", topic, format);
 

	
 
  for (size_t i = 0; i < strlen(format); i++) 
 
  {
 
    switch (format[i])
 
    {
 
      case 'i':
 
      {
 
        snprintf(out+strlen(out), 128-strlen(out), "Value: %ld", values[i].i);
 
        ESP_LOGI(TAG, "==> i: %ld", values[i].i);
 
        if(strcmp(topic, "/led_effect") == 0)
 
        {
 
          ledstrip_set_effect(values[i].i);
 
        }
 
      } break;
 

	
 
      case 'h':
 
      {
 
        ESP_LOGI(TAG, "==> h: %lld", values[i].h);
 
      }  break;
 

	
 
      case 'f':
 
      {
 
        if(values[i].f > 1.0)
 
          values[i].f = 1.0;
 
        if(values[i].f < 0.0)
 
          values[i].f = 0.0;
 

	
 
        snprintf(out+strlen(out), 128-strlen(out), "Value: %f", values[i].f);
 
        ESP_LOGI(TAG, "==> f: %f", values[i].f);
 

	
 
        if(strcmp(topic, "/led_modifier") == 0)
 
        {
 
          ledstrip_set_modifier(values[i].f);
 
        }
 
        else if(strcmp(topic, "/led_manual_r") == 0)
 
        {
 
          r_man = values[i].f * 255.0;
 
          ledstrip_set(g_man, r_man, b_man);
 
        }
 
        else if(strcmp(topic, "/led_manual_g") == 0)
 
        {
 
          g_man = values[i].f * 255.0;
 
          ledstrip_set(g_man, r_man, b_man);
 
        }
 
        else if(strcmp(topic, "/led_manual_b") == 0)
 
        {
 
          b_man = values[i].f * 255.0;
 
          ledstrip_set(g_man, r_man, b_man);
 
        }
 
      } break;
 

	
 
      case 'd':
 
      {
 
        ESP_LOGI(TAG, "==> d: %f", values[i].d);
 
      } break;
 

	
 
      case 's':
 
      {
 
        ESP_LOGI(TAG, "==> s: %s", values[i].s);
 
      }  break;
 

	
 
      case 'b':
 
      {
 
        ESP_LOGI(TAG, "==> b: %.*s (%d)", values[i].bl, values[i].b, values[i].bl);
 
        break;
 
      }
 

	
 
    }
 
    
 
  }
 

	
 
  display_update_text(out);
 

	
 
  return true;
 
}
 

	
 

	
 
#define FLAMESPEWER_IP "192.168.1.204"
 

	
 
static void osc_process() 
 
{
 

	
 
  for (;;) {
 
    // receive messages
 
    esp_osc_receive(&client, callback);
 
    // vTaskDelay(pdMS_TO_TICKS(100));
 
    vTaskDelay(pdMS_TO_TICKS(100));
 
  }
 
}
 

	
 

	
 
//#define pdTICKSTOMS( xTicks ) ( ( ( TickTypet ) ( xTicks ) * 1000u ) / configTICKRATE_HZ )
 

	
 
static void osc_send() 
 
{
 
  ESP_LOGI(TAG, "begin task osc_send");
 

	
 
  // // Really need this to wait for wifi connect
 
  esp_osc_target_t target = esp_osc_target("192.168.1.25", 5005);
 
    vTaskDelay(pdMS_TO_TICKS(2000));
 

	
 
  uint32_t last_tx = 0;
 
  uint32_t last_discover = 0;
 

	
 
  for (;;) {
 

	
 

	
 
    if(squeeze_triggered())
 
    {
 
      ESP_LOGI(TAG, "trig that squeeze");
 

	
 
      esp_osc_send(&client, &target, "/squeeze_trig", "i", 1);
 
    }
 

	
 
    if(xTaskGetTickCount()*portTICK_PERIOD_MS - last_tx > 150)
 
    {
 
      last_tx = xTaskGetTickCount() * portTICK_PERIOD_MS;
 
      esp_osc_send(&client, &target, "/squeeze_value", "f", squeeze_value());
 
    }
 

	
 
    if(xTaskGetTickCount()*portTICK_PERIOD_MS - last_discover > 1000)
 
    {
 
      last_discover = xTaskGetTickCount() * portTICK_PERIOD_MS;
 
      esp_osc_send(&client, &target, "/discover", "s", "ledstrip");
 
    }
 

	
 
    vTaskDelay(pdMS_TO_TICKS(10));
 
  }
 
}
 

	
 
void osc_init() {
 
  esp_osc_init(&client, 1024, 5005);
 
  esp_osc_init(&client, 1024, 5000);
 

	
 
  xTaskCreatePinnedToCore(osc_process, "osc_process", 1024*8, NULL, 10, NULL, 1);
 
  xTaskCreatePinnedToCore(osc_send, "osc_send", 1024*4, NULL, 10, NULL, 1);
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)