JLC test

Dependencies:   PinDetect libmDot mbed-rtos mbed

Committer:
tmulrooney
Date:
Wed Jan 27 00:57:12 2016 +0000
Revision:
1:96c429800568
Parent:
0:6aa743a332ae
Child:
2:376af6a70e8a
added CLI interface to I2C and SPI

Who changed what in which revision?

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