skizziks_53

XBox RF thingy ? v1.0

Nov 3rd, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. /*
  2.   Reddit edit - 3 November 2019
  3.  
  4.   This is an edit of some code posted online that appeared to have an error in it.
  5.  
  6.   The source of the code is here: https://dilisilib.wordpress.com/hacking/xbox-360-rf-module-arduino/
  7.  
  8. */
  9.  
  10. /* Arduino code to communicate with xbox 360 RF module.
  11.   Original work by (yaywoop) / additional ideas from Alexander Martinez - modified by dilandou (www.dilandou.com, www.diru.org/wordpress)
  12.   First sends LED initialisation code followed by LED startup animation code, then sleeps until a button press for sync command.
  13.   RF module must be powered with 3.3V, two diodes in series with USB 5v will do. Connect the USB wires to a host computer, and the data and serial wires to Arduino.
  14.   of course, make sure to have a common ground */
  15.  
  16. #include <avr/sleep.h>
  17.  
  18. #define sync_pin 2 //power button repurposed for sync button (pin 5 on the module)
  19. #define data_pin 3 //data line (pin 6 on the module)
  20. #define clock_pin 4 //clock line (pin 7 on module)
  21.  
  22. int led_cmd[10] =  {0, 0, 1, 0, 0, 0, 0, 1, 0, 0}; //Activates/initialises the LEDs, leaving the center LED lit.
  23. int anim_cmd[10] = {0, 0, 1, 0, 0, 0, 0, 1, 0, 1}; //Makes the startup animation on the ring of light.
  24. int sync_cmd[10] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}; //Initiates the sync process.
  25. volatile boolean sync_enable = 0;
  26.  
  27. void sendData(int cmd_do[]) {
  28.   pinMode(data_pin, OUTPUT);
  29.   digitalWrite(data_pin, LOW);    //start sending data.
  30.   int prev = 1;
  31.   for (int i = 0; i < 10; i++) {
  32.     /*
  33.         while (prev == digitalRead(clock_pin)) {} //detects change in clock <--------------------------------------- What is this supposed to do???
  34.         prev = digitalRead(clock_pin);
  35.         // should be after downward edge of clock, so send bit of data now
  36.         digitalWrite(data_pin, cmd_do[i]);
  37.  
  38.         while (prev == digitalRead(clock_pin)) {} //detects upward edge of clock <--------------------------------------- What is this supposed to do???
  39.           prev = digitalRead(clock_pin);
  40.         }
  41.         digitalWrite(data_pin, HIGH);
  42.         pinMode(data_pin, INPUT);
  43.     */
  44.  
  45.     // Below is what I am guessing this was supposed to do.
  46.     // I don't have an xbox to test with however...
  47.  
  48.     while (prev != digitalRead(clock_pin)) { //detects change in clock
  49.       prev = digitalRead(clock_pin);
  50.       // should be after downward edge of clock, so send bit of data now
  51.       digitalWrite(data_pin, cmd_do[i]);
  52.     }
  53.     while (prev == digitalRead(clock_pin)) { //detects upward edge of clock
  54.       prev = digitalRead(clock_pin);
  55.       digitalWrite(data_pin, HIGH);
  56.       // pinMode(data_pin, INPUT); <------------------- and this is probably wrong here also.
  57.     }
  58.   }
  59.   pinMode(data_pin, INPUT); // <----------------------- looks better here, outside of the loop to send data...?
  60. }
  61.  
  62. void initLEDs() {
  63.   sendData(led_cmd);
  64.   delay(50);
  65.   sendData(anim_cmd);
  66.   delay(50);
  67. }
  68.  
  69. void wakeUp() {
  70.   sync_enable = 1;
  71. }
  72.  
  73. void sleepNow() {
  74.   set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set sleep mode
  75.   sleep_enable(); //enable sleep bit
  76.   attachInterrupt(0, wakeUp, LOW);
  77.   sleep_mode();
  78.   sleep_disable(); //disable sleep bit
  79.   detachInterrupt(0); // disables interrupt 0 on pin 2
  80. }
  81.  
  82. void setup() {
  83.   Serial.begin(9600);
  84.   pinMode(sync_pin, INPUT);
  85.   digitalWrite(sync_pin, HIGH);
  86.   pinMode(data_pin, INPUT);
  87.   pinMode(clock_pin, INPUT);
  88.   delay(2000);
  89.  
  90.   initLEDs();
  91.   //  sendData(sync_cmd);
  92. }
  93.  
  94. void loop() {
  95.   Serial.println("Sleeping.");
  96.   sleepNow();
  97.   delay(200);
  98.   if (sync_enable == 1) {
  99.     Serial.println("Syncing.");
  100.     sendData(sync_cmd);
  101.     sync_enable = 0;
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment