Code to detect hand movement for use in video game

Dependencies:   LSM9DS1_Library_cal XBee mbed

Committer:
jgensel3
Date:
Mon Apr 24 04:14:25 2017 +0000
Revision:
0:fa587f2eec11
Code to detect hand movement for game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jgensel3 0:fa587f2eec11 1 #ifndef WProtocol_h
jgensel3 0:fa587f2eec11 2 #define WProtocol_h
jgensel3 0:fa587f2eec11 3
jgensel3 0:fa587f2eec11 4 #include "XBee/XBee.h"
jgensel3 0:fa587f2eec11 5 #include <mbed.h>
jgensel3 0:fa587f2eec11 6
jgensel3 0:fa587f2eec11 7 #define HOST_ADDRESS 0x0003
jgensel3 0:fa587f2eec11 8
jgensel3 0:fa587f2eec11 9 enum Direction {DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT, DIR_NONE, A_BUTTON, B_BUTTON};
jgensel3 0:fa587f2eec11 10 enum WMessageType {FOOT_STEP, HAND_GESTURE};
jgensel3 0:fa587f2eec11 11
jgensel3 0:fa587f2eec11 12 typedef struct WMessage {
jgensel3 0:fa587f2eec11 13 WMessageType type;
jgensel3 0:fa587f2eec11 14 Direction direction;
jgensel3 0:fa587f2eec11 15 } WMessage_t;
jgensel3 0:fa587f2eec11 16
jgensel3 0:fa587f2eec11 17 class WirelessModule {
jgensel3 0:fa587f2eec11 18 public:
jgensel3 0:fa587f2eec11 19 WirelessModule(PinName tx, PinName rx, WMessageType type) :
jgensel3 0:fa587f2eec11 20 uart(tx, rx), xbee(uart),
jgensel3 0:fa587f2eec11 21 tx16req(HOST_ADDRESS, (uint8_t *) &message, sizeof(message))
jgensel3 0:fa587f2eec11 22 {
jgensel3 0:fa587f2eec11 23 uart.baud(9600);
jgensel3 0:fa587f2eec11 24 message.type = type;
jgensel3 0:fa587f2eec11 25 }
jgensel3 0:fa587f2eec11 26
jgensel3 0:fa587f2eec11 27 int sendDirection(Direction dir) {
jgensel3 0:fa587f2eec11 28 message.direction = dir;
jgensel3 0:fa587f2eec11 29 xbee.send(tx16req);
jgensel3 0:fa587f2eec11 30 return 0;
jgensel3 0:fa587f2eec11 31 }
jgensel3 0:fa587f2eec11 32 private:
jgensel3 0:fa587f2eec11 33 RawSerial uart;
jgensel3 0:fa587f2eec11 34 XBee xbee;
jgensel3 0:fa587f2eec11 35 WMessage_t message;
jgensel3 0:fa587f2eec11 36 Tx16Request tx16req;
jgensel3 0:fa587f2eec11 37 };
jgensel3 0:fa587f2eec11 38
jgensel3 0:fa587f2eec11 39 class WirelessHost {
jgensel3 0:fa587f2eec11 40 public:
jgensel3 0:fa587f2eec11 41 WirelessHost(PinName tx, PinName rx) : uart(tx, rx), xbee(uart) {}
jgensel3 0:fa587f2eec11 42
jgensel3 0:fa587f2eec11 43 int waitForMessage(WMessage_t *store) {
jgensel3 0:fa587f2eec11 44 xbee.readPacket(5000);
jgensel3 0:fa587f2eec11 45
jgensel3 0:fa587f2eec11 46 if (xbee.getResponse().isError()) {
jgensel3 0:fa587f2eec11 47 // API error
jgensel3 0:fa587f2eec11 48 return -99;
jgensel3 0:fa587f2eec11 49 }
jgensel3 0:fa587f2eec11 50
jgensel3 0:fa587f2eec11 51 if (xbee.getResponse().isAvailable()) {
jgensel3 0:fa587f2eec11 52 if (xbee.getResponse().getApiId() != RX_16_RESPONSE) {
jgensel3 0:fa587f2eec11 53 // Unexpected API message
jgensel3 0:fa587f2eec11 54 return -5;
jgensel3 0:fa587f2eec11 55 }
jgensel3 0:fa587f2eec11 56
jgensel3 0:fa587f2eec11 57 // Retrieve response
jgensel3 0:fa587f2eec11 58 xbee.getResponse().getRx16Response(rx16resp);
jgensel3 0:fa587f2eec11 59
jgensel3 0:fa587f2eec11 60 // Validate packet length
jgensel3 0:fa587f2eec11 61 if (rx16resp.getDataLength() == sizeof(WMessage_t)) {
jgensel3 0:fa587f2eec11 62 // Store the payload
jgensel3 0:fa587f2eec11 63 memcpy(store, rx16resp.getData(), sizeof(WMessage_t));
jgensel3 0:fa587f2eec11 64 return 0;
jgensel3 0:fa587f2eec11 65 } else {
jgensel3 0:fa587f2eec11 66 // Unexpected payload format
jgensel3 0:fa587f2eec11 67 return -10;
jgensel3 0:fa587f2eec11 68 }
jgensel3 0:fa587f2eec11 69 } else {
jgensel3 0:fa587f2eec11 70 // No Response
jgensel3 0:fa587f2eec11 71 return -1;
jgensel3 0:fa587f2eec11 72 }
jgensel3 0:fa587f2eec11 73 }
jgensel3 0:fa587f2eec11 74
jgensel3 0:fa587f2eec11 75 private:
jgensel3 0:fa587f2eec11 76 RawSerial uart;
jgensel3 0:fa587f2eec11 77 XBee xbee;
jgensel3 0:fa587f2eec11 78 Rx16Response rx16resp;
jgensel3 0:fa587f2eec11 79 };
jgensel3 0:fa587f2eec11 80
jgensel3 0:fa587f2eec11 81 #endif