Wii Nunchuk via RFM69HW to Duplo 9203 Remote Control Car Kit using ARM mbed on a FRDM-KL25Z

Dependencies:   CRC FastPWM RFM69 USBDevice WakeUp WiiChuk_compat mbed-rtos mbed tlc59108

Fork of wiiNunchuk_compat by Greg Brush

Committer:
eisd
Date:
Tue Jun 28 10:33:10 2016 +0000
Revision:
14:579137c8ceac
Parent:
13:ab524cb4f768
Child:
15:8888e36afe43
Debug levels

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gbrush 0:7c98bcd8a245 1 #include "mbed.h"
eisd 5:7af5760d7e8f 2 #include "rtos.h"
eisd 13:ab524cb4f768 3 #include "WakeUp.h"
eisd 11:7c3fb795af73 4 #include "FastPWM.h"
eisd 1:de8c34c9ccdf 5 #ifndef M_PI
eisd 1:de8c34c9ccdf 6 #define M_PI 3.14159265358979323846
eisd 1:de8c34c9ccdf 7 #endif
eisd 1:de8c34c9ccdf 8
eisd 1:de8c34c9ccdf 9 #include "WiiChuk_compat.hpp"
eisd 7:f720032c2fb9 10 #include "lib_crc.h"
gbrush 0:7c98bcd8a245 11
eisd 2:04fdd571a385 12 //#include "USBSerial.h"
eisd 2:04fdd571a385 13
eisd 2:04fdd571a385 14 #include "RFM69.h"
eisd 2:04fdd571a385 15 #define GATEWAY_ID 2
eisd 2:04fdd571a385 16 #define NODE_ID 1
eisd 2:04fdd571a385 17 #define NETWORKID 100
gbrush 0:7c98bcd8a245 18
eisd 2:04fdd571a385 19 // Uncomment only one of the following three to match radio frequency
eisd 2:04fdd571a385 20 //#define FREQUENCY RF69_433MHZ
eisd 2:04fdd571a385 21 #define FREQUENCY RF69_868MHZ
eisd 2:04fdd571a385 22 //#define FREQUENCY RF69_915MHZ
eisd 2:04fdd571a385 23
eisd 3:9091adbed369 24 const char *directions[8] = { "XR", "RR", "RX", "FR", "FX", "FF", "XF", "RF" };
eisd 5:7af5760d7e8f 25
eisd 14:579137c8ceac 26 #define DEBUG 0
eisd 14:579137c8ceac 27 #ifdef DEBUG
eisd 5:7af5760d7e8f 28 #ifdef USBSerial
eisd 5:7af5760d7e8f 29 USBSerial pc;
eisd 5:7af5760d7e8f 30 #else
eisd 2:04fdd571a385 31 Serial pc(USBTX, USBRX);
eisd 1:de8c34c9ccdf 32 #endif
eisd 14:579137c8ceac 33 #else
eisd 14:579137c8ceac 34 class Null : public Stream {
eisd 14:579137c8ceac 35 public:
eisd 14:579137c8ceac 36 Null(): Stream("null") {}
eisd 14:579137c8ceac 37 void baud(int) {}
eisd 5:7af5760d7e8f 38
eisd 14:579137c8ceac 39 protected:
eisd 14:579137c8ceac 40 virtual int _getc() { return 0; }
eisd 14:579137c8ceac 41 virtual int _putc(int c) { return 0; }
eisd 14:579137c8ceac 42 };
eisd 14:579137c8ceac 43 Null pc;
eisd 14:579137c8ceac 44 #endif
eisd 5:7af5760d7e8f 45 #if TARGET_KL25Z
eisd 5:7af5760d7e8f 46 DigitalOut gnd(PTC0);
eisd 5:7af5760d7e8f 47 PwmOut ir(PTD4);
eisd 5:7af5760d7e8f 48 #endif
eisd 5:7af5760d7e8f 49 #define pulse(x, y) ir = 0.5; wait_us(x); ir = 0.0; wait_us(y);
eisd 5:7af5760d7e8f 50
eisd 5:7af5760d7e8f 51 /* generated in bash from lirc raw codes */
eisd 5:7af5760d7e8f 52 /*
eisd 5:7af5760d7e8f 53 f=duplo.conf
eisd 5:7af5760d7e8f 54 sed -n '/begin raw_codes/,/end raw_codes/s/name //p' $f|
eisd 5:7af5760d7e8f 55 while read n; do
eisd 5:7af5760d7e8f 56 echo -e "void $n() {$(
eisd 5:7af5760d7e8f 57 grep $n -A 1 $f|tail -n1|
eisd 5:7af5760d7e8f 58 sed 's/$/ 1000/;s@\([0-9]\+\)[[:space:]]\+\([0-9]\+\)[[:space:]]*@\n\tpulse(\1, \2);@g'
eisd 5:7af5760d7e8f 59 )\n}"
eisd 5:7af5760d7e8f 60 done
eisd 5:7af5760d7e8f 61 */
eisd 5:7af5760d7e8f 62
eisd 5:7af5760d7e8f 63 void FX() {
eisd 5:7af5760d7e8f 64 pulse(1042, 208);
eisd 5:7af5760d7e8f 65 pulse(208, 208);
eisd 5:7af5760d7e8f 66 pulse(208, 562);
eisd 5:7af5760d7e8f 67 pulse(625, 208);
eisd 5:7af5760d7e8f 68 pulse(208, 625);
eisd 5:7af5760d7e8f 69 pulse(208, 1);
eisd 5:7af5760d7e8f 70 }
eisd 5:7af5760d7e8f 71 void XF() {
eisd 5:7af5760d7e8f 72 pulse(229, 604);
eisd 5:7af5760d7e8f 73 pulse(229, 188);
eisd 5:7af5760d7e8f 74 pulse(229, 188);
eisd 5:7af5760d7e8f 75 pulse(438, 333);
eisd 5:7af5760d7e8f 76 pulse(625, 208);
eisd 5:7af5760d7e8f 77 pulse(1250, 1);
eisd 5:7af5760d7e8f 78 }
eisd 5:7af5760d7e8f 79 void FF() {
eisd 5:7af5760d7e8f 80 pulse(208, 625);
eisd 5:7af5760d7e8f 81 pulse(208, 208);
eisd 5:7af5760d7e8f 82 pulse(208, 208);
eisd 5:7af5760d7e8f 83 pulse(417, 354);
eisd 5:7af5760d7e8f 84 pulse(208, 208);
eisd 5:7af5760d7e8f 85 pulse(208, 1042);
eisd 5:7af5760d7e8f 86 pulse(208, 1);
eisd 5:7af5760d7e8f 87 }
eisd 5:7af5760d7e8f 88 void RX() {
eisd 5:7af5760d7e8f 89 pulse(1042, 208);
eisd 5:7af5760d7e8f 90 pulse(208, 188);
eisd 5:7af5760d7e8f 91 pulse(229, 542);
eisd 5:7af5760d7e8f 92 pulse(646, 1);
eisd 5:7af5760d7e8f 93 }
eisd 5:7af5760d7e8f 94 void XR() {
eisd 5:7af5760d7e8f 95 pulse(208, 1042);
eisd 5:7af5760d7e8f 96 pulse(208, 188);
eisd 5:7af5760d7e8f 97 pulse(229, 542);
eisd 5:7af5760d7e8f 98 pulse(625, 417);
eisd 5:7af5760d7e8f 99 pulse(854, 1);
eisd 5:7af5760d7e8f 100 }
eisd 5:7af5760d7e8f 101 void RR() {
eisd 5:7af5760d7e8f 102 pulse(229, 1021);
eisd 5:7af5760d7e8f 103 pulse(229, 208);
eisd 5:7af5760d7e8f 104 pulse(208, 542);
eisd 5:7af5760d7e8f 105 pulse(229, 188);
eisd 5:7af5760d7e8f 106 pulse(229, 1229);
eisd 5:7af5760d7e8f 107 pulse(229, 1);
eisd 5:7af5760d7e8f 108 }
eisd 5:7af5760d7e8f 109 void RF() {
eisd 5:7af5760d7e8f 110 pulse(208, 625);
eisd 5:7af5760d7e8f 111 pulse(208, 208);
eisd 5:7af5760d7e8f 112 pulse(208, 208);
eisd 5:7af5760d7e8f 113 pulse(417, 333);
eisd 5:7af5760d7e8f 114 pulse(208, 208);
eisd 5:7af5760d7e8f 115 pulse(208, 208);
eisd 5:7af5760d7e8f 116 pulse(208, 1);
eisd 5:7af5760d7e8f 117 }
eisd 5:7af5760d7e8f 118 void FR() {
eisd 5:7af5760d7e8f 119 pulse(229, 1021);
eisd 5:7af5760d7e8f 120 pulse(229, 188);
eisd 5:7af5760d7e8f 121 pulse(229, 542);
eisd 5:7af5760d7e8f 122 pulse(208, 208);
eisd 5:7af5760d7e8f 123 pulse(208, 208);
eisd 5:7af5760d7e8f 124 pulse(208, 625);
eisd 5:7af5760d7e8f 125 pulse(417, 1);
eisd 5:7af5760d7e8f 126 }
eisd 5:7af5760d7e8f 127 void BB() {
eisd 5:7af5760d7e8f 128 pulse(1042, 208);
eisd 5:7af5760d7e8f 129 pulse(208, 208);
eisd 5:7af5760d7e8f 130 pulse(208, 542);
eisd 5:7af5760d7e8f 131 pulse(208, 417);
eisd 5:7af5760d7e8f 132 pulse(208, 208);
eisd 5:7af5760d7e8f 133 pulse(1042, 1);
eisd 5:7af5760d7e8f 134 }
eisd 5:7af5760d7e8f 135
eisd 5:7af5760d7e8f 136 bool central = true;
eisd 12:eec17d54a646 137 Timer central_time;
eisd 7:f720032c2fb9 138 int stops_sent = 0;
eisd 5:7af5760d7e8f 139 int direction = -1;
eisd 5:7af5760d7e8f 140 Thread *thread;
eisd 5:7af5760d7e8f 141 void ir_thread(void const *args) {
eisd 5:7af5760d7e8f 142 while(1) {
eisd 5:7af5760d7e8f 143 //if (!central)
eisd 5:7af5760d7e8f 144 for(int x = 0; x < 5; x++) {
eisd 14:579137c8ceac 145 #if DEBUG
eisd 14:579137c8ceac 146 pc.printf(central? "stop %s %d\r\n" : "direction %s %d\r\n", directions[direction], stops_sent);
eisd 14:579137c8ceac 147 #endif
eisd 9:dc6ffa2ef179 148 if (central) {
eisd 10:ba778fc959b8 149 if (stops_sent < 50) {
eisd 10:ba778fc959b8 150 stops_sent++;
eisd 9:dc6ffa2ef179 151 BB();
eisd 10:ba778fc959b8 152 }
eisd 9:dc6ffa2ef179 153 } else {
eisd 7:f720032c2fb9 154 stops_sent = 0;
eisd 7:f720032c2fb9 155 switch(direction) {
eisd 7:f720032c2fb9 156 case 0: XR(); break;
eisd 7:f720032c2fb9 157 case 1: RR(); break;
eisd 7:f720032c2fb9 158 case 2: RX(); break;
eisd 7:f720032c2fb9 159 case 3: FR(); break;
eisd 7:f720032c2fb9 160 case 4: FX(); break;
eisd 7:f720032c2fb9 161 case 5: FF(); break;
eisd 7:f720032c2fb9 162 case 6: XF(); break;
eisd 7:f720032c2fb9 163 case 7: RF(); break;
eisd 7:f720032c2fb9 164 }
eisd 5:7af5760d7e8f 165 }
eisd 6:3482f1e19d71 166 Thread::wait(10);
eisd 5:7af5760d7e8f 167 }
eisd 5:7af5760d7e8f 168 Thread::wait(50);
eisd 5:7af5760d7e8f 169 }
eisd 5:7af5760d7e8f 170 }
eisd 5:7af5760d7e8f 171
eisd 11:7c3fb795af73 172 FastPWM speaker(PTA12);
eisd 7:f720032c2fb9 173 DigitalOut speaker_gnd(PTC4);
eisd 7:f720032c2fb9 174 //float speaker = 0;
eisd 7:f720032c2fb9 175 //float speaker_gnd = 0;
eisd 7:f720032c2fb9 176
eisd 7:f720032c2fb9 177 //global variables used by interrupt routine
eisd 11:7c3fb795af73 178 volatile int i=512;
eisd 12:eec17d54a646 179 float *wave;//[512];
eisd 7:f720032c2fb9 180
eisd 7:f720032c2fb9 181 // Interrupt routine
eisd 7:f720032c2fb9 182 // used to output next analog sample whenever a timer interrupt occurs
eisd 7:f720032c2fb9 183 void Sample_timer_interrupt(void)
eisd 7:f720032c2fb9 184 {
eisd 7:f720032c2fb9 185 // send next analog sample out to D to A
eisd 7:f720032c2fb9 186 speaker = wave[i];
eisd 7:f720032c2fb9 187 // increment pointer and wrap around back to 0 at 128
eisd 7:f720032c2fb9 188 i = (i+1) & 0x07F;
eisd 7:f720032c2fb9 189 }
eisd 7:f720032c2fb9 190
eisd 7:f720032c2fb9 191 int Siren_pitch = 1;
eisd 7:f720032c2fb9 192 void Siren_pitch_flip() {
eisd 11:7c3fb795af73 193 speaker.period(wave[--i]);
eisd 11:7c3fb795af73 194 if (i == 0)
eisd 11:7c3fb795af73 195 i = 128;
eisd 7:f720032c2fb9 196 }
eisd 7:f720032c2fb9 197
eisd 7:f720032c2fb9 198 Ticker siren_pitch;
eisd 11:7c3fb795af73 199 Timeout siren_pitch_switch;
eisd 7:f720032c2fb9 200 bool Siren_last = 0;
eisd 11:7c3fb795af73 201 void Siren_faster() {
eisd 11:7c3fb795af73 202 siren_pitch.detach();
eisd 11:7c3fb795af73 203 siren_pitch.attach(&Siren_pitch_flip, 0.6/128);
eisd 11:7c3fb795af73 204 }
eisd 7:f720032c2fb9 205 void Siren_state(bool on) {
eisd 7:f720032c2fb9 206 if (Siren_last != on) {
eisd 7:f720032c2fb9 207 pc.printf("siren %d\r\n", on);
eisd 7:f720032c2fb9 208 siren_pitch.detach();
eisd 7:f720032c2fb9 209 Siren_pitch = 1;
eisd 7:f720032c2fb9 210 Siren_pitch_flip();
eisd 11:7c3fb795af73 211 siren_pitch.detach();
eisd 11:7c3fb795af73 212 if (on)
eisd 11:7c3fb795af73 213 siren_pitch.attach(&Siren_pitch_flip, 0.6/128);
eisd 11:7c3fb795af73 214 //siren_pitch_switch.attach(&Siren_faster, 2.0);
eisd 11:7c3fb795af73 215 speaker = on ? 0.1 : 0;
eisd 7:f720032c2fb9 216 }
eisd 7:f720032c2fb9 217 Siren_last = on;
eisd 7:f720032c2fb9 218 }
eisd 7:f720032c2fb9 219
eisd 8:5f933580b560 220 DigitalOut Headlight_l(PTD7);
eisd 8:5f933580b560 221 DigitalOut Headlight_r(PTB8);
eisd 8:5f933580b560 222
eisd 8:5f933580b560 223 #define Headlight_swap 3
eisd 8:5f933580b560 224 #define Headlight_flicker 15
eisd 8:5f933580b560 225 uint8_t Headlight_mode = 0;
eisd 8:5f933580b560 226 uint8_t Headlight_phase = 0;
eisd 8:5f933580b560 227 Ticker Headlight_pattern;
eisd 8:5f933580b560 228 void Headlight_interrupt() {
eisd 8:5f933580b560 229 Headlight_phase = (Headlight_phase + 1) % Headlight_flicker;
eisd 8:5f933580b560 230 Headlight_l = Headlight_mode == 1 || (Headlight_mode == 2) && ((Headlight_phase % 2) == 0) && (Headlight_phase * 2 > Headlight_flicker);
eisd 8:5f933580b560 231 Headlight_r = Headlight_mode == 1 || (Headlight_mode == 2) && ((Headlight_phase % 2) == 0) && (Headlight_phase * 2 < Headlight_flicker);
eisd 8:5f933580b560 232 }
eisd 8:5f933580b560 233 void Headlight_state(bool a, bool b) {
eisd 8:5f933580b560 234 uint8_t mode = b ? 1 : a ? 2 : 0;
eisd 8:5f933580b560 235 if (Headlight_mode != mode) {
eisd 11:7c3fb795af73 236 if (mode == 1)
eisd 11:7c3fb795af73 237 i = 512;
eisd 8:5f933580b560 238 Headlight_mode = mode;
eisd 12:eec17d54a646 239 Headlight_pattern.detach();
eisd 8:5f933580b560 240 Headlight_interrupt();
eisd 12:eec17d54a646 241 if (mode > 0)
eisd 12:eec17d54a646 242 Headlight_pattern.attach_us(&Headlight_interrupt, (timestamp_t)(1000000/Headlight_swap/Headlight_flicker));
eisd 8:5f933580b560 243 }
eisd 8:5f933580b560 244 }
eisd 8:5f933580b560 245
eisd 12:eec17d54a646 246 void sleep_loop(void const *argument) {
eisd 12:eec17d54a646 247 while (true) {
eisd 12:eec17d54a646 248 sleep();
eisd 12:eec17d54a646 249 Thread::wait(100);
eisd 12:eec17d54a646 250 }
eisd 12:eec17d54a646 251 }
gbrush 0:7c98bcd8a245 252
eisd 2:04fdd571a385 253 #ifdef TARGET_KL25Z
eisd 6:3482f1e19d71 254 PwmOut r(LED_RED);
eisd 6:3482f1e19d71 255 //float r = 1.0f;
eisd 6:3482f1e19d71 256 PwmOut g(LED_GREEN);
eisd 6:3482f1e19d71 257 //float g = 1.0f;
eisd 6:3482f1e19d71 258 PwmOut b(LED_BLUE);
eisd 6:3482f1e19d71 259 //float b = 1.0f;
eisd 3:9091adbed369 260 WiiChuck nun(PTE0, PTE1, pc);
eisd 2:04fdd571a385 261 RFM69 radio(PTD2, PTD3, PTC5, PTD0, PTA13);
eisd 2:04fdd571a385 262 #else
eisd 2:04fdd571a385 263 WiiChuck nun(p9, p10, pc);
eisd 2:04fdd571a385 264 #endif
eisd 2:04fdd571a385 265
eisd 13:ab524cb4f768 266 Timer rx_last_contact;
eisd 12:eec17d54a646 267 bool rx_to_snooze = true;
eisd 12:eec17d54a646 268 bool rx_snoozing = false;
eisd 12:eec17d54a646 269 bool rx_snoozed() {
eisd 12:eec17d54a646 270 if (rx_snoozing) {
eisd 12:eec17d54a646 271 pc.printf("still snoozing\r\n");
eisd 12:eec17d54a646 272 Thread::signal_wait(0x1);
eisd 12:eec17d54a646 273 pc.printf("signalled?\r\n");
eisd 12:eec17d54a646 274 }
eisd 12:eec17d54a646 275 return true;
eisd 12:eec17d54a646 276 }
eisd 12:eec17d54a646 277
eisd 12:eec17d54a646 278 void rx_snoozer(void const *mainThreadID) {
eisd 13:ab524cb4f768 279 pc.printf("snooze rx %f\r\n", rx_last_contact.read());
eisd 12:eec17d54a646 280
eisd 12:eec17d54a646 281 rx_snoozing = true;
eisd 13:ab524cb4f768 282 radio.sleep();
eisd 13:ab524cb4f768 283 WakeUp::set(5);//rx_last_contact.read() < 10? 1 : 5);
eisd 13:ab524cb4f768 284 deepsleep();
eisd 12:eec17d54a646 285 rx_snoozing = false;
eisd 12:eec17d54a646 286 rx_to_snooze = true;
eisd 12:eec17d54a646 287 pc.printf("unsnooze rx\r\n");
eisd 12:eec17d54a646 288 osSignalSet((osThreadId)mainThreadID, 0x1);
eisd 12:eec17d54a646 289 }
eisd 12:eec17d54a646 290
eisd 12:eec17d54a646 291 int main()
eisd 12:eec17d54a646 292 {
eisd 13:ab524cb4f768 293 WakeUp::calibrate();
eisd 13:ab524cb4f768 294 RtosTimer rx_snooze(&rx_snoozer, osTimerOnce, (void*)osThreadGetId());
eisd 12:eec17d54a646 295
eisd 12:eec17d54a646 296 #ifndef USBSerial
eisd 12:eec17d54a646 297 pc.baud(115200);
eisd 12:eec17d54a646 298 #endif
eisd 12:eec17d54a646 299
eisd 13:ab524cb4f768 300 r = g = b = 1;
eisd 6:3482f1e19d71 301 gnd = 0;
eisd 6:3482f1e19d71 302 ir.period_us(1000/38);
eisd 6:3482f1e19d71 303
eisd 7:f720032c2fb9 304 speaker_gnd = 0;
eisd 7:f720032c2fb9 305 speaker = 0;
eisd 7:f720032c2fb9 306 //speaker.period(1.0/200000.0);
eisd 7:f720032c2fb9 307
eisd 7:f720032c2fb9 308 /*
eisd 7:f720032c2fb9 309 speaker = 0.2;
eisd 7:f720032c2fb9 310 while(1) {
eisd 7:f720032c2fb9 311 speaker.period(.8/554.365);
eisd 7:f720032c2fb9 312 wait(.8);
eisd 7:f720032c2fb9 313 speaker.period(.8/523.251);
eisd 7:f720032c2fb9 314 wait(.8);
eisd 7:f720032c2fb9 315 }
eisd 7:f720032c2fb9 316 //speaker = 0.2;
eisd 7:f720032c2fb9 317 // set up a timer to be used for sample rate interrupts
eisd 7:f720032c2fb9 318 Ticker Sample_Period;
eisd 7:f720032c2fb9 319 // precompute 128 sample points on one sine wave cycle
eisd 7:f720032c2fb9 320 // used for continuous sine wave output later
eisd 7:f720032c2fb9 321 for(int k=0; k<128; k++) {
eisd 7:f720032c2fb9 322 wave[k]=((1.0 + sin((float(k)/128.0*6.28318530717959)))/2.0);
eisd 7:f720032c2fb9 323 // scale the sine wave from 0.0 to 1.0 - as needed for AnalogOut arg
eisd 7:f720032c2fb9 324 }
eisd 7:f720032c2fb9 325 // turn on timer interrupts to start sine wave output
eisd 7:f720032c2fb9 326 // sample rate is 500Hz with 128 samples per cycle on sine wave
eisd 7:f720032c2fb9 327 while(1) {
eisd 7:f720032c2fb9 328 Sample_Period.detach();
eisd 7:f720032c2fb9 329 Sample_Period.attach(&Sample_timer_interrupt, 1.0/(554.365*128));
eisd 7:f720032c2fb9 330 wait(.8);
eisd 7:f720032c2fb9 331 Sample_Period.detach();
eisd 7:f720032c2fb9 332 Sample_Period.attach(&Sample_timer_interrupt, 1.0/(523.251*128));
eisd 7:f720032c2fb9 333 wait(.8);
eisd 7:f720032c2fb9 334 }
eisd 7:f720032c2fb9 335 // everything else needed is already being done by the interrupt routine
eisd 7:f720032c2fb9 336 */
eisd 7:f720032c2fb9 337
eisd 5:7af5760d7e8f 338 nunchuk n1, n2;
eisd 5:7af5760d7e8f 339 nunchuk *n = &n1, *next = &n2;
eisd 5:7af5760d7e8f 340 bool sender = nun.Read(&n->X, &n->Y, &n->aX, &n->aY, &n->aZ, &n->C, &n->Z);
eisd 4:c9711f0cd097 341 if (sender) {
eisd 4:c9711f0cd097 342 pc.printf("chuck attached\r\n");
eisd 4:c9711f0cd097 343 radio.initialize(FREQUENCY, NODE_ID, NETWORKID);
eisd 4:c9711f0cd097 344 } else {
eisd 4:c9711f0cd097 345 pc.printf("chuck unavailable\r\n");
eisd 4:c9711f0cd097 346 radio.initialize(FREQUENCY, GATEWAY_ID, NETWORKID);
eisd 5:7af5760d7e8f 347 thread = new Thread(ir_thread);
eisd 12:eec17d54a646 348 wave = new float[i];
eisd 12:eec17d54a646 349 int m = i-128;
eisd 12:eec17d54a646 350 for(int k = 0; k < m; k++) {
eisd 12:eec17d54a646 351 // ramp up
eisd 12:eec17d54a646 352 wave[i-k-1] = 1.0/(1000+k*400.0/m);
eisd 12:eec17d54a646 353 }
eisd 12:eec17d54a646 354 for(int k = 0; k < 128; k++) {
eisd 12:eec17d54a646 355 // LFO
eisd 12:eec17d54a646 356 wave[127-k] = 1.0/(1400+200*sin(k/128.0*6.28318530717959));
eisd 12:eec17d54a646 357 }
eisd 13:ab524cb4f768 358 rx_last_contact.start();
eisd 5:7af5760d7e8f 359 }
eisd 3:9091adbed369 360 radio.encrypt("0123456789054321");
eisd 2:04fdd571a385 361 //radio.promiscuous(false);
eisd 3:9091adbed369 362 radio.setHighPower(true);
eisd 3:9091adbed369 363 radio.setPowerLevel(20);
eisd 3:9091adbed369 364 radio.rcCalibration();
eisd 2:04fdd571a385 365 //radio.readAllRegs();
eisd 2:04fdd571a385 366 pc.printf("temp %d\r\n", radio.readTemperature(-1));
eisd 1:de8c34c9ccdf 367
eisd 12:eec17d54a646 368 Thread t_sleep_loop(&sleep_loop);
eisd 6:3482f1e19d71 369 bool read = false;
eisd 1:de8c34c9ccdf 370 while(1) {
eisd 7:f720032c2fb9 371 if (sender) {
eisd 5:7af5760d7e8f 372 read = nun.Read(&n->X, &n->Y, &n->aX, &n->aY, &n->aZ, &n->C, &n->Z);
eisd 7:f720032c2fb9 373 n->sum = 0;
eisd 7:f720032c2fb9 374 n->sum = calculate_crc8((char*)n, sizeof(struct nunchuk));
eisd 7:f720032c2fb9 375 }
eisd 12:eec17d54a646 376 else if (rx_snoozed() && radio.receiveDone()) {
eisd 13:ab524cb4f768 377 rx_last_contact.reset();
eisd 12:eec17d54a646 378 rx_snooze.stop();
eisd 12:eec17d54a646 379 rx_to_snooze = true;
eisd 12:eec17d54a646 380 pc.printf("rssi %d\r\n", radio.RSSI);
eisd 4:c9711f0cd097 381 read = (radio.DATALEN == sizeof(struct nunchuk));
eisd 5:7af5760d7e8f 382 if (read) {
eisd 6:3482f1e19d71 383 memcpy((void*)next, (const void*)radio.DATA, radio.DATALEN);
eisd 7:f720032c2fb9 384 uint8_t sum = next->sum;
eisd 7:f720032c2fb9 385 next->sum = 0;
eisd 7:f720032c2fb9 386 read = sum == calculate_crc8((char*)next, sizeof(struct nunchuk));
eisd 7:f720032c2fb9 387 if (read) {
eisd 7:f720032c2fb9 388 nunchuk *last = n;
eisd 7:f720032c2fb9 389 n = next;
eisd 7:f720032c2fb9 390 next = last;
eisd 7:f720032c2fb9 391 }
eisd 7:f720032c2fb9 392 }
eisd 7:f720032c2fb9 393 if (!read)
eisd 7:f720032c2fb9 394 pc.printf("len %d\r\n", radio.DATALEN);
eisd 12:eec17d54a646 395 } else if (rx_to_snooze) {
eisd 13:ab524cb4f768 396 rx_snooze.start(200);
eisd 12:eec17d54a646 397 rx_to_snooze = false;
eisd 4:c9711f0cd097 398 }
eisd 1:de8c34c9ccdf 399 if(read)
eisd 1:de8c34c9ccdf 400 {
eisd 7:f720032c2fb9 401 Siren_state(n->C);
eisd 8:5f933580b560 402 Headlight_state(n->C, n->Z);
eisd 5:7af5760d7e8f 403 float x = n->X - 128, y = n->Y - 128;
eisd 3:9091adbed369 404 float R = x*x + y*y, p = atan2(y, x) * 4 / M_PI - 0.5;
eisd 1:de8c34c9ccdf 405 int c = 0;
eisd 3:9091adbed369 406 if (p > -4) c = 0;
eisd 3:9091adbed369 407 if (p > -3) c = 1;
eisd 3:9091adbed369 408 if (p > -2) c = 2;
eisd 3:9091adbed369 409 if (p > -1) c = 3;
eisd 3:9091adbed369 410 if (p > 0) c = 4;
eisd 3:9091adbed369 411 if (p > 1) c = 5;
eisd 3:9091adbed369 412 if (p > 2) c = 6;
eisd 3:9091adbed369 413 if (p > 3) c = 7;
eisd 5:7af5760d7e8f 414 direction = c;
eisd 1:de8c34c9ccdf 415
eisd 14:579137c8ceac 416 #if DEBUG > 1
eisd 6:3482f1e19d71 417 pc.printf("%d: ", sizeof(struct nunchuk));
eisd 6:3482f1e19d71 418 pc.printf("x%3d y%3d c%1d z%1d --", n->X, n->Y, n->C, n->Z);
eisd 6:3482f1e19d71 419 pc.printf("x%d y%d z%d -- %.3f %s \r\n", n->aX, n->aY, n->aZ, R, direction);//s[c]);
eisd 3:9091adbed369 420
eisd 3:9091adbed369 421 //radio.send(GATEWAY_ID, (const void*)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 50, false);
eisd 3:9091adbed369 422 #endif
eisd 12:eec17d54a646 423 if (sender && central_time.read() < 10)
eisd 6:3482f1e19d71 424 radio.send(GATEWAY_ID, (const void*)n, sizeof(struct nunchuk), false);
eisd 3:9091adbed369 425
eisd 1:de8c34c9ccdf 426 #ifdef TARGET_KL25Z
eisd 3:9091adbed369 427 if (R < 20) {
eisd 3:9091adbed369 428 if (!central) {
eisd 3:9091adbed369 429 pc.printf("central\r\n");
eisd 12:eec17d54a646 430 central_time.start();
eisd 3:9091adbed369 431 central = true;
eisd 3:9091adbed369 432 }
eisd 1:de8c34c9ccdf 433 r = 1.0f;
eisd 1:de8c34c9ccdf 434 g = 1.0f;
eisd 1:de8c34c9ccdf 435 b = 1.0f;
eisd 1:de8c34c9ccdf 436 } else {
eisd 3:9091adbed369 437 if (central) {
eisd 6:3482f1e19d71 438 pc.printf("go %s\r\n", directions[direction]);
eisd 3:9091adbed369 439 central = false;
eisd 12:eec17d54a646 440 central_time.stop();
eisd 12:eec17d54a646 441 central_time.reset();
eisd 3:9091adbed369 442 }
eisd 3:9091adbed369 443 R = R/20000;
eisd 1:de8c34c9ccdf 444 float pal[8][3] = {
eisd 1:de8c34c9ccdf 445 { 0, 0, 1 },
eisd 1:de8c34c9ccdf 446 { 0, 1, 1 },
eisd 1:de8c34c9ccdf 447 { 0, 1, 0 },
eisd 1:de8c34c9ccdf 448 { 1, 1, 0 },
eisd 1:de8c34c9ccdf 449 { 1, 0.5, 0 },
eisd 1:de8c34c9ccdf 450 { 1, 0, 0 },
eisd 1:de8c34c9ccdf 451 { 1, 0, 1 },
eisd 1:de8c34c9ccdf 452 { 0.5, 0, 1 },
eisd 1:de8c34c9ccdf 453 };
eisd 6:3482f1e19d71 454 c = (c + 4) % 8;
eisd 1:de8c34c9ccdf 455 r = 1.0f - pal[c][0] * R;
eisd 1:de8c34c9ccdf 456 g = 1.0f - pal[c][1] * R;
eisd 1:de8c34c9ccdf 457 b = 1.0f - pal[c][2] * R;
eisd 1:de8c34c9ccdf 458 }
eisd 1:de8c34c9ccdf 459 #endif
eisd 1:de8c34c9ccdf 460 }
eisd 4:c9711f0cd097 461 else if (sender)
eisd 1:de8c34c9ccdf 462 {
eisd 1:de8c34c9ccdf 463 pc.printf("Error\r\n");
eisd 3:9091adbed369 464 return 0;
eisd 1:de8c34c9ccdf 465 }
eisd 4:c9711f0cd097 466 if (sender)
eisd 4:c9711f0cd097 467 wait(0.01);
eisd 5:7af5760d7e8f 468 else
eisd 5:7af5760d7e8f 469 Thread::wait(10);
eisd 1:de8c34c9ccdf 470 }
eisd 1:de8c34c9ccdf 471 }