skizziks_53

Reddit xbox serial thingy v 2.0

Nov 4th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. /*
  2.   Reddit edit - 3 November 2019 - version 2.0
  3.  
  4.   This is an edit of some code posted online that appeared to have an error in it.
  5.  
  6.   The original 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.   /*
  31.     The section below is totally re-written, to send 10 data bits whenever the clock pin goes from low to high.
  32.   */
  33.   int clock_pin_change = 1; // This represents if the data will be sent on the rise or fall of the clock signal.
  34.   // 1 means send when the clock rises to 1, 0 means send when the clock pin falls to zero.
  35.   // You might need to try setting it both ways, to zero and 1.
  36.   int current_clock_pin_value = 0; // This should be set to the opposite value of clock_pin_change.
  37.   int previous_clock_pin_value = 0; // This should be set to the opposite value of clock_pin_change.
  38.   int bytes_to_send = 10;
  39.   int bytes_sent = 0;
  40.   while (bytes_sent < bytes_to_send) { // There is 10 bytes to send, so this keeps looping until 10 bytes get sent.
  41.     current_clock_pin_value = digitalRead(clock_pin);
  42.     if (current_clock_pin_value != previous_clock_pin_value) { // This requires that the clock pin state has changed.
  43.       if (current_clock_pin_value == clock_pin_change) {
  44.         digitalWrite(data_pin, cmd_do[bytes_sent]);
  45.         bytes_sent++;
  46.       }
  47.     }
  48.     previous_clock_pin_value = current_clock_pin_value;
  49.   }
  50.   pinMode(data_pin, INPUT);
  51. }
  52.  
  53. void initLEDs() {
  54.   sendData(led_cmd);
  55.   delay(50);
  56.   sendData(anim_cmd);
  57.   delay(50);
  58. }
  59.  
  60. void wakeUp() {
  61.   sync_enable = 1;
  62.   // This function has been changed{ the following two lines have been moved from sleepNow() to here.
  63.   sleep_disable(); //disable sleep bit
  64.   detachInterrupt(0);
  65. }
  66.  
  67. void sleepNow() {
  68.   // This sleep function is not the way the Arduino website shows-- https://playground.arduino.cc/Learning/ArduinoSleepCode/
  69.   /*
  70.     set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set sleep mode
  71.     sleep_enable(); //enable sleep bit
  72.     attachInterrupt(0, wakeUp, LOW);
  73.     sleep_mode();
  74.     sleep_disable(); //disable sleep bit --------------------------------------- I think this was supposed to go into the wakeUp function.
  75.     detachInterrupt(0); // disables interrupt 0 on pin 2 ----------------------- And this also.
  76.   */
  77.   // Below is what the website shows.
  78.   // The website notes that calling these in the correct order is necessary for them to all work correctly.
  79.   sleep_enable();
  80.   attachInterrupt(0, wakeUp, LOW);
  81.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  82.   cli();
  83.   sleep_bod_disable();
  84.   sei();
  85.   sleep_cpu();
  86. }
  87.  
  88. void setup() {
  89.   Serial.begin(9600);
  90.   pinMode(sync_pin, INPUT);
  91.   digitalWrite(sync_pin, HIGH);
  92.   pinMode(data_pin, INPUT);
  93.   pinMode(clock_pin, INPUT);
  94.   delay(2000);
  95.  
  96.   initLEDs();
  97.   //  sendData(sync_cmd);
  98. }
  99.  
  100. void loop() {
  101.   Serial.println("Sleeping.");
  102.   sleepNow();
  103.   delay(200);
  104.   if (sync_enable == 1) {
  105.     Serial.println("Syncing.");
  106.     sendData(sync_cmd);
  107.     sync_enable = 0;
  108.   }
  109. }
  110.  
  111.  
  112. // ~~~~~~~~ the end ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment