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
 
@@ -34,97 +34,97 @@ bool esp_osc_init(esp_osc_client_t *clie
 
  }
 

	
 
  // 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':
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
 
@@ -103,75 +103,82 @@ static bool callback(const char *topic, 
 
      } 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)