Workshop demo program
Dependencies: PinDetect mbed LoRaWAN-lib SX1272Lib
app/app.cpp@15:07f7e9ce7e21, 2017-05-02 (annotated)
- Committer:
- Brandond200
- Date:
- Tue May 02 13:30:48 2017 +0000
- Revision:
- 15:07f7e9ce7e21
- Parent:
- 14:7d092ebf7250
- Child:
- 16:4667c0c0b48b
set max number of blinks queued. Added shmit trigger for alarm so now it works when going up and down, not just up.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sergei11522 | 7:d7cdd6804277 | 1 | #include "main.h" |
sergei11522 | 7:d7cdd6804277 | 2 | #include "mbed.h" |
sergei11522 | 7:d7cdd6804277 | 3 | #include "DebounceIn.h" |
sergei11522 | 7:d7cdd6804277 | 4 | #include "PinNames.h" |
Brandond200 | 8:fb8b53c490e1 | 5 | #include "timer.h" |
Brandond200 | 10:2eb7b1614bb8 | 6 | #include <math.h> |
Brandond200 | 8:fb8b53c490e1 | 7 | #include "app.h" |
Brandond200 | 8:fb8b53c490e1 | 8 | #include "SerialDisplay.h" |
sergei11522 | 7:d7cdd6804277 | 9 | |
sergei11522 | 7:d7cdd6804277 | 10 | //initialize our sensors |
sergei11522 | 7:d7cdd6804277 | 11 | DebounceIn button(D6); |
sergei11522 | 7:d7cdd6804277 | 12 | DigitalOut led(A1); |
sergei11522 | 7:d7cdd6804277 | 13 | AnalogIn rotary(A3); //the potentiometer/rotary sensor |
sergei11522 | 7:d7cdd6804277 | 14 | |
Brandond200 | 12:67fcab1e915a | 15 | //changable alarm threashold |
Brandond200 | 12:67fcab1e915a | 16 | #define ALARM_THRESHOLD 100 /*alarm triggered over this percentage*/ |
Brandond200 | 10:2eb7b1614bb8 | 17 | |
Brandond200 | 12:67fcab1e915a | 18 | //constatnts |
Brandond200 | 10:2eb7b1614bb8 | 19 | #define BYTE_TO_PERCENT 655.35/*divisor to convert 2 byte number into number between 0 and 100*/ |
Brandond200 | 10:2eb7b1614bb8 | 20 | #define TWO_HUNDRED_MILLISECONDS 200 * 1000 /*200 miliseconds in microseconds */ |
Brandond200 | 12:67fcab1e915a | 21 | |
Brandond200 | 12:67fcab1e915a | 22 | //myDevies codes |
Brandond200 | 12:67fcab1e915a | 23 | #define ROTATION_DATA_CHANNEL 0 /* myDevices channel to send rotation position on*/ |
Brandond200 | 10:2eb7b1614bb8 | 24 | #define ROTATION_DATA_TYPE 2 /*2 = Analog Input */ |
Brandond200 | 12:67fcab1e915a | 25 | #define ALARM_DATA_CHANNEL 1 /* myDevices channel to send alarm status on*/ |
Brandond200 | 10:2eb7b1614bb8 | 26 | #define ALARM_DATA_TYPE 0 /*0 = Digital Input */ |
Brandond200 | 12:67fcab1e915a | 27 | |
Brandond200 | 12:67fcab1e915a | 28 | //settings to not change |
Brandond200 | 10:2eb7b1614bb8 | 29 | #define MAX_DATA_LENGTH 64 |
Brandond200 | 10:2eb7b1614bb8 | 30 | #define BLINK_COUNT_EVENT_TRANSMIT 1 |
Brandond200 | 10:2eb7b1614bb8 | 31 | #define BLINK_COUNT_TIMER_TRANSMIT 2 |
Brandond200 | 10:2eb7b1614bb8 | 32 | #define BLINK_COUNT_DOWNLINK_CONFIRMATION 3 |
Brandond200 | 12:67fcab1e915a | 33 | #define BLINK_COUNT_JOINED 5 |
Brandond200 | 10:2eb7b1614bb8 | 34 | |
Brandond200 | 10:2eb7b1614bb8 | 35 | //network variables |
Brandond200 | 10:2eb7b1614bb8 | 36 | bool just_joined = true; //used to determine if this is the first time through the loop |
Brandond200 | 10:2eb7b1614bb8 | 37 | uint8_t data[MAX_DATA_LENGTH] = {0}; //data that will be transmitted |
Brandond200 | 10:2eb7b1614bb8 | 38 | uint8_t dataLength = 0; //length of data that will be transmitted |
Brandond200 | 10:2eb7b1614bb8 | 39 | |
Brandond200 | 10:2eb7b1614bb8 | 40 | //application variables |
Brandond200 | 10:2eb7b1614bb8 | 41 | bool button_pressed = false; //last state of the button |
Brandond200 | 12:67fcab1e915a | 42 | bool alarmThresholdTriggered = false; //last state on the alarm |
Brandond200 | 10:2eb7b1614bb8 | 43 | uint8_t flashCount = 0; //how many more times to flash the LED |
Brandond200 | 10:2eb7b1614bb8 | 44 | |
Brandond200 | 10:2eb7b1614bb8 | 45 | //timers |
Brandond200 | 10:2eb7b1614bb8 | 46 | TimerEvent_t txTimer; //timer so schedule heartbeat transmissions |
Brandond200 | 10:2eb7b1614bb8 | 47 | TimerEvent_t ledTimer; //timer to blink LED |
Brandond200 | 8:fb8b53c490e1 | 48 | |
sergei11522 | 7:d7cdd6804277 | 49 | |
sergei11522 | 7:d7cdd6804277 | 50 | void onDownlinkConfirmation(){ |
Brandond200 | 10:2eb7b1614bb8 | 51 | flashLED(BLINK_COUNT_DOWNLINK_CONFIRMATION); |
sergei11522 | 7:d7cdd6804277 | 52 | } |
sergei11522 | 7:d7cdd6804277 | 53 | |
sergei11522 | 7:d7cdd6804277 | 54 | void start(){ |
Brandond200 | 8:fb8b53c490e1 | 55 | //any initialization here |
Brandond200 | 10:2eb7b1614bb8 | 56 | |
Brandond200 | 10:2eb7b1614bb8 | 57 | //initilize LED flash timer |
Brandond200 | 8:fb8b53c490e1 | 58 | TimerInit(&ledTimer, flashLEDCallback); |
Brandond200 | 10:2eb7b1614bb8 | 59 | TimerSetValue( &ledTimer, TWO_HUNDRED_MILLISECONDS ); |
sergei11522 | 7:d7cdd6804277 | 60 | } |
sergei11522 | 7:d7cdd6804277 | 61 | |
sergei11522 | 7:d7cdd6804277 | 62 | //this loop will start to be called as soon as the device successfully joins the network |
sergei11522 | 7:d7cdd6804277 | 63 | void loop(){ |
sergei11522 | 7:d7cdd6804277 | 64 | //the loop is called as soon as the device joins. Flash the led 10 times when this happens |
sergei11522 | 7:d7cdd6804277 | 65 | if (just_joined){ |
sergei11522 | 7:d7cdd6804277 | 66 | just_joined = false; |
Brandond200 | 10:2eb7b1614bb8 | 67 | flashLED(BLINK_COUNT_JOINED); |
sergei11522 | 7:d7cdd6804277 | 68 | } |
sergei11522 | 7:d7cdd6804277 | 69 | |
Brandond200 | 12:67fcab1e915a | 70 | //check if button was just pressed |
Brandond200 | 12:67fcab1e915a | 71 | checkButton(); |
Brandond200 | 15:07f7e9ce7e21 | 72 | |
Brandond200 | 12:67fcab1e915a | 73 | } |
Brandond200 | 12:67fcab1e915a | 74 | |
Brandond200 | 12:67fcab1e915a | 75 | void checkButton(void){ |
sergei11522 | 7:d7cdd6804277 | 76 | //button is pressed. Trigger once so that if we hold the button this block doesnt continously execute |
Brandond200 | 10:2eb7b1614bb8 | 77 | if (button == 1 && button_pressed == false){ |
sergei11522 | 7:d7cdd6804277 | 78 | button_pressed = true; |
Brandond200 | 10:2eb7b1614bb8 | 79 | flashLED(BLINK_COUNT_EVENT_TRANSMIT); |
Brandond200 | 10:2eb7b1614bb8 | 80 | transmitData(rotary.read_u16()); |
sergei11522 | 7:d7cdd6804277 | 81 | } else if (button == 0){ |
sergei11522 | 7:d7cdd6804277 | 82 | button_pressed = false; |
sergei11522 | 7:d7cdd6804277 | 83 | } |
Brandond200 | 12:67fcab1e915a | 84 | } |
Brandond200 | 12:67fcab1e915a | 85 | |
Brandond200 | 12:67fcab1e915a | 86 | //check if value passed in should trigger the alarm, if so, transmit |
Brandond200 | 12:67fcab1e915a | 87 | void checkAlarm(void){ |
Brandond200 | 12:67fcab1e915a | 88 | uint16_t position = rotary.read_u16(); |
Brandond200 | 12:67fcab1e915a | 89 | //check if should trigger alarm |
Brandond200 | 15:07f7e9ce7e21 | 90 | if(position / BYTE_TO_PERCENT > (ALARM_THRESHOLD + 1)){ |
Brandond200 | 12:67fcab1e915a | 91 | //if the alarm has not already been triggered |
Brandond200 | 12:67fcab1e915a | 92 | if(!alarmThresholdTriggered){ |
Brandond200 | 12:67fcab1e915a | 93 | alarmThresholdTriggered = true;//set the alarm to triggered |
Brandond200 | 12:67fcab1e915a | 94 | flashLED(BLINK_COUNT_EVENT_TRANSMIT); |
Brandond200 | 12:67fcab1e915a | 95 | transmitData(position);//transmit data |
Brandond200 | 12:67fcab1e915a | 96 | } |
Brandond200 | 12:67fcab1e915a | 97 | } |
Brandond200 | 15:07f7e9ce7e21 | 98 | else if(position / BYTE_TO_PERCENT < (ALARM_THRESHOLD - 1)){ |
Brandond200 | 15:07f7e9ce7e21 | 99 | if(alarmThresholdTriggered){ |
Brandond200 | 15:07f7e9ce7e21 | 100 | //alarm just changed state to not triggered |
Brandond200 | 15:07f7e9ce7e21 | 101 | flashLED(BLINK_COUNT_EVENT_TRANSMIT); |
Brandond200 | 15:07f7e9ce7e21 | 102 | transmitData(position);//transmit data |
Brandond200 | 15:07f7e9ce7e21 | 103 | alarmThresholdTriggered = false; |
Brandond200 | 15:07f7e9ce7e21 | 104 | } |
Brandond200 | 12:67fcab1e915a | 105 | } |
Brandond200 | 12:67fcab1e915a | 106 | } |
Brandond200 | 12:67fcab1e915a | 107 | |
Brandond200 | 12:67fcab1e915a | 108 | //start heartbeat timer. pass in how often in seconds it should transmit |
Brandond200 | 12:67fcab1e915a | 109 | void startHeartbeat(uint8_t time){ |
Brandond200 | 12:67fcab1e915a | 110 | TimerInit(&txTimer, timerCallback); |
Brandond200 | 12:67fcab1e915a | 111 | TimerSetValue( &txTimer, time * 1000 * 1000); |
Brandond200 | 12:67fcab1e915a | 112 | TimerStart(&txTimer); |
Brandond200 | 10:2eb7b1614bb8 | 113 | } |
Brandond200 | 10:2eb7b1614bb8 | 114 | |
Brandond200 | 10:2eb7b1614bb8 | 115 | //Transmit rotory position and alarm status |
Brandond200 | 12:67fcab1e915a | 116 | void transmitData(uint16_t position){ |
Brandond200 | 11:88afd7663dc4 | 117 | addRotaryData(position); //add the rotary position to data |
Brandond200 | 15:07f7e9ce7e21 | 118 | |
Brandond200 | 11:88afd7663dc4 | 119 | setDataToSend(data, dataLength); //set data to be transmitted |
Brandond200 | 11:88afd7663dc4 | 120 | sendLoraTransmission(); //queue transmission |
Brandond200 | 11:88afd7663dc4 | 121 | dataLength = 0; //reset data length so new data starts back at begining |
Brandond200 | 10:2eb7b1614bb8 | 122 | } |
Brandond200 | 10:2eb7b1614bb8 | 123 | |
Brandond200 | 10:2eb7b1614bb8 | 124 | //read the rotory position and add to data to transmit |
Brandond200 | 10:2eb7b1614bb8 | 125 | void addRotaryData(uint16_t positionArg){ |
Brandond200 | 10:2eb7b1614bb8 | 126 | uint16_t position = positionArg / (BYTE_TO_PERCENT / 100); |
Brandond200 | 10:2eb7b1614bb8 | 127 | |
Brandond200 | 10:2eb7b1614bb8 | 128 | data[dataLength++] = ROTATION_DATA_CHANNEL; |
Brandond200 | 10:2eb7b1614bb8 | 129 | data[dataLength++] = ROTATION_DATA_TYPE; |
Brandond200 | 11:88afd7663dc4 | 130 | data[dataLength++] = position >> 8; //add the upper byte of the rotory position |
Brandond200 | 10:2eb7b1614bb8 | 131 | data[dataLength++] = position & 0xFF; //add the lower byte of the rotory position |
Brandond200 | 10:2eb7b1614bb8 | 132 | } |
Brandond200 | 10:2eb7b1614bb8 | 133 | |
Brandond200 | 10:2eb7b1614bb8 | 134 | //read the rotory position and add alarm status to data to transmit |
Brandond200 | 10:2eb7b1614bb8 | 135 | void addAlarmData(uint16_t positionArg){ |
Brandond200 | 10:2eb7b1614bb8 | 136 | int16_t position = positionArg / BYTE_TO_PERCENT; |
Brandond200 | 10:2eb7b1614bb8 | 137 | |
Brandond200 | 10:2eb7b1614bb8 | 138 | data[dataLength++] = ALARM_DATA_CHANNEL; |
Brandond200 | 10:2eb7b1614bb8 | 139 | data[dataLength++] = ALARM_DATA_TYPE; |
Brandond200 | 15:07f7e9ce7e21 | 140 | data[dataLength++] = (uint8_t)position >= ALARM_THRESHOLD; //add binary for it alarm triggered or not |
Brandond200 | 10:2eb7b1614bb8 | 141 | } |
Brandond200 | 10:2eb7b1614bb8 | 142 | |
Brandond200 | 10:2eb7b1614bb8 | 143 | //start flash LED |
Brandond200 | 10:2eb7b1614bb8 | 144 | void flashLED(uint8_t times){ |
Brandond200 | 11:88afd7663dc4 | 145 | if(flashCount == 0){ //if it is not currently flashing |
Brandond200 | 13:d2cccfdb615c | 146 | TimerStart(&ledTimer); //start timer to toggle LED state |
Brandond200 | 11:88afd7663dc4 | 147 | } |
Brandond200 | 11:88afd7663dc4 | 148 | flashCount += times; //set the number of times to flash |
Brandond200 | 15:07f7e9ce7e21 | 149 | //make sure it is not set to high |
Brandond200 | 15:07f7e9ce7e21 | 150 | if(flashCount > 10){ |
Brandond200 | 15:07f7e9ce7e21 | 151 | flashCount = 10; |
Brandond200 | 15:07f7e9ce7e21 | 152 | } |
Brandond200 | 13:d2cccfdb615c | 153 | led.write(1); //turn on LED |
Brandond200 | 10:2eb7b1614bb8 | 154 | } |
Brandond200 | 10:2eb7b1614bb8 | 155 | |
Brandond200 | 10:2eb7b1614bb8 | 156 | //callback to change state of LED for blinking |
Brandond200 | 10:2eb7b1614bb8 | 157 | void flashLEDCallback(){ |
Brandond200 | 10:2eb7b1614bb8 | 158 | //if the LED is on, turn it off |
Brandond200 | 10:2eb7b1614bb8 | 159 | if(led.read() == 1){ |
Brandond200 | 10:2eb7b1614bb8 | 160 | led.write(0); |
Brandond200 | 10:2eb7b1614bb8 | 161 | flashCount--; |
Brandond200 | 10:2eb7b1614bb8 | 162 | //if done flashing turn off the timer |
Brandond200 | 10:2eb7b1614bb8 | 163 | if(flashCount <= 0){ |
Brandond200 | 10:2eb7b1614bb8 | 164 | TimerStop(&ledTimer); |
Brandond200 | 10:2eb7b1614bb8 | 165 | } |
Brandond200 | 10:2eb7b1614bb8 | 166 | } |
Brandond200 | 10:2eb7b1614bb8 | 167 | //if LED is off, turn it on |
Brandond200 | 10:2eb7b1614bb8 | 168 | else{ |
Brandond200 | 10:2eb7b1614bb8 | 169 | led.write(1); |
Brandond200 | 10:2eb7b1614bb8 | 170 | } |
Brandond200 | 10:2eb7b1614bb8 | 171 | } |
Brandond200 | 10:2eb7b1614bb8 | 172 | |
Brandond200 | 12:67fcab1e915a | 173 | |
Brandond200 | 12:67fcab1e915a | 174 | //callback for transmission timer |
Brandond200 | 12:67fcab1e915a | 175 | void timerCallback(){ |
Brandond200 | 12:67fcab1e915a | 176 | flashLED(BLINK_COUNT_TIMER_TRANSMIT); |
Brandond200 | 12:67fcab1e915a | 177 | transmitData(rotary.read_u16()); |
Brandond200 | 8:fb8b53c490e1 | 178 | } |