This is the first rev of HW3 for IDD

Dependencies:   MMA8451Q USBDevice mbed nRF24L01P

Committer:
antoniorohit
Date:
Mon Sep 29 08:45:58 2014 +0000
Revision:
0:d89cdf3d29f8
Child:
1:b58cf7dd20d7
First rev of HW3 for Interactive Device Design:; A game controller for use with the super cool steam game Blade Symphony

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antoniorohit 0:d89cdf3d29f8 1 //NOTE: NORDIC BOARD ONLY WORKS W/ MBED 84!
antoniorohit 0:d89cdf3d29f8 2 //Chose pin ordering from https://mbed.org/questions/1360/Using-nRF24L01-Module-with-FRDM-Board/ -- unused pins
antoniorohit 0:d89cdf3d29f8 3
antoniorohit 0:d89cdf3d29f8 4 #include "mbed.h"
antoniorohit 0:d89cdf3d29f8 5 #include "nRF24L01P.h" // nordic library
antoniorohit 0:d89cdf3d29f8 6
antoniorohit 0:d89cdf3d29f8 7
antoniorohit 0:d89cdf3d29f8 8 #define DEBUG 0
antoniorohit 0:d89cdf3d29f8 9 #define BASE 0
antoniorohit 0:d89cdf3d29f8 10
antoniorohit 0:d89cdf3d29f8 11
antoniorohit 0:d89cdf3d29f8 12 #include "USBMouseKeyboard.h" // for the sword - mouse/keyboard combo
antoniorohit 0:d89cdf3d29f8 13 #if(BASE == 1)
antoniorohit 0:d89cdf3d29f8 14 USBMouseKeyboard MK; //Default is REL_MOUSE, bout could use ABS_MOUSE too
antoniorohit 0:d89cdf3d29f8 15 #endif
antoniorohit 0:d89cdf3d29f8 16
antoniorohit 0:d89cdf3d29f8 17 #define SCALING 50 // factor to multiply the accelerometer reading by (usually it is in the scale of g's. Decides sensitivity of mouse. Keep it less 100 (mouse is assigned an int8)
antoniorohit 0:d89cdf3d29f8 18
antoniorohit 0:d89cdf3d29f8 19 // Accelerometer includes
antoniorohit 0:d89cdf3d29f8 20 #include "MMA8451Q.h"
antoniorohit 0:d89cdf3d29f8 21 #define MMA8451_I2C_ADDRESS (0x1d<<1)
antoniorohit 0:d89cdf3d29f8 22
antoniorohit 0:d89cdf3d29f8 23 // define I2C Pins and address for KL25Z. Taken from default sample code.
antoniorohit 0:d89cdf3d29f8 24 PinName const SDA = PTE25;
antoniorohit 0:d89cdf3d29f8 25 PinName const SCL = PTE24;
antoniorohit 0:d89cdf3d29f8 26
antoniorohit 0:d89cdf3d29f8 27 // Base station TX/RX
antoniorohit 0:d89cdf3d29f8 28 #define RX_NRF24L01P_ADDRESS ((unsigned long long) 0xABABABABAB )
antoniorohit 0:d89cdf3d29f8 29 #define TX_NRF24L01P_ADDRESS ((unsigned long long) 0xCDCDCDCDCD )
antoniorohit 0:d89cdf3d29f8 30
antoniorohit 0:d89cdf3d29f8 31 // Masks
antoniorohit 0:d89cdf3d29f8 32 #define LOW8 0x00FF
antoniorohit 0:d89cdf3d29f8 33 #define HIGH3 0x0007
antoniorohit 0:d89cdf3d29f8 34 #define LOW5 0x001F
antoniorohit 0:d89cdf3d29f8 35 #define HIGH6 0x003F
antoniorohit 0:d89cdf3d29f8 36
antoniorohit 0:d89cdf3d29f8 37 // The nRF24L01+ supports transfers from 1 to 32 bytes
antoniorohit 0:d89cdf3d29f8 38 // Assume 1680x1050 max screen resolution -- MSB to LSB 0 padded, 1 bit pressed, 11 bits x, 11 bits y for Fruit Ninja
antoniorohit 0:d89cdf3d29f8 39 // For Blade Symphony, way fewer than 24 control bits for keyboard/house
antoniorohit 0:d89cdf3d29f8 40 // From PC, only need (zero padded) 1 bit flag indicating if hit + 1 bit indicating Fruit Ninja (0) or Blade Symphony (1)
antoniorohit 0:d89cdf3d29f8 41 #define TRANSFER_SIZE 3
antoniorohit 0:d89cdf3d29f8 42
antoniorohit 0:d89cdf3d29f8 43 Serial pc(USBTX, USBRX); // PC communication
antoniorohit 0:d89cdf3d29f8 44
antoniorohit 0:d89cdf3d29f8 45 PwmOut motor(D2); // Only specific pins have PWM capability
antoniorohit 0:d89cdf3d29f8 46
antoniorohit 0:d89cdf3d29f8 47 nRF24L01P nordic(PTD2, PTD3, PTC5, PTD0, PTD5, PTA13); // mosi, miso, sck, csn, ce, irq
antoniorohit 0:d89cdf3d29f8 48
antoniorohit 0:d89cdf3d29f8 49 DigitalIn modeSW(D15); // base station or sword mode
antoniorohit 0:d89cdf3d29f8 50
antoniorohit 0:d89cdf3d29f8 51 // Accelerometer
antoniorohit 0:d89cdf3d29f8 52 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
antoniorohit 0:d89cdf3d29f8 53
antoniorohit 0:d89cdf3d29f8 54 void Calibrate(void);
antoniorohit 0:d89cdf3d29f8 55 void Acc_GetXY(void);
antoniorohit 0:d89cdf3d29f8 56
antoniorohit 0:d89cdf3d29f8 57 int16_t x,y; // variables to hold acceleration data after call to Acc_Get_All
antoniorohit 0:d89cdf3d29f8 58 float x_b, y_b; // acc bias
antoniorohit 0:d89cdf3d29f8 59
antoniorohit 0:d89cdf3d29f8 60 // Debug
antoniorohit 0:d89cdf3d29f8 61 DigitalOut greenLED(LED_GREEN);
antoniorohit 0:d89cdf3d29f8 62 DigitalOut redLED(LED_RED);
antoniorohit 0:d89cdf3d29f8 63
antoniorohit 0:d89cdf3d29f8 64
antoniorohit 0:d89cdf3d29f8 65 AnalogIn ATTACK(A0);
antoniorohit 0:d89cdf3d29f8 66 AnalogIn SELECT(A1);
antoniorohit 0:d89cdf3d29f8 67 AnalogIn XJOY(A2);
antoniorohit 0:d89cdf3d29f8 68 AnalogIn YJOY(A3);
antoniorohit 0:d89cdf3d29f8 69
antoniorohit 0:d89cdf3d29f8 70 DigitalIn JOYSEL(D3);
antoniorohit 0:d89cdf3d29f8 71
antoniorohit 0:d89cdf3d29f8 72
antoniorohit 0:d89cdf3d29f8 73 bool isBaseStation;
antoniorohit 0:d89cdf3d29f8 74
antoniorohit 0:d89cdf3d29f8 75 int main() {
antoniorohit 0:d89cdf3d29f8 76 int8_t mouse_x, mouse_y;
antoniorohit 0:d89cdf3d29f8 77 Calibrate();
antoniorohit 0:d89cdf3d29f8 78
antoniorohit 0:d89cdf3d29f8 79 modeSW.mode(PullUp); // Configure pull down to minimize wire
antoniorohit 0:d89cdf3d29f8 80
antoniorohit 0:d89cdf3d29f8 81 isBaseStation = (bool) !modeSW; // Detect device via jumper connection
antoniorohit 0:d89cdf3d29f8 82 wait(5);
antoniorohit 0:d89cdf3d29f8 83
antoniorohit 0:d89cdf3d29f8 84 // Power up wireless
antoniorohit 0:d89cdf3d29f8 85 nordic.powerUp();
antoniorohit 0:d89cdf3d29f8 86
antoniorohit 0:d89cdf3d29f8 87 // Display + change the (default) setup of the nRF24L01+ chip
antoniorohit 0:d89cdf3d29f8 88
antoniorohit 0:d89cdf3d29f8 89 // Addresses 5 bytes long
antoniorohit 0:d89cdf3d29f8 90
antoniorohit 0:d89cdf3d29f8 91 if (isBaseStation){
antoniorohit 0:d89cdf3d29f8 92 nordic.setTxAddress(TX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 93 nordic.setRxAddress(RX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 94 }
antoniorohit 0:d89cdf3d29f8 95 else{
antoniorohit 0:d89cdf3d29f8 96 nordic.setRxAddress(TX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 97 nordic.setTxAddress(RX_NRF24L01P_ADDRESS ,5);
antoniorohit 0:d89cdf3d29f8 98 }
antoniorohit 0:d89cdf3d29f8 99
antoniorohit 0:d89cdf3d29f8 100 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", nordic.getRfFrequency() );
antoniorohit 0:d89cdf3d29f8 101 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", nordic.getRfOutputPower() );
antoniorohit 0:d89cdf3d29f8 102 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", nordic.getAirDataRate() ); // 1Mbps
antoniorohit 0:d89cdf3d29f8 103 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", nordic.getTxAddress() );
antoniorohit 0:d89cdf3d29f8 104 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", nordic.getRxAddress() );
antoniorohit 0:d89cdf3d29f8 105
antoniorohit 0:d89cdf3d29f8 106 // Data packet length
antoniorohit 0:d89cdf3d29f8 107 nordic.setTransferSize( TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 108
antoniorohit 0:d89cdf3d29f8 109 nordic.setReceiveMode();
antoniorohit 0:d89cdf3d29f8 110 nordic.enable();
antoniorohit 0:d89cdf3d29f8 111
antoniorohit 0:d89cdf3d29f8 112 // Set motor PWM period
antoniorohit 0:d89cdf3d29f8 113 motor.period(0.001f); // 1ms period
antoniorohit 0:d89cdf3d29f8 114 motor.write(0.0f); // initially not on
antoniorohit 0:d89cdf3d29f8 115
antoniorohit 0:d89cdf3d29f8 116 // motor.write(0.40f); // 95% duty cycle, relative to period
antoniorohit 0:d89cdf3d29f8 117
antoniorohit 0:d89cdf3d29f8 118 // Status flags
antoniorohit 0:d89cdf3d29f8 119 bool isBladeSymphony = false;
antoniorohit 0:d89cdf3d29f8 120 bool sensor1ON = false;
antoniorohit 0:d89cdf3d29f8 121
antoniorohit 0:d89cdf3d29f8 122 bool motorON = false;
antoniorohit 0:d89cdf3d29f8 123
antoniorohit 0:d89cdf3d29f8 124 int rxDataCnt = 0;
antoniorohit 0:d89cdf3d29f8 125
antoniorohit 0:d89cdf3d29f8 126 // Debug
antoniorohit 0:d89cdf3d29f8 127 int testcount = 0;
antoniorohit 0:d89cdf3d29f8 128
antoniorohit 0:d89cdf3d29f8 129 // counting the mode
antoniorohit 0:d89cdf3d29f8 130 uint8_t mode_count = 0;
antoniorohit 0:d89cdf3d29f8 131
antoniorohit 0:d89cdf3d29f8 132 while (1) {
antoniorohit 0:d89cdf3d29f8 133 // Only reads 1 byte from PC + sends to other mcu (pads w/ 0 bytes) if base station
antoniorohit 0:d89cdf3d29f8 134 if (isBaseStation){
antoniorohit 0:d89cdf3d29f8 135
antoniorohit 0:d89cdf3d29f8 136 // If we've received anything over the host serial link...
antoniorohit 0:d89cdf3d29f8 137 if ( pc.readable() ) {
antoniorohit 0:d89cdf3d29f8 138 char txData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 139 // ...add it to the transmit buffer -- only care about first byte
antoniorohit 0:d89cdf3d29f8 140 txData[0] = pc.getc();
antoniorohit 0:d89cdf3d29f8 141 txData[1] = 100;
antoniorohit 0:d89cdf3d29f8 142 txData[2] = '\n';
antoniorohit 0:d89cdf3d29f8 143
antoniorohit 0:d89cdf3d29f8 144 // Send the transmitbuffer via the nRF24L01+
antoniorohit 0:d89cdf3d29f8 145 nordic.write( NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 146
antoniorohit 0:d89cdf3d29f8 147 // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
antoniorohit 0:d89cdf3d29f8 148 /*if (txData[0] == 66)
antoniorohit 0:d89cdf3d29f8 149 greenLED = !greenLED;*/
antoniorohit 0:d89cdf3d29f8 150 }
antoniorohit 0:d89cdf3d29f8 151
antoniorohit 0:d89cdf3d29f8 152 // If we've received anything in the nRF24L01+... = sword
antoniorohit 0:d89cdf3d29f8 153 if ( nordic.readable() ) {
antoniorohit 0:d89cdf3d29f8 154 char rxData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 155 // ...read the data into the receive buffer
antoniorohit 0:d89cdf3d29f8 156 rxDataCnt = nordic.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 157
antoniorohit 0:d89cdf3d29f8 158 //NOTE: NEED TO INTERPRET AS KEYS/MOUSE HERE
antoniorohit 0:d89cdf3d29f8 159
antoniorohit 0:d89cdf3d29f8 160 int8_t dx = rxData[0];
antoniorohit 0:d89cdf3d29f8 161 int8_t dy = rxData[1];
antoniorohit 0:d89cdf3d29f8 162
antoniorohit 0:d89cdf3d29f8 163
antoniorohit 0:d89cdf3d29f8 164 bool mode_stat = (rxData[2] >> 7) & 1;
antoniorohit 0:d89cdf3d29f8 165 bool joy_but = (rxData[2] >> 6) & 1;
antoniorohit 0:d89cdf3d29f8 166 bool sel_stat = (rxData[2] >> 5) & 1;
antoniorohit 0:d89cdf3d29f8 167 bool attack_stat = (rxData[2] >> 4) & 1;
antoniorohit 0:d89cdf3d29f8 168 bool key_w = (rxData[2] >> 3) & 1;
antoniorohit 0:d89cdf3d29f8 169 bool key_s = (rxData[2] >> 2) & 1;
antoniorohit 0:d89cdf3d29f8 170 bool key_d = (rxData[2] >> 1) & 1;
antoniorohit 0:d89cdf3d29f8 171 bool key_a = (rxData[2] >> 0) & 1;
antoniorohit 0:d89cdf3d29f8 172
antoniorohit 0:d89cdf3d29f8 173 #if(BASE == 1)
antoniorohit 0:d89cdf3d29f8 174 MK.move(-dx, dy);
antoniorohit 0:d89cdf3d29f8 175
antoniorohit 0:d89cdf3d29f8 176 if(joy_but)
antoniorohit 0:d89cdf3d29f8 177 {
antoniorohit 0:d89cdf3d29f8 178 MK.putc(' ');
antoniorohit 0:d89cdf3d29f8 179 }
antoniorohit 0:d89cdf3d29f8 180 if(key_w)
antoniorohit 0:d89cdf3d29f8 181 {
antoniorohit 0:d89cdf3d29f8 182 MK.putc('w');
antoniorohit 0:d89cdf3d29f8 183 }
antoniorohit 0:d89cdf3d29f8 184 if(key_a)
antoniorohit 0:d89cdf3d29f8 185 {
antoniorohit 0:d89cdf3d29f8 186 MK.putc('a');
antoniorohit 0:d89cdf3d29f8 187 }
antoniorohit 0:d89cdf3d29f8 188 if(key_s)
antoniorohit 0:d89cdf3d29f8 189 {
antoniorohit 0:d89cdf3d29f8 190 MK.putc('s');
antoniorohit 0:d89cdf3d29f8 191 }
antoniorohit 0:d89cdf3d29f8 192 if(key_d)
antoniorohit 0:d89cdf3d29f8 193 {
antoniorohit 0:d89cdf3d29f8 194 MK.putc('d');
antoniorohit 0:d89cdf3d29f8 195 }
antoniorohit 0:d89cdf3d29f8 196 if(sel_stat)
antoniorohit 0:d89cdf3d29f8 197 {
antoniorohit 0:d89cdf3d29f8 198 MK.click(MOUSE_RIGHT);
antoniorohit 0:d89cdf3d29f8 199 }
antoniorohit 0:d89cdf3d29f8 200 if(attack_stat)
antoniorohit 0:d89cdf3d29f8 201 {
antoniorohit 0:d89cdf3d29f8 202 MK.click(MOUSE_LEFT);
antoniorohit 0:d89cdf3d29f8 203 }
antoniorohit 0:d89cdf3d29f8 204 if(mode_stat)
antoniorohit 0:d89cdf3d29f8 205 {
antoniorohit 0:d89cdf3d29f8 206 MK.putc('1'+mode_count);
antoniorohit 0:d89cdf3d29f8 207 mode_count = (mode_count+1)%3;
antoniorohit 0:d89cdf3d29f8 208 }
antoniorohit 0:d89cdf3d29f8 209
antoniorohit 0:d89cdf3d29f8 210
antoniorohit 0:d89cdf3d29f8 211 #endif
antoniorohit 0:d89cdf3d29f8 212
antoniorohit 0:d89cdf3d29f8 213
antoniorohit 0:d89cdf3d29f8 214 pc.printf("x: %d y: %d \r\n",dx,dy);
antoniorohit 0:d89cdf3d29f8 215
antoniorohit 0:d89cdf3d29f8 216 // Display the receive buffer contents via the host serial link
antoniorohit 0:d89cdf3d29f8 217 /*for ( int i = 0; i < TRANSFER_SIZE; i++ ) {
antoniorohit 0:d89cdf3d29f8 218
antoniorohit 0:d89cdf3d29f8 219 pc.putc( rxData[i] );
antoniorohit 0:d89cdf3d29f8 220 }*/
antoniorohit 0:d89cdf3d29f8 221
antoniorohit 0:d89cdf3d29f8 222 // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
antoniorohit 0:d89cdf3d29f8 223 if (rxData[0] == 65 && rxData[1] == 66 && rxData[2] == 67){
antoniorohit 0:d89cdf3d29f8 224 redLED = !redLED;
antoniorohit 0:d89cdf3d29f8 225 }
antoniorohit 0:d89cdf3d29f8 226
antoniorohit 0:d89cdf3d29f8 227 }
antoniorohit 0:d89cdf3d29f8 228 }
antoniorohit 0:d89cdf3d29f8 229
antoniorohit 0:d89cdf3d29f8 230
antoniorohit 0:d89cdf3d29f8 231 // sword
antoniorohit 0:d89cdf3d29f8 232 else{
antoniorohit 0:d89cdf3d29f8 233 int ATTACKVAL = ATTACK.read_u16();
antoniorohit 0:d89cdf3d29f8 234 int SELECTVAL = SELECT.read_u16();
antoniorohit 0:d89cdf3d29f8 235 int XJOYVAL = XJOY.read_u16();
antoniorohit 0:d89cdf3d29f8 236 int YJOYVAL = YJOY.read_u16();
antoniorohit 0:d89cdf3d29f8 237 int JOYSELVAL = (int) JOYSEL;
antoniorohit 0:d89cdf3d29f8 238
antoniorohit 0:d89cdf3d29f8 239 bool mode_stat = (SELECTVAL > 45000) ? true: 0;
antoniorohit 0:d89cdf3d29f8 240 bool joy_but = JOYSEL == 0? true: 0;
antoniorohit 0:d89cdf3d29f8 241 bool sel_stat = (SELECTVAL > 10000 && SELECTVAL < 45000) ? true: 0;
antoniorohit 0:d89cdf3d29f8 242 bool attack_stat = ATTACKVAL > 10000? true: 0;
antoniorohit 0:d89cdf3d29f8 243 bool key_d = XJOYVAL > 35000 ? true: 0;
antoniorohit 0:d89cdf3d29f8 244 bool key_a = XJOYVAL < 30000 ? true : 0;
antoniorohit 0:d89cdf3d29f8 245 bool key_w = YJOYVAL > 35000 ? true: 0;
antoniorohit 0:d89cdf3d29f8 246 bool key_s = YJOYVAL < 30000 ? true: 0;
antoniorohit 0:d89cdf3d29f8 247
antoniorohit 0:d89cdf3d29f8 248 uint8_t key_stat = mode_stat<<7|joy_but<<6|sel_stat<<5|attack_stat<<4|key_w<<3|key_s<<2|key_d<<1|key_a<<0;
antoniorohit 0:d89cdf3d29f8 249
antoniorohit 0:d89cdf3d29f8 250
antoniorohit 0:d89cdf3d29f8 251 pc.printf("F1: %d \t F2: %d \t X: %d \t Y: %d \t SEL: %d \r\n", ATTACKVAL, SELECTVAL, XJOYVAL, YJOYVAL, JOYSELVAL);
antoniorohit 0:d89cdf3d29f8 252
antoniorohit 0:d89cdf3d29f8 253 greenLED = JOYSEL;
antoniorohit 0:d89cdf3d29f8 254
antoniorohit 0:d89cdf3d29f8 255 // Let serial read catch up on base station/PC side
antoniorohit 0:d89cdf3d29f8 256 wait_us(150);
antoniorohit 0:d89cdf3d29f8 257
antoniorohit 0:d89cdf3d29f8 258 char swordData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 259
antoniorohit 0:d89cdf3d29f8 260 int16_t deltax = 768;
antoniorohit 0:d89cdf3d29f8 261 int16_t deltay = 345;
antoniorohit 0:d89cdf3d29f8 262
antoniorohit 0:d89cdf3d29f8 263 if (testcount <= 2000){
antoniorohit 0:d89cdf3d29f8 264 deltax = testcount;
antoniorohit 0:d89cdf3d29f8 265 deltay = 2000-testcount;
antoniorohit 0:d89cdf3d29f8 266 testcount++;
antoniorohit 0:d89cdf3d29f8 267 }
antoniorohit 0:d89cdf3d29f8 268 else
antoniorohit 0:d89cdf3d29f8 269 testcount = 0;
antoniorohit 0:d89cdf3d29f8 270
antoniorohit 0:d89cdf3d29f8 271 Acc_GetXY();
antoniorohit 0:d89cdf3d29f8 272
antoniorohit 0:d89cdf3d29f8 273 mouse_x = x;
antoniorohit 0:d89cdf3d29f8 274 mouse_y = y;
antoniorohit 0:d89cdf3d29f8 275
antoniorohit 0:d89cdf3d29f8 276 char lowX = char(mouse_x & LOW8);
antoniorohit 0:d89cdf3d29f8 277 char lowY = char(mouse_y & LOW8);
antoniorohit 0:d89cdf3d29f8 278
antoniorohit 0:d89cdf3d29f8 279 // left A, right D, back S, up W
antoniorohit 0:d89cdf3d29f8 280
antoniorohit 0:d89cdf3d29f8 281 // MSB -- LSB,
antoniorohit 0:d89cdf3d29f8 282 swordData[0] = lowX;
antoniorohit 0:d89cdf3d29f8 283 swordData[1] = lowY;
antoniorohit 0:d89cdf3d29f8 284 swordData[2] = char(key_stat);
antoniorohit 0:d89cdf3d29f8 285 swordData[3] = char((sensor1ON << 7)|key_stat);
antoniorohit 0:d89cdf3d29f8 286
antoniorohit 0:d89cdf3d29f8 287 // Send the transmitbuffer via the nRF24L01+
antoniorohit 0:d89cdf3d29f8 288 nordic.write( NRF24L01P_PIPE_P0, swordData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 289
antoniorohit 0:d89cdf3d29f8 290 // NOTE: Action settings depending on type of game. Accelerometer data. Force sensitive resistor thresholding, etc.
antoniorohit 0:d89cdf3d29f8 291
antoniorohit 0:d89cdf3d29f8 292 // If we've received anything from base station
antoniorohit 0:d89cdf3d29f8 293
antoniorohit 0:d89cdf3d29f8 294 if ( nordic.readable() ) {
antoniorohit 0:d89cdf3d29f8 295 char rxData[TRANSFER_SIZE];
antoniorohit 0:d89cdf3d29f8 296 // ...read the data into the receive buffer
antoniorohit 0:d89cdf3d29f8 297 rxDataCnt = nordic.read( NRF24L01P_PIPE_P0, rxData, TRANSFER_SIZE );
antoniorohit 0:d89cdf3d29f8 298
antoniorohit 0:d89cdf3d29f8 299 #if DEBUG == 1
antoniorohit 0:d89cdf3d29f8 300 // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
antoniorohit 0:d89cdf3d29f8 301 pc.printf("x: %f \t y: %f \r\n", mouse_x,mouse_y);
antoniorohit 0:d89cdf3d29f8 302 greenLED = !greenLED;
antoniorohit 0:d89cdf3d29f8 303
antoniorohit 0:d89cdf3d29f8 304 // Display the receive buffer contents via the host serial link
antoniorohit 0:d89cdf3d29f8 305 for ( int i = 0; i < TRANSFER_SIZE; i++ ) {
antoniorohit 0:d89cdf3d29f8 306
antoniorohit 0:d89cdf3d29f8 307 pc.putc( rxData[i] );
antoniorohit 0:d89cdf3d29f8 308 }
antoniorohit 0:d89cdf3d29f8 309
antoniorohit 0:d89cdf3d29f8 310 // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
antoniorohit 0:d89cdf3d29f8 311 if (rxData[0] == 65 && rxData[1] == 100 && rxData[2] == 100){
antoniorohit 0:d89cdf3d29f8 312 redLED = !redLED;
antoniorohit 0:d89cdf3d29f8 313 }
antoniorohit 0:d89cdf3d29f8 314 #endif
antoniorohit 0:d89cdf3d29f8 315
antoniorohit 0:d89cdf3d29f8 316
antoniorohit 0:d89cdf3d29f8 317 // From PC, only need (zero padded) 1 bit flag indicating if hit + 1 bit indicating Fruit Ninja (0) or Blade Symphony (1)
antoniorohit 0:d89cdf3d29f8 318 // In first byte
antoniorohit 0:d89cdf3d29f8 319 isBladeSymphony = (rxData[0] >> 0) & 1;
antoniorohit 0:d89cdf3d29f8 320 motorON = (rxData[0] >> 1) & 1; // Motor ON when sword contact made
antoniorohit 0:d89cdf3d29f8 321
antoniorohit 0:d89cdf3d29f8 322 greenLED = !isBladeSymphony; // LEDs active low
antoniorohit 0:d89cdf3d29f8 323 redLED = !motorON;
antoniorohit 0:d89cdf3d29f8 324
antoniorohit 0:d89cdf3d29f8 325 }
antoniorohit 0:d89cdf3d29f8 326 }
antoniorohit 0:d89cdf3d29f8 327 }
antoniorohit 0:d89cdf3d29f8 328 }
antoniorohit 0:d89cdf3d29f8 329
antoniorohit 0:d89cdf3d29f8 330 void Calibrate(void)
antoniorohit 0:d89cdf3d29f8 331 {
antoniorohit 0:d89cdf3d29f8 332 unsigned int count1;
antoniorohit 0:d89cdf3d29f8 333 count1 = 0;
antoniorohit 0:d89cdf3d29f8 334 float sstatex = 0;
antoniorohit 0:d89cdf3d29f8 335 float sstatey = 0;
antoniorohit 0:d89cdf3d29f8 336
antoniorohit 0:d89cdf3d29f8 337 do{
antoniorohit 0:d89cdf3d29f8 338 sstatex += acc.getAccX(); // Accumulate Samples
antoniorohit 0:d89cdf3d29f8 339 sstatey += acc.getAccY();
antoniorohit 0:d89cdf3d29f8 340 count1++;
antoniorohit 0:d89cdf3d29f8 341 }while(count1!=0x0400); // 1024 times
antoniorohit 0:d89cdf3d29f8 342 x_b = sstatex/1024.0; // division between 1024
antoniorohit 0:d89cdf3d29f8 343 y_b = sstatey/1024.0;
antoniorohit 0:d89cdf3d29f8 344 }
antoniorohit 0:d89cdf3d29f8 345
antoniorohit 0:d89cdf3d29f8 346 void Acc_GetXY(void)
antoniorohit 0:d89cdf3d29f8 347 {
antoniorohit 0:d89cdf3d29f8 348 x = (int16_t)((acc.getAccX()- x_b)*SCALING);
antoniorohit 0:d89cdf3d29f8 349 y = (int16_t)((acc.getAccY()- y_b)*SCALING);
antoniorohit 0:d89cdf3d29f8 350 }
antoniorohit 0:d89cdf3d29f8 351
antoniorohit 0:d89cdf3d29f8 352
antoniorohit 0:d89cdf3d29f8 353