Changeset - 48d2337d76c2
[Not reviewed]
default
0 4 0
Ethan Zonca - 8 months ago 2024-09-29 12:16:50
ez@ethanzonca.com
Functional OSC TX and RX
4 files changed with 69 insertions and 40 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;
 
  }
 

	
 
@@ -58,48 +57,51 @@ esp_osc_target_t esp_osc_target(const ch
 

	
 
  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");
 
  }
 

	
 
  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;
 
    }
main/main.c
Show inline comments
 
@@ -27,44 +27,44 @@ 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);
 

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

	
 
    // Connect to wifi
 
    wifi_init();
 

	
 
    ledstrip_init();
 

	
 
    squeeze_init();
 

	
 
    // Initialize OSC
 
    osc_init();
 

	
 
    squeeze_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)
 
    {
 
        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"
 

	
 
#define WIFI_SSID ""
 
#define WIFI_PASS ""
 

	
 
#define OSC_ADDRESS ""
 
#define OSC_PORT 0
 

	
 
static const char *TAG = "osc_control";
 

	
 
esp_osc_client_t client;
 
#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),
 
      esp_osc_target(OSC_ADDRESS, OSC_PORT),
 
  };
 
// 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);
 
//   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");
 
    }
 
  }
 
}
 
//     // 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)
 
@@ -96,58 +93,85 @@ static bool callback(const char *topic, 
 
        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;
 
}
 

	
 
static void receiver() {
 

	
 
#define FLAMESPEWER_IP "192.168.1.204"
 

	
 
static void osc_process() 
 
{
 

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

	
 
static void restarter() {
 
  for (;;) {
 
    // delay
 
    vTaskDelay(5000 / portTICK_PERIOD_MS);
 

	
 
    // restart client
 
    esp_osc_init(&client, 1024, 9000);
 
    // 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;
 

	
 
  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());
 
    }
 

	
 
    vTaskDelay(pdMS_TO_TICKS(10));
 
  }
 
}
 

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

	
 
  // create tasks
 
  //xTaskCreatePinnedToCore(sender, "sender", 4096, NULL, 10, NULL, 1);
 
  xTaskCreatePinnedToCore(receiver, "receiver", 4096, NULL, 10, NULL, 1);
 
  //xTaskCreatePinnedToCore(restarter, "restarter", 4096, NULL, 10, NULL, 1);
 
  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
main/wifi.c
Show inline comments
 
@@ -52,48 +52,51 @@ static void __event_handler(void* arg, e
 
        char out[128] = {0};
 
        snprintf(out, 128, "WIFI OK\nIP: " IPSTR, IP2STR(&event->ip_info.ip));
 
        display_update_text(out);
 

	
 

	
 
        s_retry_num = 0;
 
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
 
    }
 
}
 

	
 

	
 
// Initialize wifi and connect to network
 
void wifi_init(void)
 
{
 
    s_wifi_event_group = xEventGroupCreate();
 

	
 
    ESP_ERROR_CHECK(esp_netif_init());
 

	
 
    ESP_ERROR_CHECK(esp_event_loop_create_default());
 
    esp_netif_create_default_wifi_sta();
 

	
 
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
 
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
 

	
 
    // EMZ new
 
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
 

	
 
    esp_event_handler_instance_t instance_any_id;
 
    esp_event_handler_instance_t instance_got_ip;
 
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
 
                                                        ESP_EVENT_ANY_ID,
 
                                                        &__event_handler,
 
                                                        NULL,
 
                                                        &instance_any_id));
 
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
 
                                                        IP_EVENT_STA_GOT_IP,
 
                                                        &__event_handler,
 
                                                        NULL,
 
                                                        &instance_got_ip));
 

	
 
    wifi_config_t wifi_config = {
 
        .sta = {
 
            .ssid = EXAMPLE_ESP_WIFI_SSID,
 
            .password = EXAMPLE_ESP_WIFI_PASS,
 
            /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
 
             * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
 
             * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
 
             * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
 
             */
 
            .threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
 
            .sae_pwe_h2e = ESP_WIFI_SAE_MODE,
0 comments (0 inline, 0 general)