nexaTransmit.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "udp_client.h"
  21. QueueHandle_t nexaTxQueue = NULL;
  22. // Nexa : http://elektronikforumet.com/wiki/index.php/RF_Protokoll_-_Nexa_sj%C3%A4lvl%C3%A4rande
  23. // "1" = 295 µs hög och 170 µs låg
  24. // "0" = 295 µs hög och 920 µs låg
  25. //
  26. // Etta skickas som 10 och Nolla som 01
  27. //
  28. // Dessa siffror från web-sidan stämmer inte med den Nexa-kontroll som jag har.
  29. // Jag har rättat dessa med siffror från verkligheten. Finns i define's nedan.
  30. // <NT01995080>
  31. // <NT01995090>
  32. // Timing for NEXA Remote controls
  33. #define HIGH_PULSE 260
  34. #define LOW_PULSE_0 310
  35. #define LOW_PULSE_1 1330
  36. #define SYNC_PULSE 2720
  37. #define STOP_PULSE 10000
  38. static void send1() {
  39. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  40. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  41. }
  42. static void send0() {
  43. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_0 ); // 0
  44. trcvSendHighLowPulse( HIGH_PULSE, LOW_PULSE_1); // 1
  45. }
  46. static void sendSync() {
  47. trcvSendHighLowPulse( HIGH_PULSE, SYNC_PULSE );
  48. }
  49. static void sendStop() {
  50. trcvSendHighLowPulse( HIGH_PULSE, STOP_PULSE );
  51. }
  52. static void sendNexaCodeNo(uint32_t orig_code) {
  53. uint32_t code=0;
  54. int32_t i;
  55. int32_t loop;
  56. ESP_LOGI("NEXA TX", "<NT%08lX>",(unsigned long int)orig_code);
  57. blinkTheLED();
  58. if( 1 ) {
  59. trcvSwitch2transmit();
  60. for( loop=4; loop>0; loop--) {
  61. code = orig_code;
  62. sendSync();
  63. for(i=0; i < 32; i++) {
  64. if( code & 0x80000000 ) {
  65. send1();
  66. }
  67. else {
  68. send0();
  69. }
  70. code = (code << 1);
  71. }
  72. sendStop();
  73. }
  74. trcvSwitch2receive();
  75. }
  76. }
  77. // Public interface for sending a NEXA Code
  78. void sendNexaCode(uint32_t data) {
  79. if( nexaTxQueue != NULL ) xQueueSend( nexaTxQueue, &data, 0 );
  80. }
  81. // Public interface for sending a NEXA Code in string format. For example: <NT4C90AD91>
  82. void sendNexaCodeStr(char *str) {
  83. if( str[0] == '<' && str[11] == '>' ) {
  84. uint32_t code;
  85. sscanf((const char *)str,"<NT%08lX>",(long unsigned int *)&code);
  86. sendNexaCode(code);
  87. }
  88. }
  89. static void nexaTxTask(void *pvParameter)
  90. {
  91. ESP_LOGI("NEXA TX", "Task started.");
  92. uint32_t data;
  93. while (1)
  94. {
  95. while( xQueueReceive( nexaTxQueue, &data, portMAX_DELAY ) == pdTRUE ) {
  96. sendNexaCodeNo(data);
  97. // Forward the NEXA code to the next transceiver
  98. char strCode[20];
  99. sprintf(strCode,"<NT%08lX>",(unsigned long int)data);
  100. forwardNEXAMessage(strCode);
  101. vTaskDelay(200 / portTICK_PERIOD_MS); // Wait a while between transmits
  102. }
  103. }
  104. vTaskDelete(NULL);
  105. }
  106. void initNexaTransmit() {
  107. nexaTxQueue = xQueueCreate( 20, sizeof( uint32_t ) );
  108. xTaskCreatePinnedToCore(&nexaTxTask, "nexaTxTask", 8192, NULL, tskIDLE_PRIORITY + 10, NULL, 1);
  109. }