nexaTransmit.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_sleep.h"
  9. #include "esp_log.h"
  10. #include "driver/uart.h"
  11. #include "driver/rtc_io.h"
  12. #include "esp_intr_alloc.h"
  13. #include "esp_attr.h"
  14. #include "driver/timer.h"
  15. #include <stddef.h>
  16. #include "esp_intr_alloc.h"
  17. #include "transceiver.h"
  18. #include "rxTimer.h"
  19. #include "led.h"
  20. QueueHandle_t nexaTxQueue = NULL;
  21. // Nexa : http://elektronikforumet.com/wiki/index.php/RF_Protokoll_-_Nexa_sj%C3%A4lvl%C3%A4rande
  22. // "1" = 295 µs hög och 170 µs låg
  23. // "0" = 295 µs hög och 920 µs låg
  24. //
  25. // Etta skickas som 10 och Nolla som 01
  26. //
  27. // Dessa siffror från web-sidan stämmer inte med den Nexa-kontroll som jag har.
  28. // Jag har rättat dessa med siffror från verkligheten. Finns i define's nedan.
  29. // <NT01995080>
  30. // <NT01995090>
  31. // Timing for NEXA Remote controls
  32. #define HIGH_PULSE 230
  33. #define LOW_PULSE_0 340
  34. #define LOW_PULSE_1 1410
  35. #define SYNC_PULSE 2890
  36. #define STOP_PULSE 10000
  37. static void send1() {
  38. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  39. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  40. }
  41. static void send0() {
  42. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  43. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  44. }
  45. static void sendSync() {
  46. trcvSendHighLowPulse( HIGH_PULSE, SYNC_PULSE );
  47. }
  48. static void sendStop() {
  49. trcvSendHighLowPulse( HIGH_PULSE, STOP_PULSE );
  50. }
  51. static void sendNexaCodeNo(uint32_t orig_code) {
  52. uint32_t code=0;
  53. int32_t i;
  54. int32_t loop;
  55. ESP_LOGI("NEXA TX", "<NT%08lX>",(unsigned long int)orig_code);
  56. blinkTheLED();
  57. if( 1 ) {
  58. trcvSwitch2transmit();
  59. for( loop=4; loop>0; loop--) {
  60. code = orig_code;
  61. sendSync();
  62. for(i=0; i < 32; i++) {
  63. if( code & 0x80000000 ) {
  64. send1();
  65. }
  66. else {
  67. send0();
  68. }
  69. code = (code << 1);
  70. }
  71. sendStop();
  72. }
  73. trcvSwitch2receive();
  74. }
  75. }
  76. // Public interface for sending a NEXA Code
  77. void sendNexaCode(uint32_t data) {
  78. if( nexaTxQueue != NULL ) xQueueSend( nexaTxQueue, &data, 0 );
  79. }
  80. // Public interface for sending a NEXA Code in string format. For example: <NT4C90AD91>
  81. void sendNexaCodeStr(char *str) {
  82. if( str[0] == '<' && str[11] == '>' ) {
  83. uint32_t code;
  84. sscanf((const char *)str,"<NT%08lX>",(long unsigned int *)&code);
  85. sendNexaCode(code);
  86. }
  87. }
  88. static void nexaTxTask(void *pvParameter)
  89. {
  90. ESP_LOGI("NEXA TX", "Task started.");
  91. uint32_t data;
  92. while (1)
  93. {
  94. while( xQueueReceive( nexaTxQueue, &data, portMAX_DELAY ) == pdTRUE ) {
  95. sendNexaCodeNo(data);
  96. }
  97. }
  98. vTaskDelete(NULL);
  99. }
  100. void initNexaTransmit() {
  101. nexaTxQueue = xQueueCreate( 20, sizeof( uint32_t ) );
  102. xTaskCreatePinnedToCore(&nexaTxTask, "nexaTxTask", 8192, NULL, tskIDLE_PRIORITY + 2, NULL, 0);
  103. }