Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of football_project by
PhoneAppIO.h@11:d3aa5fca2330, 2015-04-23 (annotated)
- Committer:
- AntonLS
- Date:
- Thu Apr 23 06:36:57 2015 +0000
- Revision:
- 11:d3aa5fca2330
- Parent:
- 5:1b9734e68327
- Child:
- 19:afcbb425b3cf
PhoneAppIO class working.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 5:1b9734e68327 | 19 | #define STRING_STACK_LIMIT 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 | 11:d3aa5fca2330 | 33 | int txBufferSize=1280, int rxBufferSize=256 ); |
| 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]; |
| AntonLS | 5:1b9734e68327 | 68 | vsprintf( 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 | 11:d3aa5fca2330 | 95 | |
| AntonLS | 11:d3aa5fca2330 | 96 | bool loopbackMode; |
| AntonLS | 11:d3aa5fca2330 | 97 | bool busy; |
| AntonLS | 5:1b9734e68327 | 98 | |
| AntonLS | 5:1b9734e68327 | 99 | protected: |
| AntonLS | 11:d3aa5fca2330 | 100 | BLEDevice *bleP; |
| AntonLS | 11:d3aa5fca2330 | 101 | bool bleWasntReady; |
| AntonLS | 11:d3aa5fca2330 | 102 | |
| AntonLS | 11:d3aa5fca2330 | 103 | GattAttribute::Handle_t toPhoneHandle; |
| AntonLS | 11:d3aa5fca2330 | 104 | GattAttribute::Handle_t toConeHandle; |
| AntonLS | 11:d3aa5fca2330 | 105 | |
| AntonLS | 11:d3aa5fca2330 | 106 | char *data; |
| AntonLS | 11:d3aa5fca2330 | 107 | uint16_t bytesRead; |
| AntonLS | 11:d3aa5fca2330 | 108 | |
| AntonLS | 11:d3aa5fca2330 | 109 | uint8_t rx_buf[TXRX_BUF_LEN]; |
| AntonLS | 11:d3aa5fca2330 | 110 | uint8_t rx_len; |
| AntonLS | 5:1b9734e68327 | 111 | |
| AntonLS | 5:1b9734e68327 | 112 | private: |
| AntonLS | 5:1b9734e68327 | 113 | virtual void handleWrite(); // Method for handling data to be written |
| AntonLS | 5:1b9734e68327 | 114 | virtual void handleRead(); // Method for handling data to be read |
| AntonLS | 11:d3aa5fca2330 | 115 | |
| AntonLS | 11:d3aa5fca2330 | 116 | // For locking out read() during write() to prevent buffer state corruption. |
| AntonLS | 11:d3aa5fca2330 | 117 | virtual void disableTxIrq(); |
| AntonLS | 11:d3aa5fca2330 | 118 | virtual void enableTxIrq(); |
| AntonLS | 11:d3aa5fca2330 | 119 | |
| AntonLS | 11:d3aa5fca2330 | 120 | // For locking out write() during read() to prevent buffer state corruption. |
| AntonLS | 11:d3aa5fca2330 | 121 | virtual void disableRxIrq(); |
| AntonLS | 11:d3aa5fca2330 | 122 | virtual void enableRxIrq(); |
| AntonLS | 11:d3aa5fca2330 | 123 | |
| AntonLS | 11:d3aa5fca2330 | 124 | bool writing; |
| AntonLS | 11:d3aa5fca2330 | 125 | bool reading; |
| AntonLS | 5:1b9734e68327 | 126 | }; |
| AntonLS | 5:1b9734e68327 | 127 | |
| AntonLS | 5:1b9734e68327 | 128 | // } |
| AntonLS | 5:1b9734e68327 | 129 | |
| AntonLS | 5:1b9734e68327 | 130 | #endif /* PHONEAPPIO_H */ |
| AntonLS | 5:1b9734e68327 | 131 | |
| AntonLS | 5:1b9734e68327 | 132 | /* EOF */ |
