muse.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. YOUR LICENSE
  3. */
  4. #include <string.h>
  5. #include <esp_log.h>
  6. #include <esp_types.h>
  7. #include <esp_system.h>
  8. #include <freertos/FreeRTOS.h>
  9. #include <freertos/task.h>
  10. //#include <driver/adc.h>
  11. #include "driver/rmt.h"
  12. #include "monitor.h"
  13. #include "targets.h"
  14. /////////////////////////////////////////////////////////////////
  15. //*********************** NeoPixels ***************************
  16. ////////////////////////////////////////////////////////////////
  17. #define NUM_LEDS 1
  18. #define LED_RMT_TX_CHANNEL 0
  19. #define LED_RMT_TX_GPIO 22
  20. #define BITS_PER_LED_CMD 24
  21. #define LED_BUFFER_ITEMS ((NUM_LEDS * BITS_PER_LED_CMD))
  22. // These values are determined by measuring pulse timing with logic analyzer and adjusting to match datasheet.
  23. #define T0H 14 // 0 bit high time
  24. #define T1H 52 // 1 bit high time
  25. #define TL 52 // low time for either bit
  26. #define GREEN 0xFF0000
  27. #define RED 0x00FF00
  28. #define BLUE 0x0000FF
  29. #define WHITE 0xFFFFFF
  30. #define YELLOW 0xE0F060
  31. struct led_state {
  32. uint32_t leds[NUM_LEDS];
  33. };
  34. void ws2812_control_init(void);
  35. void ws2812_write_leds(struct led_state new_state);
  36. ///////////////////////////////////////////////////////////////////
  37. static const char TAG[] = "muse";
  38. static void (*battery_handler_chain)(float value);
  39. static void battery_svc(float value);
  40. static bool init(void);
  41. const struct target_s target_muse = { "muse", init };
  42. static bool init(void) {
  43. battery_handler_chain = battery_handler_svc;
  44. battery_handler_svc = battery_svc;
  45. ESP_LOGI(TAG, "Initializing for Muse");
  46. return true;
  47. }
  48. static void battery_svc(float value) {
  49. ESP_LOGI(TAG, "Called for battery service with %f", value);
  50. // put here your code for LED according to value
  51. if (battery_handler_chain) battery_handler_chain(value);
  52. }
  53. // Battery monitoring
  54. /*
  55. static void battery(void *data)
  56. {
  57. #define VGREEN 2300
  58. #define VRED 2000
  59. #define NM 10
  60. static int val;
  61. static int V[NM];
  62. static int I=0;
  63. int S;
  64. for(int i=0;i<NM;i++)V[i]=VGREEN;
  65. vTaskDelay(1000 / portTICK_PERIOD_MS);
  66. struct led_state new_state;
  67. ws2812_control_init();
  68. // init ADC interface for battery survey
  69. adc1_config_width(ADC_WIDTH_BIT_12);
  70. adc1_config_channel_atten(ADC1_GPIO33_CHANNEL, ADC_ATTEN_DB_11);
  71. while(true)
  72. {
  73. vTaskDelay(1000 / portTICK_PERIOD_MS);
  74. V[I++] = adc1_get_raw(ADC1_GPIO33_CHANNEL);
  75. if(I >= NM)I = 0;
  76. S = 0;
  77. for(int i=0;i<NM;i++)S = S + V[i];
  78. val = S / NM;
  79. new_state.leds[0] = YELLOW;
  80. if(val > VGREEN) new_state.leds[0] = GREEN;
  81. if(val < VRED) new_state.leds[0] = RED;
  82. printf("====> %d %6x\n", val, new_state.leds[0]);
  83. ws2812_write_leds(new_state);
  84. }
  85. }
  86. */
  87. // This is the buffer which the hw peripheral will access while pulsing the output pin
  88. rmt_item32_t led_data_buffer[LED_BUFFER_ITEMS];
  89. void setup_rmt_data_buffer(struct led_state new_state);
  90. void ws2812_control_init(void)
  91. {
  92. rmt_config_t config;
  93. config.rmt_mode = RMT_MODE_TX;
  94. config.channel = LED_RMT_TX_CHANNEL;
  95. config.gpio_num = LED_RMT_TX_GPIO;
  96. config.mem_block_num = 3;
  97. config.tx_config.loop_en = false;
  98. config.tx_config.carrier_en = false;
  99. config.tx_config.idle_output_en = true;
  100. config.tx_config.idle_level = 0;
  101. config.clk_div = 2;
  102. ESP_ERROR_CHECK(rmt_config(&config));
  103. ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
  104. }
  105. void ws2812_write_leds(struct led_state new_state) {
  106. setup_rmt_data_buffer(new_state);
  107. ESP_ERROR_CHECK(rmt_write_items(LED_RMT_TX_CHANNEL, led_data_buffer, LED_BUFFER_ITEMS, false));
  108. ESP_ERROR_CHECK(rmt_wait_tx_done(LED_RMT_TX_CHANNEL, portMAX_DELAY));
  109. }
  110. void setup_rmt_data_buffer(struct led_state new_state)
  111. {
  112. for (uint32_t led = 0; led < NUM_LEDS; led++) {
  113. uint32_t bits_to_send = new_state.leds[led];
  114. uint32_t mask = 1 << (BITS_PER_LED_CMD - 1);
  115. for (uint32_t bit = 0; bit < BITS_PER_LED_CMD; bit++) {
  116. uint32_t bit_is_set = bits_to_send & mask;
  117. led_data_buffer[led * BITS_PER_LED_CMD + bit] = bit_is_set ?
  118. (rmt_item32_t){{{T1H, 1, TL, 0}}} :
  119. (rmt_item32_t){{{T0H, 1, TL, 0}}};
  120. mask >>= 1;
  121. }
  122. }
  123. }