![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Workshop demo program
Dependencies: PinDetect mbed LoRaWAN-lib SX1272Lib
Diff: app/app.cpp
- Revision:
- 18:326069443137
- Parent:
- 16:4667c0c0b48b
- Child:
- 19:31fc997c460b
--- a/app/app.cpp Tue May 02 21:07:30 2017 +0000 +++ b/app/app.cpp Wed May 03 15:55:42 2017 +0000 @@ -34,6 +34,7 @@ //network variables bool just_joined = true; //used to determine if this is the first time through the loop +volatile bool sending = false; uint8_t data[MAX_DATA_LENGTH] = {0}; //data that will be transmitted uint8_t dataLength = 0; //length of data that will be transmitted @@ -41,6 +42,7 @@ bool button_pressed = false; //last state of the button bool alarmThresholdTriggered = false; //last state on the alarm uint8_t flashCount = 0; //how many more times to flash the LED +uint8_t checkCount = 0; //timers TimerEvent_t txTimer; //timer so schedule heartbeat transmissions @@ -49,11 +51,16 @@ void onDownlinkConfirmation(){ flashLED(BLINK_COUNT_DOWNLINK_CONFIRMATION); + sending = false; +} + +void onDownlinkUnconfirmed(){ + sending = false; } void start(){ //any initialization here - + //initilize LED flash timer TimerInit(&ledTimer, flashLEDCallback); TimerSetValue( &ledTimer, TWO_HUNDRED_MILLISECONDS ); @@ -69,7 +76,7 @@ //check if button was just pressed checkButton(); - + } void checkButton(void){ @@ -85,22 +92,26 @@ //check if value passed in should trigger the alarm, if so, transmit void checkAlarm(void){ - uint16_t position = rotary.read_u16(); - //check if should trigger alarm - if(position / BYTE_TO_PERCENT > (ALARM_THRESHOLD)){ - //if the alarm has not already been triggered - if(!alarmThresholdTriggered){ - alarmThresholdTriggered = true;//set the alarm to triggered - flashLED(BLINK_COUNT_EVENT_TRANSMIT); - transmitData(position);//transmit data + checkCount++; + if(checkCount > 5000){ + checkCount = 0; + uint16_t position = rotary.read_u16(); + //check if should trigger alarm + if(position / BYTE_TO_PERCENT > (ALARM_THRESHOLD)){ + //if the alarm has not already been triggered + if(!alarmThresholdTriggered){ + alarmThresholdTriggered = true;//set the alarm to triggered + flashLED(BLINK_COUNT_EVENT_TRANSMIT); + transmitData(position);//transmit data + } } - } - else if(position / BYTE_TO_PERCENT < (ALARM_THRESHOLD - 1)){ - if(alarmThresholdTriggered){ - //alarm just changed state to not triggered - flashLED(BLINK_COUNT_EVENT_TRANSMIT); - transmitData(position);//transmit data - alarmThresholdTriggered = false; + else if(position / BYTE_TO_PERCENT < (ALARM_THRESHOLD - 1)){ + if(alarmThresholdTriggered){ + //alarm just changed state to not triggered + flashLED(BLINK_COUNT_EVENT_TRANSMIT); + transmitData(position);//transmit data + alarmThresholdTriggered = false; + } } } } @@ -114,11 +125,14 @@ //Transmit rotory position and alarm status void transmitData(uint16_t position){ - addRotaryData(position); //add the rotary position to data + if(!sending){ + sending = true; + addRotaryData(position); //add the rotary position to data - setDataToSend(data, dataLength); //set data to be transmitted - sendLoraTransmission(); //queue transmission - dataLength = 0; //reset data length so new data starts back at begining + setDataToSend(data, dataLength); //set data to be transmitted + sendLoraTransmission(); //queue transmission + dataLength = 0; //reset data length so new data starts back at begining + } } //read the rotory position and add to data to transmit