Code to detect walking and convert to input for video game
Dependencies: LSM9DS1_Library_cal2 XBee mbed
Fork of FootModule by
Diff: Wireless.h
- Revision:
- 3:2d6ff72599f1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Wireless.h Mon Apr 24 04:05:46 2017 +0000 @@ -0,0 +1,81 @@ +#ifndef WProtocol_h +#define WProtocol_h + +#include "XBee/XBee.h" +#include <mbed.h> + +#define HOST_ADDRESS 0x0003 + +enum Direction {DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT, DIR_NONE}; +enum WMessageType {FOOT_STEP, HAND_GESTURE}; + +typedef struct WMessage { + WMessageType type; + Direction direction; +} WMessage_t; + +class WirelessModule { +public: + WirelessModule(PinName tx, PinName rx, WMessageType type) : + uart(tx, rx), xbee(uart), + tx16req(HOST_ADDRESS, (uint8_t *) &message, sizeof(message)) + { + uart.baud(9600); + message.type = type; + } + + int sendDirection(Direction dir) { + message.direction = dir; + xbee.send(tx16req); + return 0; + } +private: + RawSerial uart; + XBee xbee; + WMessage_t message; + Tx16Request tx16req; +}; + +class WirelessHost { +public: + WirelessHost(PinName tx, PinName rx) : uart(tx, rx), xbee(uart) {} + + int waitForMessage(WMessage_t *store) { + xbee.readPacket(5000); + + if (xbee.getResponse().isError()) { + // API error + return -99; + } + + if (xbee.getResponse().isAvailable()) { + if (xbee.getResponse().getApiId() != RX_16_RESPONSE) { + // Unexpected API message + return -5; + } + + // Retrieve response + xbee.getResponse().getRx16Response(rx16resp); + + // Validate packet length + if (rx16resp.getDataLength() == sizeof(WMessage_t)) { + // Store the payload + memcpy(store, rx16resp.getData(), sizeof(WMessage_t)); + return 0; + } else { + // Unexpected payload format + return -10; + } + } else { + // No Response + return -1; + } + } + +private: + RawSerial uart; + XBee xbee; + Rx16Response rx16resp; +}; + +#endif \ No newline at end of file