Elmo Terminal provides functionality to test Lora radio and access SX1272 chip registers delivered with Elmo board. This firmware allows the user to control the LoRa radio parameters (eg. frequency, bandwidth, spreading factor etc.) by entering console commands via serial terminal. Application also contains "Ping-Pong" and data transmission functionalities.
Fork of Elmo-Terminal by
Commands/PingPongCmd.cpp@12:26045241f50f, 2015-10-22 (annotated)
- Committer:
- WGorniak
- Date:
- Thu Oct 22 08:03:32 2015 +0000
- Revision:
- 12:26045241f50f
- Parent:
- 2:8d8295a51f68
updated sx lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WGorniak | 2:8d8295a51f68 | 1 | #include "PingPongCmd.h" |
WGorniak | 2:8d8295a51f68 | 2 | #include "dbg.h" |
WGorniak | 2:8d8295a51f68 | 3 | |
WGorniak | 2:8d8295a51f68 | 4 | const string PingPongCmd::PingMsg = "PING"; |
WGorniak | 2:8d8295a51f68 | 5 | const string PingPongCmd::PongMsg = "PONG"; |
WGorniak | 2:8d8295a51f68 | 6 | |
WGorniak | 2:8d8295a51f68 | 7 | #define RX_TIMEOUT_VALUE 3500000 // in us |
WGorniak | 2:8d8295a51f68 | 8 | |
WGorniak | 2:8d8295a51f68 | 9 | |
WGorniak | 2:8d8295a51f68 | 10 | |
WGorniak | 2:8d8295a51f68 | 11 | |
WGorniak | 2:8d8295a51f68 | 12 | PingPongCmd::PingPongCmd(Settings* radioSettings) |
WGorniak | 2:8d8295a51f68 | 13 | : RadioCmd(radioSettings), |
WGorniak | 2:8d8295a51f68 | 14 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
WGorniak | 2:8d8295a51f68 | 15 | led(LED2) |
WGorniak | 2:8d8295a51f68 | 16 | #else |
WGorniak | 2:8d8295a51f68 | 17 | led(LED1) |
WGorniak | 2:8d8295a51f68 | 18 | #endif |
WGorniak | 2:8d8295a51f68 | 19 | { |
WGorniak | 2:8d8295a51f68 | 20 | isMaster = true; |
WGorniak | 2:8d8295a51f68 | 21 | queryable_ = true; |
WGorniak | 2:8d8295a51f68 | 22 | led = 0; |
WGorniak | 2:8d8295a51f68 | 23 | } |
WGorniak | 2:8d8295a51f68 | 24 | |
WGorniak | 2:8d8295a51f68 | 25 | PingPongCmd::~PingPongCmd() |
WGorniak | 2:8d8295a51f68 | 26 | { |
WGorniak | 2:8d8295a51f68 | 27 | } |
WGorniak | 2:8d8295a51f68 | 28 | |
WGorniak | 2:8d8295a51f68 | 29 | string PingPongCmd::cmd() |
WGorniak | 2:8d8295a51f68 | 30 | { |
WGorniak | 2:8d8295a51f68 | 31 | return "pp"; |
WGorniak | 2:8d8295a51f68 | 32 | } |
WGorniak | 2:8d8295a51f68 | 33 | |
WGorniak | 2:8d8295a51f68 | 34 | string PingPongCmd::desc() |
WGorniak | 2:8d8295a51f68 | 35 | { |
WGorniak | 2:8d8295a51f68 | 36 | return "ping pong test"; |
WGorniak | 2:8d8295a51f68 | 37 | } |
WGorniak | 2:8d8295a51f68 | 38 | |
WGorniak | 2:8d8295a51f68 | 39 | bool PingPongCmd::execute(list<string> args) |
WGorniak | 2:8d8295a51f68 | 40 | { |
WGorniak | 2:8d8295a51f68 | 41 | debug( "Starting Ping-Pong loop\r\n" ); |
WGorniak | 2:8d8295a51f68 | 42 | getRadioContex()->radio().Rx( RX_TIMEOUT_VALUE ); |
WGorniak | 2:8d8295a51f68 | 43 | getRadioContex()->setState(LOWPOWER); |
WGorniak | 2:8d8295a51f68 | 44 | return true; |
WGorniak | 2:8d8295a51f68 | 45 | } |
WGorniak | 2:8d8295a51f68 | 46 | |
WGorniak | 2:8d8295a51f68 | 47 | Cmd::Status PingPongCmd::process() |
WGorniak | 2:8d8295a51f68 | 48 | { |
WGorniak | 2:8d8295a51f68 | 49 | RadioContex* p = getRadioContex(); |
WGorniak | 2:8d8295a51f68 | 50 | Radio& Radio = p->radio(); |
WGorniak | 2:8d8295a51f68 | 51 | uint16_t& BufferSize = p->BufferSize; |
WGorniak | 2:8d8295a51f68 | 52 | uint8_t* Buffer = p->Buffer; |
WGorniak | 2:8d8295a51f68 | 53 | |
WGorniak | 2:8d8295a51f68 | 54 | |
WGorniak | 2:8d8295a51f68 | 55 | switch( p->getState() ) |
WGorniak | 2:8d8295a51f68 | 56 | { |
WGorniak | 2:8d8295a51f68 | 57 | case RX: |
WGorniak | 2:8d8295a51f68 | 58 | if( isMaster == true ) |
WGorniak | 2:8d8295a51f68 | 59 | { |
WGorniak | 2:8d8295a51f68 | 60 | if( BufferSize > 0 ) |
WGorniak | 2:8d8295a51f68 | 61 | { |
WGorniak | 2:8d8295a51f68 | 62 | if( strncmp( ( const char* )Buffer, PongMsg.c_str(), 4 ) == 0 ) |
WGorniak | 2:8d8295a51f68 | 63 | { |
WGorniak | 2:8d8295a51f68 | 64 | led = !led; |
WGorniak | 2:8d8295a51f68 | 65 | debug( "...Pong\r\n" ); |
WGorniak | 2:8d8295a51f68 | 66 | |
WGorniak | 2:8d8295a51f68 | 67 | p->Send(PingMsg); |
WGorniak | 2:8d8295a51f68 | 68 | } |
WGorniak | 2:8d8295a51f68 | 69 | else if( strncmp( ( const char* )Buffer, PingMsg.c_str(), 4 ) == 0 ) |
WGorniak | 2:8d8295a51f68 | 70 | { // A master already exists then become a slave |
WGorniak | 2:8d8295a51f68 | 71 | debug( "...Ping\r\n" ); |
WGorniak | 2:8d8295a51f68 | 72 | led = !led; |
WGorniak | 2:8d8295a51f68 | 73 | isMaster = false; |
WGorniak | 2:8d8295a51f68 | 74 | p->Send(PongMsg); |
WGorniak | 2:8d8295a51f68 | 75 | } |
WGorniak | 2:8d8295a51f68 | 76 | else // valid reception but neither a PING or a PONG message |
WGorniak | 2:8d8295a51f68 | 77 | { // Set device as master ans start again |
WGorniak | 2:8d8295a51f68 | 78 | isMaster = true; |
WGorniak | 2:8d8295a51f68 | 79 | Radio.Rx( RX_TIMEOUT_VALUE ); |
WGorniak | 2:8d8295a51f68 | 80 | } |
WGorniak | 2:8d8295a51f68 | 81 | } |
WGorniak | 2:8d8295a51f68 | 82 | } |
WGorniak | 2:8d8295a51f68 | 83 | else |
WGorniak | 2:8d8295a51f68 | 84 | { |
WGorniak | 2:8d8295a51f68 | 85 | if( BufferSize > 0 ) |
WGorniak | 2:8d8295a51f68 | 86 | { |
WGorniak | 2:8d8295a51f68 | 87 | if( strncmp( ( const char* )Buffer, PingMsg.c_str(), 4 ) == 0 ) |
WGorniak | 2:8d8295a51f68 | 88 | { |
WGorniak | 2:8d8295a51f68 | 89 | led = !led; |
WGorniak | 2:8d8295a51f68 | 90 | debug( "...Ping\r\n" ); |
WGorniak | 2:8d8295a51f68 | 91 | p->Send(PongMsg); |
WGorniak | 2:8d8295a51f68 | 92 | } |
WGorniak | 2:8d8295a51f68 | 93 | else // valid reception but not a PING as expected |
WGorniak | 2:8d8295a51f68 | 94 | { // Set device as master and start again |
WGorniak | 2:8d8295a51f68 | 95 | isMaster = true; |
WGorniak | 2:8d8295a51f68 | 96 | Radio.Rx( RX_TIMEOUT_VALUE ); |
WGorniak | 2:8d8295a51f68 | 97 | } |
WGorniak | 2:8d8295a51f68 | 98 | } |
WGorniak | 2:8d8295a51f68 | 99 | } |
WGorniak | 2:8d8295a51f68 | 100 | p->setState(LOWPOWER); |
WGorniak | 2:8d8295a51f68 | 101 | break; |
WGorniak | 2:8d8295a51f68 | 102 | case TX: |
WGorniak | 2:8d8295a51f68 | 103 | led = !led; |
WGorniak | 2:8d8295a51f68 | 104 | if( isMaster == true ) |
WGorniak | 2:8d8295a51f68 | 105 | { |
WGorniak | 2:8d8295a51f68 | 106 | debug( "Ping...\r\n" ); |
WGorniak | 2:8d8295a51f68 | 107 | } |
WGorniak | 2:8d8295a51f68 | 108 | else |
WGorniak | 2:8d8295a51f68 | 109 | { |
WGorniak | 2:8d8295a51f68 | 110 | debug( "Pong...\r\n" ); |
WGorniak | 2:8d8295a51f68 | 111 | } |
WGorniak | 2:8d8295a51f68 | 112 | Radio.Rx( RX_TIMEOUT_VALUE ); |
WGorniak | 2:8d8295a51f68 | 113 | p->setState(LOWPOWER); |
WGorniak | 2:8d8295a51f68 | 114 | break; |
WGorniak | 2:8d8295a51f68 | 115 | case RX_TIMEOUT: |
WGorniak | 2:8d8295a51f68 | 116 | if( isMaster == true ) |
WGorniak | 2:8d8295a51f68 | 117 | { |
WGorniak | 2:8d8295a51f68 | 118 | p->Send(PingMsg); |
WGorniak | 2:8d8295a51f68 | 119 | } |
WGorniak | 2:8d8295a51f68 | 120 | else |
WGorniak | 2:8d8295a51f68 | 121 | { |
WGorniak | 2:8d8295a51f68 | 122 | Radio.Rx( RX_TIMEOUT_VALUE ); |
WGorniak | 2:8d8295a51f68 | 123 | } |
WGorniak | 2:8d8295a51f68 | 124 | p->setState(LOWPOWER); |
WGorniak | 2:8d8295a51f68 | 125 | break; |
WGorniak | 2:8d8295a51f68 | 126 | case RX_ERROR: |
WGorniak | 2:8d8295a51f68 | 127 | // We have received a Packet with a CRC error, send reply as if packet was correct |
WGorniak | 2:8d8295a51f68 | 128 | if( isMaster == true ) |
WGorniak | 2:8d8295a51f68 | 129 | { |
WGorniak | 2:8d8295a51f68 | 130 | p->Send(PingMsg); |
WGorniak | 2:8d8295a51f68 | 131 | } |
WGorniak | 2:8d8295a51f68 | 132 | else |
WGorniak | 2:8d8295a51f68 | 133 | { |
WGorniak | 2:8d8295a51f68 | 134 | p->Send(PongMsg); |
WGorniak | 2:8d8295a51f68 | 135 | } |
WGorniak | 2:8d8295a51f68 | 136 | p->setState(LOWPOWER); |
WGorniak | 2:8d8295a51f68 | 137 | break; |
WGorniak | 2:8d8295a51f68 | 138 | case TX_TIMEOUT: |
WGorniak | 2:8d8295a51f68 | 139 | Radio.Rx( RX_TIMEOUT_VALUE ); |
WGorniak | 2:8d8295a51f68 | 140 | p->setState(LOWPOWER); |
WGorniak | 2:8d8295a51f68 | 141 | break; |
WGorniak | 2:8d8295a51f68 | 142 | case LOWPOWER: |
WGorniak | 2:8d8295a51f68 | 143 | break; |
WGorniak | 2:8d8295a51f68 | 144 | default: |
WGorniak | 2:8d8295a51f68 | 145 | p->setState(LOWPOWER); |
WGorniak | 2:8d8295a51f68 | 146 | break; |
WGorniak | 2:8d8295a51f68 | 147 | } |
WGorniak | 2:8d8295a51f68 | 148 | return CONTINUE; |
WGorniak | 2:8d8295a51f68 | 149 | } |
WGorniak | 2:8d8295a51f68 | 150 | |
WGorniak | 2:8d8295a51f68 | 151 | string PingPongCmd::getResponse(void) |
WGorniak | 2:8d8295a51f68 | 152 | { |
WGorniak | 2:8d8295a51f68 | 153 | return "frequency cmd rulez!"; |
WGorniak | 2:8d8295a51f68 | 154 | } |
WGorniak | 2:8d8295a51f68 | 155 |