Advertisement
y2kbug

Untitled

Oct 10th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #define AOK_DOWN "10100011101011101110000001011110000000010000000001000011001100001"
  2. #define AOK_UP "10100011101011101110000001011110000000010000000000001011111110001"
  3.  
  4. #define TRANSMIT_PIN 13 // We'll use digital 13 for transmitting
  5. #define REPEAT_COMMAND 8 // How many times to repeat the same command: original remotes repeat 8 (multi) or 10 (single) times by default
  6. #define DEBUG false // Do note that if you add serial output during transmit, it will cause delay and commands may fail
  7.  
  8. // If you wish to use PORTB commands instead of digitalWrite, these are for Arduino Uno digital 13:
  9. #define D13high | 0x20;
  10. #define D13low & 0xDF;
  11.  
  12. // Timings in microseconds (us). Get sample count by zooming all the way in to the waveform with Audacity.
  13. // Calculate microseconds with: (samples / sample rate, usually 44100 or 48000) - ~15-20 to compensate for delayMicroseconds overhead.
  14. // Sample counts listed below with a sample rate of 44100 Hz:
  15. #define AOK_AGC1_PULSE 5300 // 234 samples
  16. #define AOK_AGC2_PULSE 530 // 24 samples after the actual AGC bit
  17. #define AOK_RADIO_SILENCE 5030 // 222 samples
  18.  
  19. #define AOK_PULSE_SHORT 270 // 12 samples
  20. #define AOK_PULSE_LONG 565 // 25 samples, approx. 2 * AOK_PULSE_SHORT
  21.  
  22. #define AOK_COMMAND_BIT_ARRAY_SIZE 65 // Command bit count
  23.  
  24. const byte numChars = 32;
  25. char receivedChars[numChars];
  26. boolean newData = false;
  27.  
  28. void setup() {
  29. Serial.begin(9600);
  30. }
  31.  
  32. void loop() {
  33. recvWithEndMarker();
  34. showNewData();
  35. delay(1000);
  36. }
  37.  
  38. void recvWithEndMarker() {
  39. static byte ndx = 0;
  40. char endMarker = '\n';
  41. char rc;
  42.  
  43. while (Serial.available() > 0 && newData == false) {
  44. rc = Serial.read();
  45.  
  46. if (rc != endMarker) {
  47. receivedChars[ndx] = rc;
  48. ndx++;
  49. if (ndx >= numChars) {
  50. ndx = numChars - 1;
  51. }
  52. } else {
  53. receivedChars[ndx] = '\0';
  54. ndx = 0;
  55. newData = true;
  56. }
  57. }
  58. }
  59.  
  60. void showNewData() {
  61. if (newData == true) {
  62. newData = false;
  63.  
  64. Serial.println("This just in ... ");
  65. Serial.println(receivedChars);
  66.  
  67. if (strcmp(receivedChars, "screen_up") == 0) {
  68. sendAOKCommand(AOK_UP);
  69. } else if (strcmp(receivedChars, "screen_down") == 0) {
  70. sendAOKCommand(AOK_DOWN);
  71. }
  72. }
  73. }
  74.  
  75. void sendAOKCommand(char* command) {
  76.  
  77. if (command == NULL) {
  78. errorLog("sendAOKCommand(): Command array pointer was NULL, cannot continue.");
  79. return;
  80. }
  81.  
  82. // Prepare for transmitting and check for validity
  83. pinMode(TRANSMIT_PIN, OUTPUT); // Prepare the digital pin for output
  84.  
  85. if (strlen(command) < AOK_COMMAND_BIT_ARRAY_SIZE) {
  86. errorLog("sendAOKCommand(): Invalid command (too short), cannot continue.");
  87. return;
  88. }
  89. if (strlen(command) > AOK_COMMAND_BIT_ARRAY_SIZE) {
  90. errorLog("sendAOKCommand(): Invalid command (too long), cannot continue.");
  91. return;
  92. }
  93.  
  94. // Repeat the command:
  95. for (int i = 0; i < REPEAT_COMMAND; i++) {
  96. doAOKTribitSend(command);
  97. }
  98.  
  99. // Disable output to transmitter to prevent interference with
  100. // other devices. Otherwise the transmitter will keep on transmitting,
  101. // disrupting most appliances operating on the 433.92MHz band:
  102. digitalWrite(TRANSMIT_PIN, LOW);
  103. }
  104.  
  105. void doAOKTribitSend(char* command) {
  106.  
  107. // Starting (AGC) bits:
  108. transmitHigh(AOK_AGC1_PULSE);
  109. transmitLow(AOK_AGC2_PULSE);
  110.  
  111. // Transmit command:
  112. for (int i = 0; i < AOK_COMMAND_BIT_ARRAY_SIZE; i++) {
  113.  
  114. // If current bit is 0, transmit HIGH-LOW-LOW (100):
  115. if (command[i] == '0') {
  116. transmitHigh(AOK_PULSE_SHORT);
  117. transmitLow(AOK_PULSE_LONG);
  118. }
  119.  
  120. // If current bit is 1, transmit HIGH-HIGH-LOW (110):
  121. if (command[i] == '1') {
  122. transmitHigh(AOK_PULSE_LONG);
  123. transmitLow(AOK_PULSE_SHORT);
  124. }
  125. }
  126.  
  127. // Radio silence at the end.
  128. // It's better to go a bit over than under minimum required length:
  129. transmitLow(AOK_RADIO_SILENCE);
  130.  
  131. if (DEBUG) {
  132. Serial.println();
  133. Serial.print("Transmitted ");
  134. Serial.print(AOK_COMMAND_BIT_ARRAY_SIZE);
  135. Serial.println(" bits.");
  136. Serial.println();
  137. }
  138. }
  139.  
  140. void transmitHigh(int delay_microseconds) {
  141. digitalWrite(TRANSMIT_PIN, HIGH);
  142. //PORTB = PORTB D13high; // If you wish to use faster PORTB calls instead
  143. delayMicroseconds(delay_microseconds);
  144. }
  145.  
  146. void transmitLow(int delay_microseconds) {
  147. digitalWrite(TRANSMIT_PIN, LOW);
  148. //PORTB = PORTB D13low; // If you wish to use faster PORTB calls instead
  149. delayMicroseconds(delay_microseconds);
  150. }
  151.  
  152. void errorLog(String message) {
  153. Serial.println(message);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement