This is the first rev of HW3 for IDD

Dependencies:   MMA8451Q USBDevice mbed nRF24L01P

Committer:
antoniorohit
Date:
Mon Sep 29 09:15:37 2014 +0000
Revision:
1:b58cf7dd20d7
Parent:
0:d89cdf3d29f8
Child:
2:c3b748d86642
Update:; - Removed LED code for lower power; - Added 'roll' functionality, but not fully implemented ;

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