Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Sat Jan 09 12:04:00 2016 +0000
Revision:
41:dee3fd34e37a
Parent:
30:c60b0d52b067
Child:
67:5650f461722a
Station-setting protocol optimizations.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AntonLS 5:1b9734e68327 1 /*
AntonLS 11:d3aa5fca2330 2 * True Agility
AntonLS 11:d3aa5fca2330 3 *
AntonLS 5:1b9734e68327 4 * Buffered IO of pending CharacteristicWrites meant to Nofify
AntonLS 11:d3aa5fca2330 5 * the phone/tablet application. Started 20150416 ALS
AntonLS 5:1b9734e68327 6 *
AntonLS 5:1b9734e68327 7 */
AntonLS 5:1b9734e68327 8
AntonLS 5:1b9734e68327 9 #ifndef PHONEAPPIO_H
AntonLS 5:1b9734e68327 10 #define PHONEAPPIO_H
AntonLS 5:1b9734e68327 11
AntonLS 5:1b9734e68327 12 #include "mbed.h"
AntonLS 5:1b9734e68327 13 #include "BLEDevice.h"
AntonLS 5:1b9734e68327 14 #include <cstdarg>
AntonLS 11:d3aa5fca2330 15 #include <cstdio>
AntonLS 5:1b9734e68327 16 #include "MTSBufferedIO.h"
AntonLS 11:d3aa5fca2330 17 #include "Vars.h"
AntonLS 5:1b9734e68327 18
AntonLS 30:c60b0d52b067 19 #define STRING_STACK_LIMIT 64 /* 120 */
AntonLS 11:d3aa5fca2330 20 #define TXRX_BUF_LEN 20
AntonLS 11:d3aa5fca2330 21
AntonLS 11:d3aa5fca2330 22 extern uint8_t txPayload[];
AntonLS 11:d3aa5fca2330 23 extern bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead );
AntonLS 5:1b9734e68327 24
AntonLS 5:1b9734e68327 25 // namespace moo
AntonLS 5:1b9734e68327 26 // {
AntonLS 5:1b9734e68327 27
AntonLS 5:1b9734e68327 28 class PhoneAppIO : public mts::MTSBufferedIO
AntonLS 5:1b9734e68327 29 {
AntonLS 5:1b9734e68327 30 public:
AntonLS 11:d3aa5fca2330 31 PhoneAppIO( BLEDevice &ble, GattAttribute::Handle_t toPhoneHandle,
AntonLS 11:d3aa5fca2330 32 GattAttribute::Handle_t toConeHandle,
AntonLS 41:dee3fd34e37a 33 int txBufferSize=468, int rxBufferSize=120 );
AntonLS 5:1b9734e68327 34
AntonLS 11:d3aa5fca2330 35 /** Destructs a PhoneAppIO object and frees all related resources, including
AntonLS 5:1b9734e68327 36 * internal buffers.
AntonLS 5:1b9734e68327 37 */
AntonLS 5:1b9734e68327 38 ~PhoneAppIO();
AntonLS 5:1b9734e68327 39
AntonLS 5:1b9734e68327 40 /** Write a string to the buffer
AntonLS 5:1b9734e68327 41 *
AntonLS 11:d3aa5fca2330 42 * @param str The string to write (append your own CR)
AntonLS 5:1b9734e68327 43 *
AntonLS 5:1b9734e68327 44 * @returns 0 if the write succeeds, EOF for error
AntonLS 5:1b9734e68327 45 */
AntonLS 5:1b9734e68327 46 int puts( const char *str )
AntonLS 5:1b9734e68327 47 {
AntonLS 5:1b9734e68327 48 return write( str, strlen( str ) );
AntonLS 5:1b9734e68327 49 }
AntonLS 5:1b9734e68327 50
AntonLS 5:1b9734e68327 51 int printf( const char *format, ... )
AntonLS 5:1b9734e68327 52 {
AntonLS 5:1b9734e68327 53 va_list arg;
AntonLS 5:1b9734e68327 54 va_start( arg, format );
AntonLS 5:1b9734e68327 55
AntonLS 5:1b9734e68327 56 int len = PhoneAppIO::vprintf( format, arg );
AntonLS 5:1b9734e68327 57
AntonLS 5:1b9734e68327 58 va_end( arg );
AntonLS 5:1b9734e68327 59
AntonLS 5:1b9734e68327 60 return len;
AntonLS 5:1b9734e68327 61 }
AntonLS 5:1b9734e68327 62 int vprintf( const char *format, va_list arg )
AntonLS 5:1b9734e68327 63 {
AntonLS 5:1b9734e68327 64 int len = vsnprintf( NULL, 0, format, arg );
AntonLS 5:1b9734e68327 65 if( len < STRING_STACK_LIMIT )
AntonLS 5:1b9734e68327 66 {
AntonLS 5:1b9734e68327 67 char temp[STRING_STACK_LIMIT];
elmbed 23:26f27c462976 68 vsnprintf( temp, sizeof(temp), format, arg );
AntonLS 5:1b9734e68327 69 puts( temp );
AntonLS 5:1b9734e68327 70
AntonLS 5:1b9734e68327 71 } else
AntonLS 5:1b9734e68327 72 {
AntonLS 5:1b9734e68327 73 char *temp = new char[len + 1];
AntonLS 5:1b9734e68327 74 vsprintf( temp, format, arg );
AntonLS 5:1b9734e68327 75 puts( temp );
AntonLS 5:1b9734e68327 76 delete[] temp;
AntonLS 5:1b9734e68327 77 }
AntonLS 5:1b9734e68327 78
AntonLS 5:1b9734e68327 79 return len;
AntonLS 5:1b9734e68327 80 }
AntonLS 5:1b9734e68327 81
AntonLS 11:d3aa5fca2330 82 int putchar( int ch )
AntonLS 11:d3aa5fca2330 83 {
AntonLS 11:d3aa5fca2330 84 return ((0 == write( (char)ch )) ? EOF : ch);
AntonLS 11:d3aa5fca2330 85 }
AntonLS 11:d3aa5fca2330 86
AntonLS 11:d3aa5fca2330 87 int getchar()
AntonLS 11:d3aa5fca2330 88 {
AntonLS 11:d3aa5fca2330 89 char ch;
AntonLS 11:d3aa5fca2330 90 return ((0 == atomicRead( ch )) ? EOF : ch);
AntonLS 11:d3aa5fca2330 91 }
AntonLS 11:d3aa5fca2330 92
AntonLS 11:d3aa5fca2330 93 uint16_t maybeHandleRead( const GattCharacteristicWriteCBParams *params ); // Called by onDataWritten() from BLE.
AntonLS 11:d3aa5fca2330 94 uint16_t maybeHandleWrite(); // Called by toPhoneChk() from main loop.
AntonLS 19:afcbb425b3cf 95 uint16_t injectHandleRead( char *buf, int len ); // Inject data from elsewhere simulating BLE-in.
AntonLS 11:d3aa5fca2330 96
AntonLS 11:d3aa5fca2330 97 bool loopbackMode;
AntonLS 11:d3aa5fca2330 98 bool busy;
AntonLS 5:1b9734e68327 99
AntonLS 5:1b9734e68327 100 protected:
AntonLS 11:d3aa5fca2330 101 BLEDevice *bleP;
AntonLS 11:d3aa5fca2330 102 bool bleWasntReady;
AntonLS 11:d3aa5fca2330 103
AntonLS 11:d3aa5fca2330 104 GattAttribute::Handle_t toPhoneHandle;
AntonLS 11:d3aa5fca2330 105 GattAttribute::Handle_t toConeHandle;
AntonLS 11:d3aa5fca2330 106
AntonLS 11:d3aa5fca2330 107 char *data;
AntonLS 11:d3aa5fca2330 108 uint16_t bytesRead;
AntonLS 11:d3aa5fca2330 109
AntonLS 11:d3aa5fca2330 110 uint8_t rx_buf[TXRX_BUF_LEN];
AntonLS 11:d3aa5fca2330 111 uint8_t rx_len;
AntonLS 5:1b9734e68327 112
AntonLS 5:1b9734e68327 113 private:
AntonLS 5:1b9734e68327 114 virtual void handleWrite(); // Method for handling data to be written
AntonLS 5:1b9734e68327 115 virtual void handleRead(); // Method for handling data to be read
AntonLS 11:d3aa5fca2330 116
AntonLS 11:d3aa5fca2330 117 // For locking out read() during write() to prevent buffer state corruption.
AntonLS 11:d3aa5fca2330 118 virtual void disableTxIrq();
AntonLS 11:d3aa5fca2330 119 virtual void enableTxIrq();
AntonLS 11:d3aa5fca2330 120
AntonLS 11:d3aa5fca2330 121 // For locking out write() during read() to prevent buffer state corruption.
AntonLS 11:d3aa5fca2330 122 virtual void disableRxIrq();
AntonLS 11:d3aa5fca2330 123 virtual void enableRxIrq();
AntonLS 11:d3aa5fca2330 124
AntonLS 11:d3aa5fca2330 125 bool writing;
AntonLS 11:d3aa5fca2330 126 bool reading;
AntonLS 5:1b9734e68327 127 };
AntonLS 5:1b9734e68327 128
AntonLS 5:1b9734e68327 129 // }
AntonLS 5:1b9734e68327 130
AntonLS 5:1b9734e68327 131 #endif /* PHONEAPPIO_H */
AntonLS 5:1b9734e68327 132
AntonLS 5:1b9734e68327 133 /* EOF */