Advertisement
y2kbug

Untitled

Oct 10th, 2021
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   Serial.flush();
  31. }
  32.  
  33. void loop() {
  34.   recvWithEndMarker();
  35.   showNewData();
  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(receivedChars);
  65.  
  66.     if (strcmp(receivedChars, "screen_up") == 0) {
  67.       sendAOKCommand(AOK_UP);
  68.     } else if (strcmp(receivedChars, "screen_down") == 0) {
  69.       sendAOKCommand(AOK_DOWN);
  70.     }
  71.   }
  72. }
  73.  
  74. void sendAOKCommand(char* command) {
  75.  
  76.   if (command == NULL) {
  77.     errorLog("sendAOKCommand(): Command array pointer was NULL, cannot continue.");
  78.     return;
  79.   }
  80.  
  81.   // Prepare for transmitting and check for validity
  82.   pinMode(TRANSMIT_PIN, OUTPUT); // Prepare the digital pin for output
  83.  
  84.   if (strlen(command) < AOK_COMMAND_BIT_ARRAY_SIZE) {
  85.     errorLog("sendAOKCommand(): Invalid command (too short), cannot continue.");
  86.     return;
  87.   }
  88.   if (strlen(command) > AOK_COMMAND_BIT_ARRAY_SIZE) {
  89.     errorLog("sendAOKCommand(): Invalid command (too long), cannot continue.");
  90.     return;
  91.   }
  92.  
  93.   // Repeat the command:
  94.   for (int i = 0; i < REPEAT_COMMAND; i++) {
  95.     doAOKTribitSend(command);
  96.   }
  97.  
  98.   // Disable output to transmitter to prevent interference with
  99.   // other devices. Otherwise the transmitter will keep on transmitting,
  100.   // disrupting most appliances operating on the 433.92MHz band:
  101.   digitalWrite(TRANSMIT_PIN, LOW);
  102. }
  103.  
  104. void doAOKTribitSend(char* command) {
  105.  
  106.   // Starting (AGC) bits:
  107.   transmitHigh(AOK_AGC1_PULSE);
  108.   transmitLow(AOK_AGC2_PULSE);
  109.  
  110.   // Transmit command:
  111.   for (int i = 0; i < AOK_COMMAND_BIT_ARRAY_SIZE; i++) {
  112.  
  113.       // If current bit is 0, transmit HIGH-LOW-LOW (100):
  114.       if (command[i] == '0') {
  115.         transmitHigh(AOK_PULSE_SHORT);
  116.         transmitLow(AOK_PULSE_LONG);
  117.       }
  118.  
  119.       // If current bit is 1, transmit HIGH-HIGH-LOW (110):
  120.       if (command[i] == '1') {
  121.         transmitHigh(AOK_PULSE_LONG);
  122.         transmitLow(AOK_PULSE_SHORT);
  123.       }  
  124.    }
  125.  
  126.   // Radio silence at the end.
  127.   // It's better to go a bit over than under minimum required length:
  128.   transmitLow(AOK_RADIO_SILENCE);
  129.  
  130.   if (DEBUG) {
  131.     Serial.println();
  132.     Serial.print("Transmitted ");
  133.     Serial.print(AOK_COMMAND_BIT_ARRAY_SIZE);
  134.     Serial.println(" bits.");
  135.     Serial.println();
  136.   }
  137. }
  138.  
  139. void transmitHigh(int delay_microseconds) {
  140.   digitalWrite(TRANSMIT_PIN, HIGH);
  141.   //PORTB = PORTB D13high; // If you wish to use faster PORTB calls instead
  142.   delayMicroseconds(delay_microseconds);
  143. }
  144.  
  145. void transmitLow(int delay_microseconds) {
  146.   digitalWrite(TRANSMIT_PIN, LOW);
  147.   //PORTB = PORTB D13low; // If you wish to use faster PORTB calls instead
  148.   delayMicroseconds(delay_microseconds);
  149. }
  150.  
  151. void errorLog(String message) {
  152.   Serial.println(message);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement