JLC test
Dependencies: PinDetect libmDot mbed-rtos mbed
main.cpp@2:376af6a70e8a, 2016-01-30 (annotated)
- Committer:
- tmulrooney
- Date:
- Sat Jan 30 17:02:23 2016 +0000
- Revision:
- 2:376af6a70e8a
- Parent:
- 1:96c429800568
using UART 2 - not working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tmulrooney | 0:6aa743a332ae | 1 | /********************************************************************************** |
tmulrooney | 0:6aa743a332ae | 2 | * This program monitors the power on state, kill state and battery voltage on |
tmulrooney | 0:6aa743a332ae | 3 | * Woodstream mouse trap using the Multitech mDot and UDK2 development system. |
tmulrooney | 0:6aa743a332ae | 4 | * The power on state is monitored on the PA_4(UDK2 Pin D10)and the kill state is |
tmulrooney | 0:6aa743a332ae | 5 | * monitored on the PA_5 (UDK2 Pin D13) Digital Input Pins. The battery voltage |
tmulrooney | 0:6aa743a332ae | 6 | * is monitored on the PB_1 (UDK2 Pin A0) Analog Input. The status of these pins |
tmulrooney | 0:6aa743a332ae | 7 | * are transferred from the mDot LoRa to the Conduit LoRa gateway. The status is |
tmulrooney | 0:6aa743a332ae | 8 | * also shown on the USB Debug Terminal Window. |
tmulrooney | 0:6aa743a332ae | 9 | *********************************************************************************/ |
tmulrooney | 0:6aa743a332ae | 10 | |
tmulrooney | 0:6aa743a332ae | 11 | #include "mbed.h" |
tmulrooney | 0:6aa743a332ae | 12 | #include "mDot.h" |
tmulrooney | 0:6aa743a332ae | 13 | #include "MTSLog.h" |
tmulrooney | 0:6aa743a332ae | 14 | #include <string> |
tmulrooney | 0:6aa743a332ae | 15 | #include <vector> |
tmulrooney | 0:6aa743a332ae | 16 | #include <algorithm> |
tmulrooney | 0:6aa743a332ae | 17 | #include "PinDetect.h" |
tmulrooney | 1:96c429800568 | 18 | #include "common.h" |
tmulrooney | 0:6aa743a332ae | 19 | |
tmulrooney | 0:6aa743a332ae | 20 | #define I2C_TIME 5 |
tmulrooney | 0:6aa743a332ae | 21 | #define LED_TIME 1 |
tmulrooney | 0:6aa743a332ae | 22 | #define BATTERY_TIME 2.0 |
tmulrooney | 0:6aa743a332ae | 23 | #define KILL_STATUS_TIME 3.0 |
tmulrooney | 0:6aa743a332ae | 24 | #define POWER_ON_TIME 4.0 |
tmulrooney | 0:6aa743a332ae | 25 | |
tmulrooney | 0:6aa743a332ae | 26 | //mDot* dot; |
tmulrooney | 0:6aa743a332ae | 27 | Ticker ledTimer; |
tmulrooney | 0:6aa743a332ae | 28 | Ticker batteryTimer; |
tmulrooney | 0:6aa743a332ae | 29 | Ticker killStatusTimer; |
tmulrooney | 0:6aa743a332ae | 30 | Ticker powerOnTimer; |
tmulrooney | 0:6aa743a332ae | 31 | |
tmulrooney | 0:6aa743a332ae | 32 | AnalogIn batteryVoltage(PTB0); |
tmulrooney | 0:6aa743a332ae | 33 | DigitalIn powerOn(PTC1); /* SW2 */ |
tmulrooney | 0:6aa743a332ae | 34 | DigitalIn killStatus(PTB17); /* SW3 */ |
tmulrooney | 0:6aa743a332ae | 35 | DigitalOut led_red(PTA1); |
tmulrooney | 0:6aa743a332ae | 36 | DigitalOut led_green(PTA2); |
tmulrooney | 0:6aa743a332ae | 37 | //DigitalOut led_blue(PTD5); |
tmulrooney | 0:6aa743a332ae | 38 | DigitalOut spi_cs(PTD4); |
tmulrooney | 0:6aa743a332ae | 39 | |
tmulrooney | 2:376af6a70e8a | 40 | //Serial pc(PTE0, PTE1); |
tmulrooney | 2:376af6a70e8a | 41 | Serial pc(PTD3, PTD2); |
tmulrooney | 0:6aa743a332ae | 42 | |
tmulrooney | 0:6aa743a332ae | 43 | // Configuration variables |
tmulrooney | 0:6aa743a332ae | 44 | static std::string config_network_name = "LORA_NET"; |
tmulrooney | 0:6aa743a332ae | 45 | static std::string config_network_pass = "BLUE_HEN1"; |
tmulrooney | 0:6aa743a332ae | 46 | static uint8_t config_frequency_sub_band = 7; |
tmulrooney | 0:6aa743a332ae | 47 | |
tmulrooney | 0:6aa743a332ae | 48 | //Global Variables |
tmulrooney | 0:6aa743a332ae | 49 | bool readyToSendI2C; |
tmulrooney | 0:6aa743a332ae | 50 | bool readyToSendBatteryVoltage; |
tmulrooney | 0:6aa743a332ae | 51 | bool readyToSendKillStatus; |
tmulrooney | 0:6aa743a332ae | 52 | bool readyToSendPowerOn; |
tmulrooney | 0:6aa743a332ae | 53 | float lastBatteryVoltage; |
tmulrooney | 0:6aa743a332ae | 54 | uint8_t lastKillStatus; |
tmulrooney | 0:6aa743a332ae | 55 | uint8_t lastPowerOn; |
tmulrooney | 0:6aa743a332ae | 56 | |
tmulrooney | 0:6aa743a332ae | 57 | //Function prototypes |
tmulrooney | 1:96c429800568 | 58 | void timeOfDay(); |
tmulrooney | 0:6aa743a332ae | 59 | void periodicSendTock(); |
tmulrooney | 0:6aa743a332ae | 60 | void batteryRead(); |
tmulrooney | 0:6aa743a332ae | 61 | void killStatusRead(); |
tmulrooney | 0:6aa743a332ae | 62 | void powerOnRead(); |
tmulrooney | 0:6aa743a332ae | 63 | void printError(mDot* dot, int32_t returnCode); |
tmulrooney | 0:6aa743a332ae | 64 | void printVersion(); |
tmulrooney | 0:6aa743a332ae | 65 | bool setFrequencySubBand(uint8_t subBand); |
tmulrooney | 0:6aa743a332ae | 66 | bool setNetworkName(const std::string name); |
tmulrooney | 0:6aa743a332ae | 67 | bool setNetworkPassphrase(const std::string passphrase); |
tmulrooney | 0:6aa743a332ae | 68 | bool setTxDataRate(const int dataRate); |
tmulrooney | 0:6aa743a332ae | 69 | bool setPower(uint8_t power); |
tmulrooney | 0:6aa743a332ae | 70 | bool setAck(uint8_t retries); |
tmulrooney | 0:6aa743a332ae | 71 | bool joinNetwork(); |
tmulrooney | 0:6aa743a332ae | 72 | bool send(const std::string text); |
tmulrooney | 0:6aa743a332ae | 73 | I2C i2c_mem(PTB3,PTB2); |
tmulrooney | 0:6aa743a332ae | 74 | //SPI spi_rf(PTD6, PTD7, PTD5, PTD4); |
tmulrooney | 0:6aa743a332ae | 75 | SPI spi_rf(PTD6, PTD7, PTD5); |
tmulrooney | 0:6aa743a332ae | 76 | |
tmulrooney | 0:6aa743a332ae | 77 | int i2cAddr = 0xAA; |
tmulrooney | 0:6aa743a332ae | 78 | char data[0x10]; |
tmulrooney | 0:6aa743a332ae | 79 | char data1[0x10]; |
tmulrooney | 0:6aa743a332ae | 80 | |
tmulrooney | 0:6aa743a332ae | 81 | int main() |
tmulrooney | 0:6aa743a332ae | 82 | { |
tmulrooney | 0:6aa743a332ae | 83 | bool configFail = false; |
tmulrooney | 0:6aa743a332ae | 84 | int ack; |
tmulrooney | 0:6aa743a332ae | 85 | |
tmulrooney | 0:6aa743a332ae | 86 | readyToSendI2C = false; |
tmulrooney | 0:6aa743a332ae | 87 | readyToSendBatteryVoltage = false; |
tmulrooney | 0:6aa743a332ae | 88 | readyToSendKillStatus = false; |
tmulrooney | 0:6aa743a332ae | 89 | readyToSendPowerOn = false; |
tmulrooney | 0:6aa743a332ae | 90 | lastBatteryVoltage = 0.0; |
tmulrooney | 0:6aa743a332ae | 91 | lastKillStatus = killStatus; |
tmulrooney | 0:6aa743a332ae | 92 | lastPowerOn = powerOn; |
tmulrooney | 0:6aa743a332ae | 93 | |
tmulrooney | 0:6aa743a332ae | 94 | i2c_mem.frequency(100000); |
tmulrooney | 0:6aa743a332ae | 95 | |
tmulrooney | 0:6aa743a332ae | 96 | // Setup the spi for 8 bit data, high steady state clock, |
tmulrooney | 0:6aa743a332ae | 97 | // second edge capture, with a 1MHz clock rate |
tmulrooney | 0:6aa743a332ae | 98 | // spi_rf.format(16,3); |
tmulrooney | 0:6aa743a332ae | 99 | spi_rf.format(16,0); |
tmulrooney | 0:6aa743a332ae | 100 | spi_rf.frequency(1000000); |
tmulrooney | 0:6aa743a332ae | 101 | spi_cs = 1; |
tmulrooney | 0:6aa743a332ae | 102 | |
tmulrooney | 0:6aa743a332ae | 103 | //Start LED startup sequence |
tmulrooney | 1:96c429800568 | 104 | ledTimer.attach(&timeOfDay, LED_TIME); |
tmulrooney | 0:6aa743a332ae | 105 | |
tmulrooney | 0:6aa743a332ae | 106 | pc.baud(115200); |
tmulrooney | 1:96c429800568 | 107 | // pc.printf("\r\n\r\n"); |
tmulrooney | 1:96c429800568 | 108 | // pc.printf("=====================================\r\n"); |
tmulrooney | 1:96c429800568 | 109 | // pc.printf("JLC MT Demo \r\n"); |
tmulrooney | 1:96c429800568 | 110 | // pc.printf("=====================================\r\n"); |
tmulrooney | 0:6aa743a332ae | 111 | |
tmulrooney | 0:6aa743a332ae | 112 | // get the mDot handle |
tmulrooney | 0:6aa743a332ae | 113 | // dot = mDot::getInstance(); |
tmulrooney | 1:96c429800568 | 114 | /* dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
tmulrooney | 0:6aa743a332ae | 115 | |
tmulrooney | 0:6aa743a332ae | 116 | //******************************************* |
tmulrooney | 0:6aa743a332ae | 117 | // configuration |
tmulrooney | 0:6aa743a332ae | 118 | //******************************************* |
tmulrooney | 0:6aa743a332ae | 119 | // reset to default config so we know what state we're in |
tmulrooney | 0:6aa743a332ae | 120 | dot->resetNetworkSession(); |
tmulrooney | 0:6aa743a332ae | 121 | dot->resetConfig(); |
tmulrooney | 0:6aa743a332ae | 122 | |
tmulrooney | 0:6aa743a332ae | 123 | // set up the mDot with our network information: frequency sub band, network name, and network password |
tmulrooney | 0:6aa743a332ae | 124 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
tmulrooney | 0:6aa743a332ae | 125 | configFail = !setFrequencySubBand(config_frequency_sub_band); |
tmulrooney | 0:6aa743a332ae | 126 | configFail |= !setNetworkName(config_network_name); |
tmulrooney | 0:6aa743a332ae | 127 | configFail |= !setNetworkPassphrase(config_network_pass); |
tmulrooney | 0:6aa743a332ae | 128 | configFail |= !setTxDataRate(mDot::SF_8); |
tmulrooney | 0:6aa743a332ae | 129 | configFail |= !setPower(20); // Reduce latency for 868 units |
tmulrooney | 0:6aa743a332ae | 130 | configFail |= !setAck(0); // Disable ack for less latency |
tmulrooney | 0:6aa743a332ae | 131 | |
tmulrooney | 0:6aa743a332ae | 132 | printf("Configration %s\r\n",configFail?"Failed":"Passed"); |
tmulrooney | 0:6aa743a332ae | 133 | |
tmulrooney | 0:6aa743a332ae | 134 | // save this configuration to the mDot's NVM |
tmulrooney | 0:6aa743a332ae | 135 | printf("saving config\r\n"); |
tmulrooney | 0:6aa743a332ae | 136 | if (! dot->saveConfig()) |
tmulrooney | 0:6aa743a332ae | 137 | { |
tmulrooney | 0:6aa743a332ae | 138 | logError("failed to save configuration"); |
tmulrooney | 0:6aa743a332ae | 139 | } |
tmulrooney | 0:6aa743a332ae | 140 | //******************************************* |
tmulrooney | 0:6aa743a332ae | 141 | // end of configuration |
tmulrooney | 0:6aa743a332ae | 142 | //******************************************* |
tmulrooney | 0:6aa743a332ae | 143 | |
tmulrooney | 0:6aa743a332ae | 144 | // keep trying to join the network |
tmulrooney | 0:6aa743a332ae | 145 | while (!joinNetwork()) |
tmulrooney | 0:6aa743a332ae | 146 | { |
tmulrooney | 0:6aa743a332ae | 147 | wait(200); |
tmulrooney | 0:6aa743a332ae | 148 | dot->resetNetworkSession(); |
tmulrooney | 0:6aa743a332ae | 149 | } |
tmulrooney | 0:6aa743a332ae | 150 | */ |
tmulrooney | 0:6aa743a332ae | 151 | // Stop LED startup sequence & configure them for operation |
tmulrooney | 0:6aa743a332ae | 152 | led_red = 1; |
tmulrooney | 0:6aa743a332ae | 153 | led_green = 1; |
tmulrooney | 0:6aa743a332ae | 154 | // led_blue = 1; |
tmulrooney | 0:6aa743a332ae | 155 | // start all paramter timers to sample analog and digital inputs |
tmulrooney | 0:6aa743a332ae | 156 | // batteryTimer.attach(batteryRead, BATTERY_TIME); |
tmulrooney | 0:6aa743a332ae | 157 | // killStatusTimer.attach(killStatusRead, KILL_STATUS_TIME); |
tmulrooney | 0:6aa743a332ae | 158 | // powerOnTimer.attach(powerOnRead, POWER_ON_TIME); |
tmulrooney | 1:96c429800568 | 159 | |
tmulrooney | 1:96c429800568 | 160 | /* initialize time and date */ |
tmulrooney | 1:96c429800568 | 161 | epoch =0; |
tmulrooney | 1:96c429800568 | 162 | |
tmulrooney | 1:96c429800568 | 163 | /* Send message to verify UART is connected properly */ |
tmulrooney | 1:96c429800568 | 164 | printVersion(); |
tmulrooney | 1:96c429800568 | 165 | sprintf(TransmitBuffer,"%s cmd>",time_string); |
tmulrooney | 1:96c429800568 | 166 | pc.printf(TransmitBuffer); |
tmulrooney | 0:6aa743a332ae | 167 | |
tmulrooney | 0:6aa743a332ae | 168 | while (1) |
tmulrooney | 0:6aa743a332ae | 169 | { |
tmulrooney | 1:96c429800568 | 170 | if (getLine() == 1) /* wait until command line complete */ |
tmulrooney | 1:96c429800568 | 171 | { |
tmulrooney | 1:96c429800568 | 172 | executeCmd(); |
tmulrooney | 1:96c429800568 | 173 | sprintf(TransmitBuffer,"%s cmd> ",time_string); |
tmulrooney | 1:96c429800568 | 174 | pc.printf("%s",TransmitBuffer); |
tmulrooney | 1:96c429800568 | 175 | } |
tmulrooney | 1:96c429800568 | 176 | #if 0 |
tmulrooney | 0:6aa743a332ae | 177 | // is there anything to send |
tmulrooney | 0:6aa743a332ae | 178 | if(readyToSendI2C) |
tmulrooney | 0:6aa743a332ae | 179 | { |
tmulrooney | 0:6aa743a332ae | 180 | #if 0 |
tmulrooney | 0:6aa743a332ae | 181 | data[0] = 0x12; |
tmulrooney | 0:6aa743a332ae | 182 | data[1] = 0x34; |
tmulrooney | 0:6aa743a332ae | 183 | data[2] = 0x56; |
tmulrooney | 0:6aa743a332ae | 184 | myI2CWrite(0,data,3); |
tmulrooney | 0:6aa743a332ae | 185 | myI2CRead(0,data1,3); |
tmulrooney | 0:6aa743a332ae | 186 | sprintf(latestData,"I2c %02X %02X %02X",data1[0], data1[1], data1[2]); |
tmulrooney | 0:6aa743a332ae | 187 | pc.printf("%s\r\n",latestData); |
tmulrooney | 0:6aa743a332ae | 188 | #endif |
tmulrooney | 0:6aa743a332ae | 189 | |
tmulrooney | 0:6aa743a332ae | 190 | #if 1 |
tmulrooney | 0:6aa743a332ae | 191 | // Send 0x8f, the command to read the WHOAMI register |
tmulrooney | 0:6aa743a332ae | 192 | data[0] = 0x12; |
tmulrooney | 1:96c429800568 | 193 | mySPIWrite(0x0A,data,1); |
tmulrooney | 1:96c429800568 | 194 | mySPIRead(0x0A,data1,1); |
tmulrooney | 1:96c429800568 | 195 | mySPIRead(0x42,data1,2); |
tmulrooney | 0:6aa743a332ae | 196 | // int spiData = spi_rf.write(0x0600 | 0x0000); |
tmulrooney | 0:6aa743a332ae | 197 | sprintf(latestData,"SPI %02X %02X",data1[0], data1[1]); |
tmulrooney | 0:6aa743a332ae | 198 | pc.printf("%s\r\n",latestData); |
tmulrooney | 0:6aa743a332ae | 199 | |
tmulrooney | 0:6aa743a332ae | 200 | #endif |
tmulrooney | 0:6aa743a332ae | 201 | send(latestData); |
tmulrooney | 0:6aa743a332ae | 202 | readyToSendI2C = false; |
tmulrooney | 0:6aa743a332ae | 203 | } |
tmulrooney | 0:6aa743a332ae | 204 | if(readyToSendBatteryVoltage) |
tmulrooney | 0:6aa743a332ae | 205 | { |
tmulrooney | 0:6aa743a332ae | 206 | sprintf(latestData,"Voltage %2.2f",(double)lastBatteryVoltage); |
tmulrooney | 0:6aa743a332ae | 207 | pc.printf("%s\r\n",latestData); |
tmulrooney | 0:6aa743a332ae | 208 | send(latestData); |
tmulrooney | 0:6aa743a332ae | 209 | readyToSendBatteryVoltage = false; |
tmulrooney | 0:6aa743a332ae | 210 | } |
tmulrooney | 0:6aa743a332ae | 211 | if(readyToSendKillStatus) |
tmulrooney | 0:6aa743a332ae | 212 | { |
tmulrooney | 0:6aa743a332ae | 213 | sprintf(latestData,"KillStatus %d",lastKillStatus); |
tmulrooney | 0:6aa743a332ae | 214 | pc.printf("%s\r\n",latestData); |
tmulrooney | 0:6aa743a332ae | 215 | send(latestData); |
tmulrooney | 0:6aa743a332ae | 216 | readyToSendKillStatus = false; |
tmulrooney | 0:6aa743a332ae | 217 | } |
tmulrooney | 0:6aa743a332ae | 218 | if(readyToSendPowerOn) |
tmulrooney | 0:6aa743a332ae | 219 | { |
tmulrooney | 0:6aa743a332ae | 220 | sprintf(latestData,"PowerOn %d",lastPowerOn); |
tmulrooney | 0:6aa743a332ae | 221 | pc.printf("%s\r\n",latestData); |
tmulrooney | 0:6aa743a332ae | 222 | send(latestData); |
tmulrooney | 0:6aa743a332ae | 223 | readyToSendPowerOn = false; |
tmulrooney | 0:6aa743a332ae | 224 | } |
tmulrooney | 1:96c429800568 | 225 | #endif |
tmulrooney | 0:6aa743a332ae | 226 | } |
tmulrooney | 0:6aa743a332ae | 227 | } |
tmulrooney | 0:6aa743a332ae | 228 | |
tmulrooney | 1:96c429800568 | 229 | void I2CWrite(uint16 addr, uint8 *data, int length) |
tmulrooney | 0:6aa743a332ae | 230 | { |
tmulrooney | 0:6aa743a332ae | 231 | int i; |
tmulrooney | 0:6aa743a332ae | 232 | char bytes[3]; |
tmulrooney | 0:6aa743a332ae | 233 | |
tmulrooney | 0:6aa743a332ae | 234 | for(i=0; i< length; i++,addr++) |
tmulrooney | 0:6aa743a332ae | 235 | { |
tmulrooney | 1:96c429800568 | 236 | bytes[0] = addr >> 8; |
tmulrooney | 0:6aa743a332ae | 237 | bytes[1] = addr & 0xFF; |
tmulrooney | 0:6aa743a332ae | 238 | bytes[2] = data[i]; |
tmulrooney | 0:6aa743a332ae | 239 | i2c_mem.write(i2cAddr, bytes, 3); /* write address and one byte */ |
tmulrooney | 0:6aa743a332ae | 240 | while(i2c_mem.write(i2cAddr, bytes, 0) != 0); /* wait till write complete */ |
tmulrooney | 0:6aa743a332ae | 241 | } |
tmulrooney | 1:96c429800568 | 242 | return; |
tmulrooney | 0:6aa743a332ae | 243 | } |
tmulrooney | 0:6aa743a332ae | 244 | |
tmulrooney | 1:96c429800568 | 245 | void I2CRead(uint16 addr, uint8 *data, int length) |
tmulrooney | 0:6aa743a332ae | 246 | { |
tmulrooney | 0:6aa743a332ae | 247 | char bytes[2]; |
tmulrooney | 0:6aa743a332ae | 248 | |
tmulrooney | 1:96c429800568 | 249 | bytes[0] = addr >> 8; |
tmulrooney | 0:6aa743a332ae | 250 | bytes[1] = addr & 0xFF; |
tmulrooney | 1:96c429800568 | 251 | // sprintf(TransmitBuffer,"read I2C %04X %02X %04X\r\n",regAddress, bytes[0], bytes[1]); |
tmulrooney | 1:96c429800568 | 252 | // pc.printf(TransmitBuffer); |
tmulrooney | 0:6aa743a332ae | 253 | i2c_mem.write(i2cAddr, bytes, 2,true); /* write address with repeated start */ |
tmulrooney | 1:96c429800568 | 254 | i2c_mem.read(i2cAddr, (char *)data, length); /* read all bytes */ |
tmulrooney | 1:96c429800568 | 255 | return; |
tmulrooney | 0:6aa743a332ae | 256 | } |
tmulrooney | 0:6aa743a332ae | 257 | |
tmulrooney | 0:6aa743a332ae | 258 | |
tmulrooney | 1:96c429800568 | 259 | void SPIWrite(uint16 addr, uint8 *data, int length) |
tmulrooney | 0:6aa743a332ae | 260 | { |
tmulrooney | 0:6aa743a332ae | 261 | int i; |
tmulrooney | 0:6aa743a332ae | 262 | |
tmulrooney | 0:6aa743a332ae | 263 | for(i=0;i<length;i++,addr++) |
tmulrooney | 0:6aa743a332ae | 264 | { |
tmulrooney | 0:6aa743a332ae | 265 | spi_cs = 0; |
tmulrooney | 0:6aa743a332ae | 266 | data[i] = spi_rf.write( (addr << 8) | data[i] | 0x8000); |
tmulrooney | 0:6aa743a332ae | 267 | spi_cs = 1; |
tmulrooney | 0:6aa743a332ae | 268 | } |
tmulrooney | 0:6aa743a332ae | 269 | } |
tmulrooney | 0:6aa743a332ae | 270 | |
tmulrooney | 1:96c429800568 | 271 | void SPIRead(uint16 addr, uint8 *data, int length) |
tmulrooney | 0:6aa743a332ae | 272 | { |
tmulrooney | 0:6aa743a332ae | 273 | int i; |
tmulrooney | 0:6aa743a332ae | 274 | |
tmulrooney | 0:6aa743a332ae | 275 | for(i=0;i<length;i++,addr++) |
tmulrooney | 0:6aa743a332ae | 276 | { |
tmulrooney | 0:6aa743a332ae | 277 | spi_cs = 0; |
tmulrooney | 0:6aa743a332ae | 278 | data[i] = spi_rf.write( (addr << 8) | data[i] | 0x0000); |
tmulrooney | 0:6aa743a332ae | 279 | spi_cs = 1; |
tmulrooney | 0:6aa743a332ae | 280 | } |
tmulrooney | 1:96c429800568 | 281 | } |
tmulrooney | 0:6aa743a332ae | 282 | |
tmulrooney | 1:96c429800568 | 283 | void timeOfDay() |
tmulrooney | 0:6aa743a332ae | 284 | { |
tmulrooney | 1:96c429800568 | 285 | epoch++; /* update time of day */ |
tmulrooney | 0:6aa743a332ae | 286 | led_red = !led_red; |
tmulrooney | 0:6aa743a332ae | 287 | // led_green = !led_green; |
tmulrooney | 0:6aa743a332ae | 288 | // led_blue = !led_blue; |
tmulrooney | 0:6aa743a332ae | 289 | readyToSendI2C = true; |
tmulrooney | 0:6aa743a332ae | 290 | } |
tmulrooney | 0:6aa743a332ae | 291 | |
tmulrooney | 0:6aa743a332ae | 292 | void batteryRead() |
tmulrooney | 0:6aa743a332ae | 293 | { |
tmulrooney | 0:6aa743a332ae | 294 | lastBatteryVoltage = batteryVoltage * (float)3.3; |
tmulrooney | 0:6aa743a332ae | 295 | readyToSendBatteryVoltage = true; |
tmulrooney | 0:6aa743a332ae | 296 | } |
tmulrooney | 0:6aa743a332ae | 297 | |
tmulrooney | 0:6aa743a332ae | 298 | void killStatusRead() |
tmulrooney | 0:6aa743a332ae | 299 | { |
tmulrooney | 0:6aa743a332ae | 300 | lastKillStatus = killStatus; |
tmulrooney | 0:6aa743a332ae | 301 | readyToSendKillStatus = true; |
tmulrooney | 0:6aa743a332ae | 302 | } |
tmulrooney | 0:6aa743a332ae | 303 | |
tmulrooney | 0:6aa743a332ae | 304 | void powerOnRead() |
tmulrooney | 0:6aa743a332ae | 305 | { |
tmulrooney | 0:6aa743a332ae | 306 | lastPowerOn = powerOn; |
tmulrooney | 0:6aa743a332ae | 307 | readyToSendPowerOn = true; |
tmulrooney | 0:6aa743a332ae | 308 | } |
tmulrooney | 0:6aa743a332ae | 309 | |
tmulrooney | 0:6aa743a332ae | 310 | bool setFrequencySubBand(uint8_t subBand) |
tmulrooney | 0:6aa743a332ae | 311 | { |
tmulrooney | 0:6aa743a332ae | 312 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 313 | |
tmulrooney | 0:6aa743a332ae | 314 | printf("Setting frequency sub band to '%d'...\r\n", subBand); |
tmulrooney | 0:6aa743a332ae | 315 | if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) { |
tmulrooney | 0:6aa743a332ae | 316 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 317 | return false; |
tmulrooney | 0:6aa743a332ae | 318 | } |
tmulrooney | 0:6aa743a332ae | 319 | return true; |
tmulrooney | 0:6aa743a332ae | 320 | */ |
tmulrooney | 0:6aa743a332ae | 321 | } |
tmulrooney | 0:6aa743a332ae | 322 | |
tmulrooney | 0:6aa743a332ae | 323 | bool setNetworkName(const std::string name) |
tmulrooney | 0:6aa743a332ae | 324 | { |
tmulrooney | 0:6aa743a332ae | 325 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 326 | |
tmulrooney | 0:6aa743a332ae | 327 | printf("Setting network name to '%s'...\r\n", name.c_str()); |
tmulrooney | 0:6aa743a332ae | 328 | if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK) |
tmulrooney | 0:6aa743a332ae | 329 | { |
tmulrooney | 0:6aa743a332ae | 330 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 331 | return false; |
tmulrooney | 0:6aa743a332ae | 332 | } |
tmulrooney | 0:6aa743a332ae | 333 | return true; |
tmulrooney | 0:6aa743a332ae | 334 | */} |
tmulrooney | 0:6aa743a332ae | 335 | |
tmulrooney | 0:6aa743a332ae | 336 | bool setNetworkPassphrase(const std::string passphrase) |
tmulrooney | 0:6aa743a332ae | 337 | { |
tmulrooney | 0:6aa743a332ae | 338 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 339 | |
tmulrooney | 0:6aa743a332ae | 340 | printf("Setting passphrase to '%s'...\r\n", passphrase.c_str()); |
tmulrooney | 0:6aa743a332ae | 341 | if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK) |
tmulrooney | 0:6aa743a332ae | 342 | { |
tmulrooney | 0:6aa743a332ae | 343 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 344 | return false; |
tmulrooney | 0:6aa743a332ae | 345 | } |
tmulrooney | 0:6aa743a332ae | 346 | return true; |
tmulrooney | 0:6aa743a332ae | 347 | */} |
tmulrooney | 0:6aa743a332ae | 348 | |
tmulrooney | 0:6aa743a332ae | 349 | bool setTxDataRate(const int dataRate) |
tmulrooney | 0:6aa743a332ae | 350 | { |
tmulrooney | 0:6aa743a332ae | 351 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 352 | |
tmulrooney | 0:6aa743a332ae | 353 | printf("Setting TX Spreading factor to %d...\r\n",dataRate); |
tmulrooney | 0:6aa743a332ae | 354 | if ((returnCode = dot->setTxDataRate(dataRate)) != mDot::MDOT_OK) |
tmulrooney | 0:6aa743a332ae | 355 | { |
tmulrooney | 0:6aa743a332ae | 356 | logError("Failed To Set Tx Datarate %d:%s", returnCode, mDot::getReturnCodeString(returnCode).c_str()); |
tmulrooney | 0:6aa743a332ae | 357 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 358 | return false; |
tmulrooney | 0:6aa743a332ae | 359 | } |
tmulrooney | 0:6aa743a332ae | 360 | return true; |
tmulrooney | 0:6aa743a332ae | 361 | */} |
tmulrooney | 0:6aa743a332ae | 362 | |
tmulrooney | 0:6aa743a332ae | 363 | bool setPower(uint8_t power) |
tmulrooney | 0:6aa743a332ae | 364 | { |
tmulrooney | 0:6aa743a332ae | 365 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 366 | printf("Setting tx power to '%d'...\r\n", power); |
tmulrooney | 0:6aa743a332ae | 367 | if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) |
tmulrooney | 0:6aa743a332ae | 368 | { |
tmulrooney | 0:6aa743a332ae | 369 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 370 | return false; |
tmulrooney | 0:6aa743a332ae | 371 | } |
tmulrooney | 0:6aa743a332ae | 372 | return true; |
tmulrooney | 0:6aa743a332ae | 373 | */} |
tmulrooney | 0:6aa743a332ae | 374 | |
tmulrooney | 0:6aa743a332ae | 375 | bool joinNetwork() |
tmulrooney | 0:6aa743a332ae | 376 | { |
tmulrooney | 0:6aa743a332ae | 377 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 378 | printf("\r\nJoining network...\r\n"); |
tmulrooney | 0:6aa743a332ae | 379 | if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) |
tmulrooney | 0:6aa743a332ae | 380 | { |
tmulrooney | 0:6aa743a332ae | 381 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 382 | return false; |
tmulrooney | 0:6aa743a332ae | 383 | } |
tmulrooney | 0:6aa743a332ae | 384 | printf("Network Joined!\r\n"); |
tmulrooney | 0:6aa743a332ae | 385 | return true; |
tmulrooney | 0:6aa743a332ae | 386 | */} |
tmulrooney | 0:6aa743a332ae | 387 | |
tmulrooney | 0:6aa743a332ae | 388 | bool setAck(uint8_t retries) |
tmulrooney | 0:6aa743a332ae | 389 | { |
tmulrooney | 0:6aa743a332ae | 390 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 391 | printf("Setting ack to '%d'...\r\n", retries); |
tmulrooney | 0:6aa743a332ae | 392 | if ((returnCode = dot->setAck(retries)) != mDot::MDOT_OK) |
tmulrooney | 0:6aa743a332ae | 393 | { |
tmulrooney | 0:6aa743a332ae | 394 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 395 | return false; |
tmulrooney | 0:6aa743a332ae | 396 | } |
tmulrooney | 0:6aa743a332ae | 397 | return true; |
tmulrooney | 0:6aa743a332ae | 398 | */} |
tmulrooney | 0:6aa743a332ae | 399 | |
tmulrooney | 0:6aa743a332ae | 400 | bool send(const std::string text) |
tmulrooney | 0:6aa743a332ae | 401 | { |
tmulrooney | 0:6aa743a332ae | 402 | /* int32_t returnCode; |
tmulrooney | 0:6aa743a332ae | 403 | uint32_t timeTillSend = dot->getNextTxMs(); |
tmulrooney | 0:6aa743a332ae | 404 | if (timeTillSend != 0) { |
tmulrooney | 0:6aa743a332ae | 405 | printf("waiting %lu ms to send\r\n", timeTillSend); |
tmulrooney | 0:6aa743a332ae | 406 | return false; |
tmulrooney | 0:6aa743a332ae | 407 | } |
tmulrooney | 0:6aa743a332ae | 408 | |
tmulrooney | 0:6aa743a332ae | 409 | printf("Sending data... "); |
tmulrooney | 0:6aa743a332ae | 410 | std::vector<uint8_t> data(text.begin(), text.end()); |
tmulrooney | 0:6aa743a332ae | 411 | if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK) |
tmulrooney | 0:6aa743a332ae | 412 | { |
tmulrooney | 0:6aa743a332ae | 413 | printError(dot, returnCode); |
tmulrooney | 0:6aa743a332ae | 414 | return false; |
tmulrooney | 0:6aa743a332ae | 415 | } |
tmulrooney | 0:6aa743a332ae | 416 | printf("Data sent!\r\n"); |
tmulrooney | 0:6aa743a332ae | 417 | return true; |
tmulrooney | 0:6aa743a332ae | 418 | */} |
tmulrooney | 0:6aa743a332ae | 419 | |
tmulrooney | 0:6aa743a332ae | 420 | void printError(mDot* dot, int32_t returnCode) |
tmulrooney | 0:6aa743a332ae | 421 | { |
tmulrooney | 0:6aa743a332ae | 422 | // std::string error = mDot::getReturnCodeString(returnCode) + " - " + dot->getLastError(); |
tmulrooney | 0:6aa743a332ae | 423 | // printf("%s\r\n", error.c_str()); |
tmulrooney | 0:6aa743a332ae | 424 | } |
tmulrooney | 0:6aa743a332ae | 425 |