Workshop demo program

Dependencies:   PinDetect mbed LoRaWAN-lib SX1272Lib

Committer:
Brandond200
Date:
Fri Apr 28 17:01:49 2017 +0000
Revision:
13:d2cccfdb615c
Parent:
12:67fcab1e915a
Child:
14:7d092ebf7250
turned LED back on

Who changed what in which revision?

UserRevisionLine numberNew 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 12:67fcab1e915a 72 }
Brandond200 12:67fcab1e915a 73
Brandond200 12:67fcab1e915a 74 void checkButton(void){
sergei11522 7:d7cdd6804277 75 //button is pressed. Trigger once so that if we hold the button this block doesnt continously execute
Brandond200 10:2eb7b1614bb8 76 if (button == 1 && button_pressed == false){
sergei11522 7:d7cdd6804277 77 button_pressed = true;
Brandond200 10:2eb7b1614bb8 78 flashLED(BLINK_COUNT_EVENT_TRANSMIT);
Brandond200 10:2eb7b1614bb8 79 transmitData(rotary.read_u16());
sergei11522 7:d7cdd6804277 80 } else if (button == 0){
sergei11522 7:d7cdd6804277 81 button_pressed = false;
sergei11522 7:d7cdd6804277 82 }
Brandond200 12:67fcab1e915a 83 }
Brandond200 12:67fcab1e915a 84
Brandond200 12:67fcab1e915a 85 //check if value passed in should trigger the alarm, if so, transmit
Brandond200 12:67fcab1e915a 86 void checkAlarm(void){
Brandond200 12:67fcab1e915a 87 uint16_t position = rotary.read_u16();
Brandond200 12:67fcab1e915a 88 //check if should trigger alarm
Brandond200 12:67fcab1e915a 89 if(position / BYTE_TO_PERCENT > ALARM_THRESHOLD){
Brandond200 12:67fcab1e915a 90 //if the alarm has not already been triggered
Brandond200 12:67fcab1e915a 91 if(!alarmThresholdTriggered){
Brandond200 12:67fcab1e915a 92 alarmThresholdTriggered = true;//set the alarm to triggered
Brandond200 12:67fcab1e915a 93 flashLED(BLINK_COUNT_EVENT_TRANSMIT);
Brandond200 12:67fcab1e915a 94 transmitData(position);//transmit data
Brandond200 12:67fcab1e915a 95 }
Brandond200 12:67fcab1e915a 96 }
Brandond200 12:67fcab1e915a 97 else if(alarmThresholdTriggered){
Brandond200 12:67fcab1e915a 98 alarmThresholdTriggered = false;
Brandond200 12:67fcab1e915a 99 }
Brandond200 12:67fcab1e915a 100 }
Brandond200 12:67fcab1e915a 101
Brandond200 12:67fcab1e915a 102 //start heartbeat timer. pass in how often in seconds it should transmit
Brandond200 12:67fcab1e915a 103 void startHeartbeat(uint8_t time){
Brandond200 12:67fcab1e915a 104 TimerInit(&txTimer, timerCallback);
Brandond200 12:67fcab1e915a 105 TimerSetValue( &txTimer, time * 1000 * 1000);
Brandond200 12:67fcab1e915a 106 TimerStart(&txTimer);
Brandond200 10:2eb7b1614bb8 107 }
Brandond200 10:2eb7b1614bb8 108
Brandond200 10:2eb7b1614bb8 109 //Transmit rotory position and alarm status
Brandond200 12:67fcab1e915a 110 void transmitData(uint16_t position){
Brandond200 11:88afd7663dc4 111 addRotaryData(position); //add the rotary position to data
Brandond200 11:88afd7663dc4 112 addAlarmData(position); //add alarm status to data
Brandond200 11:88afd7663dc4 113 setDataToSend(data, dataLength); //set data to be transmitted
Brandond200 11:88afd7663dc4 114 sendLoraTransmission(); //queue transmission
Brandond200 11:88afd7663dc4 115 dataLength = 0; //reset data length so new data starts back at begining
Brandond200 10:2eb7b1614bb8 116 }
Brandond200 10:2eb7b1614bb8 117
Brandond200 10:2eb7b1614bb8 118 //read the rotory position and add to data to transmit
Brandond200 10:2eb7b1614bb8 119 void addRotaryData(uint16_t positionArg){
Brandond200 10:2eb7b1614bb8 120 uint16_t position = positionArg / (BYTE_TO_PERCENT / 100);
Brandond200 10:2eb7b1614bb8 121
Brandond200 10:2eb7b1614bb8 122 data[dataLength++] = ROTATION_DATA_CHANNEL;
Brandond200 10:2eb7b1614bb8 123 data[dataLength++] = ROTATION_DATA_TYPE;
Brandond200 11:88afd7663dc4 124 data[dataLength++] = position >> 8; //add the upper byte of the rotory position
Brandond200 10:2eb7b1614bb8 125 data[dataLength++] = position & 0xFF; //add the lower byte of the rotory position
Brandond200 10:2eb7b1614bb8 126 }
Brandond200 10:2eb7b1614bb8 127
Brandond200 10:2eb7b1614bb8 128 //read the rotory position and add alarm status to data to transmit
Brandond200 10:2eb7b1614bb8 129 void addAlarmData(uint16_t positionArg){
Brandond200 10:2eb7b1614bb8 130 int16_t position = positionArg / BYTE_TO_PERCENT;
Brandond200 10:2eb7b1614bb8 131
Brandond200 10:2eb7b1614bb8 132 data[dataLength++] = ALARM_DATA_CHANNEL;
Brandond200 10:2eb7b1614bb8 133 data[dataLength++] = ALARM_DATA_TYPE;
Brandond200 10:2eb7b1614bb8 134 data[dataLength++] = (uint8_t)position >= ALARM_THRESHOLD; //add binary for it allar triggered or not
Brandond200 10:2eb7b1614bb8 135 }
Brandond200 10:2eb7b1614bb8 136
Brandond200 10:2eb7b1614bb8 137 //start flash LED
Brandond200 10:2eb7b1614bb8 138 void flashLED(uint8_t times){
Brandond200 11:88afd7663dc4 139 if(flashCount == 0){ //if it is not currently flashing
Brandond200 13:d2cccfdb615c 140 TimerStart(&ledTimer); //start timer to toggle LED state
Brandond200 11:88afd7663dc4 141 }
Brandond200 11:88afd7663dc4 142 flashCount += times; //set the number of times to flash
Brandond200 13:d2cccfdb615c 143 led.write(1); //turn on LED
Brandond200 10:2eb7b1614bb8 144 }
Brandond200 10:2eb7b1614bb8 145
Brandond200 10:2eb7b1614bb8 146 //callback to change state of LED for blinking
Brandond200 10:2eb7b1614bb8 147 void flashLEDCallback(){
Brandond200 10:2eb7b1614bb8 148 //if the LED is on, turn it off
Brandond200 10:2eb7b1614bb8 149 if(led.read() == 1){
Brandond200 10:2eb7b1614bb8 150 led.write(0);
Brandond200 10:2eb7b1614bb8 151 flashCount--;
Brandond200 10:2eb7b1614bb8 152 //if done flashing turn off the timer
Brandond200 10:2eb7b1614bb8 153 if(flashCount <= 0){
Brandond200 10:2eb7b1614bb8 154 TimerStop(&ledTimer);
Brandond200 10:2eb7b1614bb8 155 }
Brandond200 10:2eb7b1614bb8 156 }
Brandond200 10:2eb7b1614bb8 157 //if LED is off, turn it on
Brandond200 10:2eb7b1614bb8 158 else{
Brandond200 10:2eb7b1614bb8 159 led.write(1);
Brandond200 10:2eb7b1614bb8 160 }
Brandond200 10:2eb7b1614bb8 161 }
Brandond200 10:2eb7b1614bb8 162
Brandond200 12:67fcab1e915a 163
Brandond200 12:67fcab1e915a 164 //callback for transmission timer
Brandond200 12:67fcab1e915a 165 void timerCallback(){
Brandond200 12:67fcab1e915a 166 flashLED(BLINK_COUNT_TIMER_TRANSMIT);
Brandond200 12:67fcab1e915a 167 transmitData(rotary.read_u16());
Brandond200 8:fb8b53c490e1 168 }