Easylube
Dependencies: DS1820 libmDot mbed-rtos mbed
main.cpp@1:0fe7bd229e28, 2016-08-23 (annotated)
- Committer:
- TataLora
- Date:
- Tue Aug 23 08:47:09 2016 +0000
- Revision:
- 1:0fe7bd229e28
- Parent:
- 0:7e7bd4f937c7
- Child:
- 2:26ffaed90e4f
uid uitlezen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TataLora | 0:7e7bd4f937c7 | 1 | #include "mbed.h" |
TataLora | 0:7e7bd4f937c7 | 2 | #include "mDot.h" |
TataLora | 0:7e7bd4f937c7 | 3 | #include "MTSLog.h" |
TataLora | 0:7e7bd4f937c7 | 4 | #include <string> |
TataLora | 0:7e7bd4f937c7 | 5 | #include <vector> |
TataLora | 0:7e7bd4f937c7 | 6 | #include <algorithm> |
TataLora | 0:7e7bd4f937c7 | 7 | |
TataLora | 0:7e7bd4f937c7 | 8 | typedef enum {NONE, STARTING, TURNING, ERR, ALIVE, NO_SIGNAL, POWER_UP} activity_type; |
TataLora | 0:7e7bd4f937c7 | 9 | |
TataLora | 0:7e7bd4f937c7 | 10 | |
TataLora | 0:7e7bd4f937c7 | 11 | |
TataLora | 0:7e7bd4f937c7 | 12 | // these options must match the settings on your Conduit |
TataLora | 0:7e7bd4f937c7 | 13 | // uncomment the following lines and edit their values to match your configuration |
TataLora | 0:7e7bd4f937c7 | 14 | static std::string config_network_name = "MultiTech"; |
TataLora | 0:7e7bd4f937c7 | 15 | static std::string config_network_pass = "MultiTech"; |
TataLora | 0:7e7bd4f937c7 | 16 | static uint8_t config_frequency_sub_band = 1; |
TataLora | 0:7e7bd4f937c7 | 17 | |
TataLora | 0:7e7bd4f937c7 | 18 | Timer t; |
TataLora | 0:7e7bd4f937c7 | 19 | int isPulseActive = 0; |
TataLora | 0:7e7bd4f937c7 | 20 | int isWaiting = 1; |
TataLora | 0:7e7bd4f937c7 | 21 | activity_type activity = NONE; |
TataLora | 0:7e7bd4f937c7 | 22 | int pulseCount = 0; |
TataLora | 0:7e7bd4f937c7 | 23 | bool initial = 1; |
TataLora | 0:7e7bd4f937c7 | 24 | |
TataLora | 1:0fe7bd229e28 | 25 | void PrintID() |
TataLora | 1:0fe7bd229e28 | 26 | { |
TataLora | 1:0fe7bd229e28 | 27 | mDot* dot = mDot::getInstance(); |
TataLora | 1:0fe7bd229e28 | 28 | std::vector<uint8_t> unitID = dot->getDeviceId(); |
TataLora | 1:0fe7bd229e28 | 29 | char EUI[20]; |
TataLora | 1:0fe7bd229e28 | 30 | char Id[2]; |
TataLora | 1:0fe7bd229e28 | 31 | string Ident; |
TataLora | 1:0fe7bd229e28 | 32 | |
TataLora | 1:0fe7bd229e28 | 33 | for(int n = 0; n < 8; n++) |
TataLora | 1:0fe7bd229e28 | 34 | { |
TataLora | 1:0fe7bd229e28 | 35 | sprintf(Id,"%02X",unitID[n]); |
TataLora | 1:0fe7bd229e28 | 36 | strcat (EUI,Id); |
TataLora | 1:0fe7bd229e28 | 37 | } |
TataLora | 1:0fe7bd229e28 | 38 | Ident = EUI; |
TataLora | 1:0fe7bd229e28 | 39 | logInfo("UID = %s \r\n",Ident.c_str()); |
TataLora | 1:0fe7bd229e28 | 40 | } |
TataLora | 1:0fe7bd229e28 | 41 | |
TataLora | 0:7e7bd4f937c7 | 42 | void ConfigLora(mDot* dot) |
TataLora | 0:7e7bd4f937c7 | 43 | { |
TataLora | 0:7e7bd4f937c7 | 44 | int32_t ret; |
TataLora | 0:7e7bd4f937c7 | 45 | Timer configTimer; |
TataLora | 0:7e7bd4f937c7 | 46 | configTimer.reset(); |
TataLora | 0:7e7bd4f937c7 | 47 | configTimer.start(); |
TataLora | 0:7e7bd4f937c7 | 48 | //******************************************* |
TataLora | 0:7e7bd4f937c7 | 49 | // configuration |
TataLora | 0:7e7bd4f937c7 | 50 | //******************************************* |
TataLora | 0:7e7bd4f937c7 | 51 | // reset to default config so we know what state we're in |
TataLora | 0:7e7bd4f937c7 | 52 | dot->resetConfig(); |
TataLora | 0:7e7bd4f937c7 | 53 | |
TataLora | 0:7e7bd4f937c7 | 54 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
TataLora | 0:7e7bd4f937c7 | 55 | |
TataLora | 1:0fe7bd229e28 | 56 | PrintID(); |
TataLora | 1:0fe7bd229e28 | 57 | |
TataLora | 0:7e7bd4f937c7 | 58 | // frequency sub band is only applicable in the 915 (US) frequency band |
TataLora | 0:7e7bd4f937c7 | 59 | // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band |
TataLora | 0:7e7bd4f937c7 | 60 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
TataLora | 0:7e7bd4f937c7 | 61 | logInfo("setting frequency sub band"); |
TataLora | 0:7e7bd4f937c7 | 62 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 63 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 64 | } |
TataLora | 0:7e7bd4f937c7 | 65 | |
TataLora | 0:7e7bd4f937c7 | 66 | logInfo("setting network name"); |
TataLora | 0:7e7bd4f937c7 | 67 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 68 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 69 | } |
TataLora | 0:7e7bd4f937c7 | 70 | |
TataLora | 0:7e7bd4f937c7 | 71 | logInfo("setting network password"); |
TataLora | 0:7e7bd4f937c7 | 72 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 73 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 74 | } |
TataLora | 0:7e7bd4f937c7 | 75 | |
TataLora | 0:7e7bd4f937c7 | 76 | // a higher spreading factor allows for longer range but lower throughput |
TataLora | 0:7e7bd4f937c7 | 77 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
TataLora | 0:7e7bd4f937c7 | 78 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
TataLora | 0:7e7bd4f937c7 | 79 | logInfo("setting TX spreading factor"); |
TataLora | 0:7e7bd4f937c7 | 80 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 81 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 82 | } |
TataLora | 0:7e7bd4f937c7 | 83 | |
TataLora | 0:7e7bd4f937c7 | 84 | // request receive confirmation of packets from the gateway |
TataLora | 0:7e7bd4f937c7 | 85 | logInfo("enabling ACKs"); |
TataLora | 0:7e7bd4f937c7 | 86 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 87 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 88 | } |
TataLora | 0:7e7bd4f937c7 | 89 | |
TataLora | 0:7e7bd4f937c7 | 90 | // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping |
TataLora | 0:7e7bd4f937c7 | 91 | logInfo("setting join mode to AUTO_OTA"); |
TataLora | 0:7e7bd4f937c7 | 92 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 93 | logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 94 | } |
TataLora | 0:7e7bd4f937c7 | 95 | |
TataLora | 0:7e7bd4f937c7 | 96 | // save this configuration to the mDot's NVM |
TataLora | 0:7e7bd4f937c7 | 97 | logInfo("saving config"); |
TataLora | 0:7e7bd4f937c7 | 98 | if (! dot->saveConfig()) { |
TataLora | 0:7e7bd4f937c7 | 99 | logError("failed to save configuration"); |
TataLora | 0:7e7bd4f937c7 | 100 | } |
TataLora | 0:7e7bd4f937c7 | 101 | //******************************************* |
TataLora | 0:7e7bd4f937c7 | 102 | // end of configuration |
TataLora | 0:7e7bd4f937c7 | 103 | //******************************************* |
TataLora | 0:7e7bd4f937c7 | 104 | |
TataLora | 0:7e7bd4f937c7 | 105 | logInfo("Configuratie duurde: %f seconds", configTimer.read()); |
TataLora | 0:7e7bd4f937c7 | 106 | configTimer.stop(); |
TataLora | 0:7e7bd4f937c7 | 107 | } |
TataLora | 0:7e7bd4f937c7 | 108 | |
TataLora | 0:7e7bd4f937c7 | 109 | |
TataLora | 0:7e7bd4f937c7 | 110 | void resetPulse(mDot* dot) |
TataLora | 0:7e7bd4f937c7 | 111 | { |
TataLora | 0:7e7bd4f937c7 | 112 | int32_t ret; |
TataLora | 0:7e7bd4f937c7 | 113 | std::vector<uint8_t> data; |
TataLora | 0:7e7bd4f937c7 | 114 | std::string data_Starten = "Starten"; |
TataLora | 0:7e7bd4f937c7 | 115 | std::string data_Draaien = "Draaien"; |
TataLora | 0:7e7bd4f937c7 | 116 | std::string data_Error = "Error!"; |
TataLora | 0:7e7bd4f937c7 | 117 | std::string data_Alive = "Alive"; |
TataLora | 0:7e7bd4f937c7 | 118 | std::string data_NoSignal = "NoSignal"; |
TataLora | 0:7e7bd4f937c7 | 119 | std::string data_PowerUp = "PowerUp"; |
TataLora | 0:7e7bd4f937c7 | 120 | std::string data_Onbekend = "Onbekend"; |
TataLora | 0:7e7bd4f937c7 | 121 | bool stuurBericht = 1; |
TataLora | 0:7e7bd4f937c7 | 122 | |
TataLora | 0:7e7bd4f937c7 | 123 | isPulseActive = 0; |
TataLora | 0:7e7bd4f937c7 | 124 | |
TataLora | 0:7e7bd4f937c7 | 125 | if(activity == NONE) |
TataLora | 0:7e7bd4f937c7 | 126 | { |
TataLora | 0:7e7bd4f937c7 | 127 | logInfo("Einde pulse %d stuks - Onbekend", pulseCount); |
TataLora | 0:7e7bd4f937c7 | 128 | // format data for sending to the gateway |
TataLora | 0:7e7bd4f937c7 | 129 | for (std::string::iterator it = data_Onbekend.begin(); it != data_Onbekend.end(); it++) |
TataLora | 0:7e7bd4f937c7 | 130 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 131 | stuurBericht = 0; |
TataLora | 0:7e7bd4f937c7 | 132 | } |
TataLora | 0:7e7bd4f937c7 | 133 | |
TataLora | 0:7e7bd4f937c7 | 134 | if(activity == STARTING) |
TataLora | 0:7e7bd4f937c7 | 135 | { |
TataLora | 0:7e7bd4f937c7 | 136 | logInfo("Einde pulse %d stuks - Starten", pulseCount); |
TataLora | 0:7e7bd4f937c7 | 137 | // format data for sending to the gateway |
TataLora | 0:7e7bd4f937c7 | 138 | for (std::string::iterator it = data_Starten.begin(); it != data_Starten.end(); it++) |
TataLora | 0:7e7bd4f937c7 | 139 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 140 | } |
TataLora | 0:7e7bd4f937c7 | 141 | |
TataLora | 0:7e7bd4f937c7 | 142 | if(activity == TURNING) |
TataLora | 0:7e7bd4f937c7 | 143 | { |
TataLora | 0:7e7bd4f937c7 | 144 | logInfo("Einde pulse %d stuks - Draaien", pulseCount); |
TataLora | 0:7e7bd4f937c7 | 145 | // format data for sending to the gateway |
TataLora | 0:7e7bd4f937c7 | 146 | for (std::string::iterator it = data_Draaien.begin(); it != data_Draaien.end(); it++) |
TataLora | 0:7e7bd4f937c7 | 147 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 148 | } |
TataLora | 0:7e7bd4f937c7 | 149 | |
TataLora | 0:7e7bd4f937c7 | 150 | if(activity == ERR) |
TataLora | 0:7e7bd4f937c7 | 151 | { |
TataLora | 0:7e7bd4f937c7 | 152 | logInfo("Einde pulse %d stuks - Storing", pulseCount); |
TataLora | 0:7e7bd4f937c7 | 153 | // format data for sending to the gateway |
TataLora | 0:7e7bd4f937c7 | 154 | for (std::string::iterator it = data_Error.begin(); it != data_Error.end(); it++) |
TataLora | 0:7e7bd4f937c7 | 155 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 156 | } |
TataLora | 0:7e7bd4f937c7 | 157 | |
TataLora | 0:7e7bd4f937c7 | 158 | if(activity == NO_SIGNAL) |
TataLora | 0:7e7bd4f937c7 | 159 | { |
TataLora | 0:7e7bd4f937c7 | 160 | logInfo("Einde pulse %d stuks - No signal", pulseCount); |
TataLora | 0:7e7bd4f937c7 | 161 | // format data for sending to the gateway |
TataLora | 0:7e7bd4f937c7 | 162 | for (std::string::iterator it = data_NoSignal.begin(); it != data_NoSignal.end(); it++) |
TataLora | 0:7e7bd4f937c7 | 163 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 164 | } |
TataLora | 0:7e7bd4f937c7 | 165 | |
TataLora | 0:7e7bd4f937c7 | 166 | if(activity == ALIVE) |
TataLora | 0:7e7bd4f937c7 | 167 | { |
TataLora | 0:7e7bd4f937c7 | 168 | logInfo("Einde pulse %d stuks - Alive", pulseCount); |
TataLora | 0:7e7bd4f937c7 | 169 | // format data for sending to the gateway |
TataLora | 0:7e7bd4f937c7 | 170 | for (std::string::iterator it = data_Alive.begin(); it != data_Alive.end(); it++) |
TataLora | 0:7e7bd4f937c7 | 171 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 172 | } |
TataLora | 0:7e7bd4f937c7 | 173 | |
TataLora | 0:7e7bd4f937c7 | 174 | if(activity == POWER_UP) |
TataLora | 0:7e7bd4f937c7 | 175 | { |
TataLora | 0:7e7bd4f937c7 | 176 | logInfo("Einde pulse %d stuks - Power up", pulseCount); |
TataLora | 0:7e7bd4f937c7 | 177 | // format data for sending to the gateway |
TataLora | 0:7e7bd4f937c7 | 178 | for (std::string::iterator it = data_PowerUp.begin(); it != data_PowerUp.end(); it++) |
TataLora | 0:7e7bd4f937c7 | 179 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 180 | } |
TataLora | 0:7e7bd4f937c7 | 181 | |
TataLora | 0:7e7bd4f937c7 | 182 | pulseCount = 0; |
TataLora | 0:7e7bd4f937c7 | 183 | isWaiting = 1; |
TataLora | 0:7e7bd4f937c7 | 184 | |
TataLora | 0:7e7bd4f937c7 | 185 | if(!stuurBericht) |
TataLora | 0:7e7bd4f937c7 | 186 | { |
TataLora | 0:7e7bd4f937c7 | 187 | logInfo("Geen bericht versturen"); |
TataLora | 0:7e7bd4f937c7 | 188 | return; |
TataLora | 0:7e7bd4f937c7 | 189 | } |
TataLora | 0:7e7bd4f937c7 | 190 | |
TataLora | 0:7e7bd4f937c7 | 191 | ConfigLora(dot); |
TataLora | 0:7e7bd4f937c7 | 192 | |
TataLora | 0:7e7bd4f937c7 | 193 | // join the network if not joined |
TataLora | 0:7e7bd4f937c7 | 194 | /*if (!dot->getNetworkJoinStatus()) { |
TataLora | 0:7e7bd4f937c7 | 195 | logInfo("network not joined, joining network"); |
TataLora | 0:7e7bd4f937c7 | 196 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 197 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 198 | } |
TataLora | 0:7e7bd4f937c7 | 199 | } |
TataLora | 0:7e7bd4f937c7 | 200 | if (dot->getNetworkJoinStatus()) { |
TataLora | 0:7e7bd4f937c7 | 201 | // send the data |
TataLora | 0:7e7bd4f937c7 | 202 | // ACKs are enabled by default, so we're expecting to get one back |
TataLora | 0:7e7bd4f937c7 | 203 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 204 | logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 205 | } else { |
TataLora | 0:7e7bd4f937c7 | 206 | logInfo("successfully sent data to gateway"); |
TataLora | 0:7e7bd4f937c7 | 207 | } |
TataLora | 0:7e7bd4f937c7 | 208 | } |
TataLora | 0:7e7bd4f937c7 | 209 | |
TataLora | 0:7e7bd4f937c7 | 210 | */ |
TataLora | 0:7e7bd4f937c7 | 211 | |
TataLora | 0:7e7bd4f937c7 | 212 | logInfo("joining network"); |
TataLora | 0:7e7bd4f937c7 | 213 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 214 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 215 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
TataLora | 0:7e7bd4f937c7 | 216 | //osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
TataLora | 0:7e7bd4f937c7 | 217 | |
TataLora | 0:7e7bd4f937c7 | 218 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
TataLora | 0:7e7bd4f937c7 | 219 | uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000; |
TataLora | 0:7e7bd4f937c7 | 220 | logInfo("going to sleep..."); |
TataLora | 0:7e7bd4f937c7 | 221 | |
TataLora | 0:7e7bd4f937c7 | 222 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
TataLora | 0:7e7bd4f937c7 | 223 | } |
TataLora | 0:7e7bd4f937c7 | 224 | |
TataLora | 0:7e7bd4f937c7 | 225 | while (true) { |
TataLora | 0:7e7bd4f937c7 | 226 | // send the data to the gateway |
TataLora | 0:7e7bd4f937c7 | 227 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
TataLora | 0:7e7bd4f937c7 | 228 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
TataLora | 0:7e7bd4f937c7 | 229 | } else { |
TataLora | 0:7e7bd4f937c7 | 230 | logInfo("successfully sent data to gateway"); |
TataLora | 0:7e7bd4f937c7 | 231 | return; |
TataLora | 0:7e7bd4f937c7 | 232 | } |
TataLora | 0:7e7bd4f937c7 | 233 | |
TataLora | 0:7e7bd4f937c7 | 234 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
TataLora | 0:7e7bd4f937c7 | 235 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
TataLora | 0:7e7bd4f937c7 | 236 | } |
TataLora | 0:7e7bd4f937c7 | 237 | |
TataLora | 0:7e7bd4f937c7 | 238 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
TataLora | 0:7e7bd4f937c7 | 239 | //uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000; |
TataLora | 0:7e7bd4f937c7 | 240 | //logInfo("going to sleep..."); |
TataLora | 0:7e7bd4f937c7 | 241 | } |
TataLora | 0:7e7bd4f937c7 | 242 | |
TataLora | 0:7e7bd4f937c7 | 243 | int main() { |
TataLora | 0:7e7bd4f937c7 | 244 | mDot* dot; |
TataLora | 0:7e7bd4f937c7 | 245 | DigitalIn input(PA_0); |
TataLora | 0:7e7bd4f937c7 | 246 | DigitalIn data0(PB_0); |
TataLora | 0:7e7bd4f937c7 | 247 | DigitalIn data1(PB_1); |
TataLora | 0:7e7bd4f937c7 | 248 | DigitalIn data2(PA_1); |
TataLora | 0:7e7bd4f937c7 | 249 | |
TataLora | 0:7e7bd4f937c7 | 250 | // get a mDot handle |
TataLora | 0:7e7bd4f937c7 | 251 | dot = mDot::getInstance(); |
TataLora | 0:7e7bd4f937c7 | 252 | |
TataLora | 0:7e7bd4f937c7 | 253 | // print library version information |
TataLora | 0:7e7bd4f937c7 | 254 | logInfo("V: %s", dot->getId().c_str()); |
TataLora | 0:7e7bd4f937c7 | 255 | |
TataLora | 0:7e7bd4f937c7 | 256 | |
TataLora | 0:7e7bd4f937c7 | 257 | if(input) |
TataLora | 0:7e7bd4f937c7 | 258 | { |
TataLora | 0:7e7bd4f937c7 | 259 | uint8_t data = 0; |
TataLora | 0:7e7bd4f937c7 | 260 | if(data0) |
TataLora | 0:7e7bd4f937c7 | 261 | { |
TataLora | 0:7e7bd4f937c7 | 262 | data |= (1 << 0); |
TataLora | 0:7e7bd4f937c7 | 263 | } |
TataLora | 0:7e7bd4f937c7 | 264 | if(data1) |
TataLora | 0:7e7bd4f937c7 | 265 | { |
TataLora | 0:7e7bd4f937c7 | 266 | data |= (1 << 1); |
TataLora | 0:7e7bd4f937c7 | 267 | } |
TataLora | 0:7e7bd4f937c7 | 268 | if(data2) |
TataLora | 0:7e7bd4f937c7 | 269 | { |
TataLora | 0:7e7bd4f937c7 | 270 | data |= (1 << 2); |
TataLora | 0:7e7bd4f937c7 | 271 | } |
TataLora | 0:7e7bd4f937c7 | 272 | |
TataLora | 0:7e7bd4f937c7 | 273 | if(data == 0) |
TataLora | 0:7e7bd4f937c7 | 274 | { |
TataLora | 0:7e7bd4f937c7 | 275 | activity = POWER_UP; |
TataLora | 0:7e7bd4f937c7 | 276 | logInfo("Starting with data: POWER_UP"); |
TataLora | 0:7e7bd4f937c7 | 277 | } |
TataLora | 0:7e7bd4f937c7 | 278 | |
TataLora | 0:7e7bd4f937c7 | 279 | if(data == 1) |
TataLora | 0:7e7bd4f937c7 | 280 | { |
TataLora | 0:7e7bd4f937c7 | 281 | activity = ERR; |
TataLora | 0:7e7bd4f937c7 | 282 | logInfo("Starting with data: ERROR"); |
TataLora | 0:7e7bd4f937c7 | 283 | } |
TataLora | 0:7e7bd4f937c7 | 284 | |
TataLora | 0:7e7bd4f937c7 | 285 | if(data == 2) |
TataLora | 0:7e7bd4f937c7 | 286 | { |
TataLora | 0:7e7bd4f937c7 | 287 | activity = TURNING; |
TataLora | 0:7e7bd4f937c7 | 288 | logInfo("Starting with data: TURNING"); |
TataLora | 0:7e7bd4f937c7 | 289 | } |
TataLora | 0:7e7bd4f937c7 | 290 | |
TataLora | 0:7e7bd4f937c7 | 291 | if(data == 3) |
TataLora | 0:7e7bd4f937c7 | 292 | { |
TataLora | 0:7e7bd4f937c7 | 293 | activity = STARTING; |
TataLora | 0:7e7bd4f937c7 | 294 | logInfo("Starting with data: STARTING"); |
TataLora | 0:7e7bd4f937c7 | 295 | } |
TataLora | 0:7e7bd4f937c7 | 296 | if(data == 4) |
TataLora | 0:7e7bd4f937c7 | 297 | { |
TataLora | 0:7e7bd4f937c7 | 298 | activity = ALIVE; |
TataLora | 0:7e7bd4f937c7 | 299 | logInfo("Starting with data: ALIVE"); |
TataLora | 0:7e7bd4f937c7 | 300 | } |
TataLora | 0:7e7bd4f937c7 | 301 | resetPulse(dot); |
TataLora | 0:7e7bd4f937c7 | 302 | } |
TataLora | 0:7e7bd4f937c7 | 303 | else |
TataLora | 0:7e7bd4f937c7 | 304 | { |
TataLora | 0:7e7bd4f937c7 | 305 | activity = NO_SIGNAL; |
TataLora | 0:7e7bd4f937c7 | 306 | logInfo("Signal-pin was niet hoog"); |
TataLora | 0:7e7bd4f937c7 | 307 | resetPulse(dot); |
TataLora | 0:7e7bd4f937c7 | 308 | } |
TataLora | 0:7e7bd4f937c7 | 309 | |
TataLora | 0:7e7bd4f937c7 | 310 | //32 minuten slapen of wachten op een interrupt |
TataLora | 0:7e7bd4f937c7 | 311 | dot->sleep(1920, mDot::RTC_ALARM_OR_INTERRUPT, true); |
TataLora | 0:7e7bd4f937c7 | 312 | |
TataLora | 0:7e7bd4f937c7 | 313 | |
TataLora | 0:7e7bd4f937c7 | 314 | |
TataLora | 0:7e7bd4f937c7 | 315 | //logInfo("Pulsen inspecteren"); |
TataLora | 0:7e7bd4f937c7 | 316 | |
TataLora | 0:7e7bd4f937c7 | 317 | /*t.start(); |
TataLora | 0:7e7bd4f937c7 | 318 | |
TataLora | 0:7e7bd4f937c7 | 319 | int oldValue = 2; |
TataLora | 0:7e7bd4f937c7 | 320 | |
TataLora | 0:7e7bd4f937c7 | 321 | while (true) { |
TataLora | 0:7e7bd4f937c7 | 322 | int newLedValue = input; |
TataLora | 0:7e7bd4f937c7 | 323 | int newValue = input.read(); |
TataLora | 0:7e7bd4f937c7 | 324 | int diff; |
TataLora | 0:7e7bd4f937c7 | 325 | |
TataLora | 0:7e7bd4f937c7 | 326 | if(newValue > oldValue) |
TataLora | 0:7e7bd4f937c7 | 327 | { |
TataLora | 0:7e7bd4f937c7 | 328 | diff = newValue - oldValue; |
TataLora | 0:7e7bd4f937c7 | 329 | } |
TataLora | 0:7e7bd4f937c7 | 330 | else |
TataLora | 0:7e7bd4f937c7 | 331 | { |
TataLora | 0:7e7bd4f937c7 | 332 | diff = oldValue - newValue; |
TataLora | 0:7e7bd4f937c7 | 333 | } |
TataLora | 0:7e7bd4f937c7 | 334 | |
TataLora | 0:7e7bd4f937c7 | 335 | |
TataLora | 0:7e7bd4f937c7 | 336 | if(isPulseActive) |
TataLora | 0:7e7bd4f937c7 | 337 | { |
TataLora | 0:7e7bd4f937c7 | 338 | if(t.read_ms() > 1200) |
TataLora | 0:7e7bd4f937c7 | 339 | { |
TataLora | 0:7e7bd4f937c7 | 340 | if(pulseCount < 3) |
TataLora | 0:7e7bd4f937c7 | 341 | { |
TataLora | 0:7e7bd4f937c7 | 342 | activity = NONE; |
TataLora | 0:7e7bd4f937c7 | 343 | } |
TataLora | 0:7e7bd4f937c7 | 344 | |
TataLora | 0:7e7bd4f937c7 | 345 | t.stop(); |
TataLora | 0:7e7bd4f937c7 | 346 | resetPulse(dot); |
TataLora | 0:7e7bd4f937c7 | 347 | //printf("Slapen\r\n"); |
TataLora | 0:7e7bd4f937c7 | 348 | // go to deepsleep and wake up automatically sleep_time seconds later |
TataLora | 0:7e7bd4f937c7 | 349 | dot->sleep(20, mDot::INTERRUPT, true); |
TataLora | 0:7e7bd4f937c7 | 350 | } |
TataLora | 0:7e7bd4f937c7 | 351 | else if(activity == ERR) |
TataLora | 0:7e7bd4f937c7 | 352 | { |
TataLora | 0:7e7bd4f937c7 | 353 | if(pulseCount > 5) |
TataLora | 0:7e7bd4f937c7 | 354 | { |
TataLora | 0:7e7bd4f937c7 | 355 | t.stop(); |
TataLora | 0:7e7bd4f937c7 | 356 | resetPulse(dot); |
TataLora | 0:7e7bd4f937c7 | 357 | break; |
TataLora | 0:7e7bd4f937c7 | 358 | } |
TataLora | 0:7e7bd4f937c7 | 359 | } |
TataLora | 0:7e7bd4f937c7 | 360 | } |
TataLora | 0:7e7bd4f937c7 | 361 | if(initial) |
TataLora | 0:7e7bd4f937c7 | 362 | { |
TataLora | 0:7e7bd4f937c7 | 363 | if(t.read_ms() > 1200) |
TataLora | 0:7e7bd4f937c7 | 364 | { |
TataLora | 0:7e7bd4f937c7 | 365 | logInfo("Geen pulsen gedetecteerd, slapen"); |
TataLora | 0:7e7bd4f937c7 | 366 | dot->sleep(0, mDot::INTERRUPT, true); |
TataLora | 0:7e7bd4f937c7 | 367 | } |
TataLora | 0:7e7bd4f937c7 | 368 | } |
TataLora | 0:7e7bd4f937c7 | 369 | |
TataLora | 0:7e7bd4f937c7 | 370 | if(diff >= 1) |
TataLora | 0:7e7bd4f937c7 | 371 | { |
TataLora | 0:7e7bd4f937c7 | 372 | |
TataLora | 0:7e7bd4f937c7 | 373 | if(newValue) |
TataLora | 0:7e7bd4f937c7 | 374 | { |
TataLora | 0:7e7bd4f937c7 | 375 | initial = 0; |
TataLora | 0:7e7bd4f937c7 | 376 | t.stop(); |
TataLora | 0:7e7bd4f937c7 | 377 | t.reset(); |
TataLora | 0:7e7bd4f937c7 | 378 | t.start(); |
TataLora | 0:7e7bd4f937c7 | 379 | isPulseActive = 1; |
TataLora | 0:7e7bd4f937c7 | 380 | //logInfo("Start "); |
TataLora | 0:7e7bd4f937c7 | 381 | //dot->sleep(0, mDot::INTERRUPT, false); |
TataLora | 0:7e7bd4f937c7 | 382 | } |
TataLora | 0:7e7bd4f937c7 | 383 | else if(isPulseActive) |
TataLora | 0:7e7bd4f937c7 | 384 | { |
TataLora | 0:7e7bd4f937c7 | 385 | t.stop(); |
TataLora | 0:7e7bd4f937c7 | 386 | logInfo("Positieve puls duur %f seconds", t.read()); |
TataLora | 0:7e7bd4f937c7 | 387 | |
TataLora | 0:7e7bd4f937c7 | 388 | int timeInMs = t.read_ms(); |
TataLora | 0:7e7bd4f937c7 | 389 | |
TataLora | 0:7e7bd4f937c7 | 390 | if(timeInMs > 990 && timeInMs < 1020) |
TataLora | 0:7e7bd4f937c7 | 391 | { |
TataLora | 0:7e7bd4f937c7 | 392 | activity = STARTING; |
TataLora | 0:7e7bd4f937c7 | 393 | } |
TataLora | 0:7e7bd4f937c7 | 394 | else if(timeInMs > 220 && timeInMs < 260) |
TataLora | 0:7e7bd4f937c7 | 395 | { |
TataLora | 0:7e7bd4f937c7 | 396 | activity = TURNING; |
TataLora | 0:7e7bd4f937c7 | 397 | } |
TataLora | 0:7e7bd4f937c7 | 398 | else if(timeInMs > 88 && timeInMs < 92) |
TataLora | 0:7e7bd4f937c7 | 399 | { |
TataLora | 0:7e7bd4f937c7 | 400 | activity = ALIVE; |
TataLora | 0:7e7bd4f937c7 | 401 | dot->sleep(20, mDot::INTERRUPT, true); |
TataLora | 0:7e7bd4f937c7 | 402 | } |
TataLora | 0:7e7bd4f937c7 | 403 | else if(timeInMs > 32 && timeInMs < 40) |
TataLora | 0:7e7bd4f937c7 | 404 | { |
TataLora | 0:7e7bd4f937c7 | 405 | activity = ERR; |
TataLora | 0:7e7bd4f937c7 | 406 | } |
TataLora | 0:7e7bd4f937c7 | 407 | else if(timeInMs > 20) |
TataLora | 0:7e7bd4f937c7 | 408 | { |
TataLora | 0:7e7bd4f937c7 | 409 | activity = NONE; |
TataLora | 0:7e7bd4f937c7 | 410 | } |
TataLora | 0:7e7bd4f937c7 | 411 | |
TataLora | 0:7e7bd4f937c7 | 412 | pulseCount++; |
TataLora | 0:7e7bd4f937c7 | 413 | t.reset(); |
TataLora | 0:7e7bd4f937c7 | 414 | t.start(); |
TataLora | 0:7e7bd4f937c7 | 415 | //dot->sleep(2, mDot::RTC_ALARM, false); |
TataLora | 0:7e7bd4f937c7 | 416 | } |
TataLora | 0:7e7bd4f937c7 | 417 | //printf("analog value: %d\r\n", newValue); |
TataLora | 0:7e7bd4f937c7 | 418 | } |
TataLora | 0:7e7bd4f937c7 | 419 | oldValue = newValue; |
TataLora | 0:7e7bd4f937c7 | 420 | |
TataLora | 0:7e7bd4f937c7 | 421 | if(!isWaiting) |
TataLora | 0:7e7bd4f937c7 | 422 | { |
TataLora | 0:7e7bd4f937c7 | 423 | //printf("Slapen\r\n"); |
TataLora | 0:7e7bd4f937c7 | 424 | //dot->sleep(4, mDot::RTC_ALARM); |
TataLora | 0:7e7bd4f937c7 | 425 | } |
TataLora | 0:7e7bd4f937c7 | 426 | |
TataLora | 0:7e7bd4f937c7 | 427 | } |
TataLora | 0:7e7bd4f937c7 | 428 | |
TataLora | 0:7e7bd4f937c7 | 429 | logInfo("Nu nergens meer op reageren....."); |
TataLora | 0:7e7bd4f937c7 | 430 | |
TataLora | 0:7e7bd4f937c7 | 431 | dot->sleep(20, mDot::INTERRUPT, true); |
TataLora | 0:7e7bd4f937c7 | 432 | */ |
TataLora | 0:7e7bd4f937c7 | 433 | return 0; |
TataLora | 0:7e7bd4f937c7 | 434 | } |