UDK + Shield
Dependencies: DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot_1012-hotifx mbed-rtos mbed-src Senet_Packet X_NUCLEO_IKS01A1
Fork of MTDOT-EVBDemo_Senet by
main.cpp@10:658720d51610, 2016-08-23 (annotated)
- Committer:
- Shaun Nelson
- Date:
- Tue Aug 23 18:03:01 2016 -0400
- Revision:
- 10:658720d51610
- Parent:
- 7:3ccfc47b8e0b
- Child:
- 12:ad92bbb6312f
Initial commit of mdot UDK + MEMS shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
falingtrea | 0:bdd16076aaa5 | 1 | /** |
falingtrea | 0:bdd16076aaa5 | 2 | * @file main.cpp |
falingtrea | 0:bdd16076aaa5 | 3 | * @brief Main application for mDot-EVB demo |
falingtrea | 0:bdd16076aaa5 | 4 | * @author Tim Barr MultiTech Systems Inc. |
falingtrea | 3:68e974f5f532 | 5 | * @version 1.03 |
falingtrea | 0:bdd16076aaa5 | 6 | * @see |
falingtrea | 0:bdd16076aaa5 | 7 | * |
falingtrea | 0:bdd16076aaa5 | 8 | * Copyright (c) 2015 |
falingtrea | 0:bdd16076aaa5 | 9 | * |
falingtrea | 0:bdd16076aaa5 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
falingtrea | 0:bdd16076aaa5 | 11 | * you may not use this file except in compliance with the License. |
falingtrea | 0:bdd16076aaa5 | 12 | * You may obtain a copy of the License at |
falingtrea | 0:bdd16076aaa5 | 13 | * |
falingtrea | 0:bdd16076aaa5 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
falingtrea | 0:bdd16076aaa5 | 15 | * |
falingtrea | 0:bdd16076aaa5 | 16 | * Unless required by applicable law or agreed to in writing, software |
falingtrea | 0:bdd16076aaa5 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
falingtrea | 0:bdd16076aaa5 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
falingtrea | 0:bdd16076aaa5 | 19 | * See the License for the specific language governing permissions and |
falingtrea | 0:bdd16076aaa5 | 20 | * limitations under the License. |
falingtrea | 1:ac9595d0f0e7 | 21 | * |
falingtrea | 1:ac9595d0f0e7 | 22 | * 1.01 TAB 7/6/15 Removed NULL pointer from evbAmbientLight creation call. |
falingtrea | 2:75adc72aa6a0 | 23 | * |
falingtrea | 3:68e974f5f532 | 24 | * 1.02 TAB 7/8/15 Send logo to LCD before attempting connection to LoRa network. Added |
falingtrea | 2:75adc72aa6a0 | 25 | * information on setting up for public LoRa network. Moved SW setup to |
falingtrea | 2:75adc72aa6a0 | 26 | * beginning of main. Removed printf call from ISR functions. Added |
falingtrea | 2:75adc72aa6a0 | 27 | * additional checks for exit_program. |
falingtrea | 3:68e974f5f532 | 28 | * |
falingtrea | 3:68e974f5f532 | 29 | * 1.03 TAB 7/15/15 Added threads for push button switch debounce. |
falingtrea | 3:68e974f5f532 | 30 | * |
falingtrea | 0:bdd16076aaa5 | 31 | */ |
falingtrea | 0:bdd16076aaa5 | 32 | |
Shaun Nelson |
10:658720d51610 | 33 | // #define MTDOT_EVB |
Shaun Nelson |
10:658720d51610 | 34 | |
Shaun Nelson |
10:658720d51610 | 35 | //#define MTDOT_UDK |
Shaun Nelson |
10:658720d51610 | 36 | |
falingtrea | 0:bdd16076aaa5 | 37 | #include "mbed.h" |
Shaun Nelson |
10:658720d51610 | 38 | #include "senet_packet.h" |
Shaun Nelson |
10:658720d51610 | 39 | |
Shaun Nelson |
10:658720d51610 | 40 | // EVB Sensors |
Shaun Nelson |
10:658720d51610 | 41 | #ifdef MTDOT_EVB |
falingtrea | 0:bdd16076aaa5 | 42 | #include "MMA845x.h" |
falingtrea | 0:bdd16076aaa5 | 43 | #include "MPL3115A2.h" |
falingtrea | 0:bdd16076aaa5 | 44 | #include "ISL29011.h" |
falingtrea | 0:bdd16076aaa5 | 45 | #include "NCP5623B.h" |
falingtrea | 0:bdd16076aaa5 | 46 | #include "DOGS102.h" |
falingtrea | 0:bdd16076aaa5 | 47 | #include "font_6x8.h" |
falingtrea | 0:bdd16076aaa5 | 48 | #include "MultiTech_Logo.h" |
Shaun Nelson |
10:658720d51610 | 49 | #elif defined(MTDOT_UDK) |
Shaun Nelson |
10:658720d51610 | 50 | #include "x_nucleo_iks01a1.h" |
Shaun Nelson |
10:658720d51610 | 51 | #endif |
Shaun Nelson |
10:658720d51610 | 52 | |
falingtrea | 0:bdd16076aaa5 | 53 | #include "mDot.h" |
falingtrea | 0:bdd16076aaa5 | 54 | #include "rtos.h" |
falingtrea | 0:bdd16076aaa5 | 55 | #include <string> |
falingtrea | 0:bdd16076aaa5 | 56 | #include <vector> |
falingtrea | 0:bdd16076aaa5 | 57 | |
falingtrea | 0:bdd16076aaa5 | 58 | enum LED1_COLOR { |
falingtrea | 0:bdd16076aaa5 | 59 | RED = 0, |
falingtrea | 0:bdd16076aaa5 | 60 | GREEN = 1 |
falingtrea | 0:bdd16076aaa5 | 61 | }; |
falingtrea | 0:bdd16076aaa5 | 62 | |
falingtrea | 0:bdd16076aaa5 | 63 | /* |
falingtrea | 0:bdd16076aaa5 | 64 | * union for converting from 32-bit to 4 8-bit values |
falingtrea | 0:bdd16076aaa5 | 65 | */ |
falingtrea | 0:bdd16076aaa5 | 66 | union convert32 { |
falingtrea | 0:bdd16076aaa5 | 67 | int32_t f_s; // convert from signed 32 bit int |
falingtrea | 0:bdd16076aaa5 | 68 | uint32_t f_u; // convert from unsigned 32 bit int |
falingtrea | 0:bdd16076aaa5 | 69 | uint8_t t_u[4]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 70 | }; |
falingtrea | 0:bdd16076aaa5 | 71 | |
falingtrea | 0:bdd16076aaa5 | 72 | /* |
falingtrea | 0:bdd16076aaa5 | 73 | * union for converting from 16- bit to 2 8-bit values |
falingtrea | 0:bdd16076aaa5 | 74 | */ |
falingtrea | 0:bdd16076aaa5 | 75 | union convert16 { |
falingtrea | 0:bdd16076aaa5 | 76 | int16_t f_s; // convert from signed 16 bit int |
falingtrea | 0:bdd16076aaa5 | 77 | uint16_t f_u; // convert from unsigned 16 bit int |
falingtrea | 0:bdd16076aaa5 | 78 | uint8_t t_u[2]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 79 | }; |
falingtrea | 0:bdd16076aaa5 | 80 | |
Shaun Nelson |
10:658720d51610 | 81 | struct BoardSensorData |
Shaun Nelson |
10:658720d51610 | 82 | { |
Shaun Nelson |
10:658720d51610 | 83 | float temperature; |
Shaun Nelson |
10:658720d51610 | 84 | float pressure; |
Shaun Nelson |
10:658720d51610 | 85 | int32_t accel_x; |
Shaun Nelson |
10:658720d51610 | 86 | int32_t accel_y; |
Shaun Nelson |
10:658720d51610 | 87 | int32_t accel_z; |
Shaun Nelson |
10:658720d51610 | 88 | |
Shaun Nelson |
10:658720d51610 | 89 | inline void init() |
Shaun Nelson |
10:658720d51610 | 90 | { |
Shaun Nelson |
10:658720d51610 | 91 | temperature= 0; |
Shaun Nelson |
10:658720d51610 | 92 | pressure = 0; |
Shaun Nelson |
10:658720d51610 | 93 | accel_x = 0; |
Shaun Nelson |
10:658720d51610 | 94 | accel_y = 0; |
Shaun Nelson |
10:658720d51610 | 95 | accel_z = 0; |
Shaun Nelson |
10:658720d51610 | 96 | } |
Shaun Nelson |
10:658720d51610 | 97 | |
Shaun Nelson |
10:658720d51610 | 98 | BoardSensorData() { init(); } |
Shaun Nelson |
10:658720d51610 | 99 | }; |
Shaun Nelson |
10:658720d51610 | 100 | |
falingtrea | 0:bdd16076aaa5 | 101 | //DigitalIn mDot02(PA_2); // GPIO/UART_TX |
falingtrea | 0:bdd16076aaa5 | 102 | //DigitalOut mDot03(PA_3); // GPIO/UART_RX |
falingtrea | 0:bdd16076aaa5 | 103 | //DigitalIn mDot04(PA_6); // GPIO/SPI_MISO |
falingtrea | 0:bdd16076aaa5 | 104 | //DigitalIn mDot06(PA_8); // GPIO/I2C_SCL |
falingtrea | 0:bdd16076aaa5 | 105 | //DigitalIn mDot07(PC_9); // GPIO/I2C_SDA |
falingtrea | 0:bdd16076aaa5 | 106 | |
Shaun Nelson |
10:658720d51610 | 107 | #ifdef MDOT_EVB |
falingtrea | 0:bdd16076aaa5 | 108 | InterruptIn mDot08(PA_12); // GPIO/USB PB S1 on EVB |
falingtrea | 0:bdd16076aaa5 | 109 | InterruptIn mDot09(PA_11); // GPIO/USB PB S2 on EVB |
falingtrea | 0:bdd16076aaa5 | 110 | |
falingtrea | 0:bdd16076aaa5 | 111 | //DigitalIn mDot11(PA_7); // GPIO/SPI_MOSI |
falingtrea | 0:bdd16076aaa5 | 112 | |
falingtrea | 0:bdd16076aaa5 | 113 | InterruptIn mDot12(PA_0); // GPIO/UART_CTS PRESSURE_INT2 on EVB |
falingtrea | 0:bdd16076aaa5 | 114 | DigitalOut mDot13(PC_13,1); // GPIO LCD_C/D |
falingtrea | 0:bdd16076aaa5 | 115 | InterruptIn mDot15(PC_1); // GPIO LIGHT_PROX_INT on EVB |
falingtrea | 0:bdd16076aaa5 | 116 | InterruptIn mDot16(PA_1); // GPIO/UART_RTS ACCEL_INT2 on EVB |
falingtrea | 0:bdd16076aaa5 | 117 | DigitalOut mDot17(PA_4,1); // GPIO/SPI_NCS LCD_CS on EVB |
falingtrea | 0:bdd16076aaa5 | 118 | |
falingtrea | 0:bdd16076aaa5 | 119 | //DigitalIn mDot18(PA_5); // GPIO/SPI_SCK |
falingtrea | 0:bdd16076aaa5 | 120 | |
falingtrea | 0:bdd16076aaa5 | 121 | //DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO PushPull LED Low=Red High=Green set MODE=INPUT to turn off |
falingtrea | 0:bdd16076aaa5 | 122 | AnalogIn mDot20(PB_1); // GPIO Current Sense Analog in on EVB |
falingtrea | 0:bdd16076aaa5 | 123 | |
falingtrea | 0:bdd16076aaa5 | 124 | Serial debugUART(PA_9, PA_10); // mDot debug UART |
falingtrea | 0:bdd16076aaa5 | 125 | |
falingtrea | 0:bdd16076aaa5 | 126 | //Serial mDotUART(PA_2, PA_3); // mDot external UART mDot02 and mDot03 |
falingtrea | 0:bdd16076aaa5 | 127 | |
falingtrea | 0:bdd16076aaa5 | 128 | I2C mDoti2c(PC_9,PA_8); // mDot External I2C mDot6 and mDot7 |
falingtrea | 0:bdd16076aaa5 | 129 | |
falingtrea | 0:bdd16076aaa5 | 130 | SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18 |
Shaun Nelson |
10:658720d51610 | 131 | #elif defined(MTDOT_UDK) |
Shaun Nelson |
10:658720d51610 | 132 | |
Shaun Nelson |
10:658720d51610 | 133 | Serial debugUART(USBTX, USBRX); // mDot debug UART |
Shaun Nelson |
10:658720d51610 | 134 | |
Shaun Nelson |
10:658720d51610 | 135 | #endif |
falingtrea | 0:bdd16076aaa5 | 136 | |
falingtrea | 2:75adc72aa6a0 | 137 | /* **** replace these values with the proper public or private network settings **** |
falingtrea | 2:75adc72aa6a0 | 138 | * config_network_nameand config_network_pass are for private networks. |
falingtrea | 2:75adc72aa6a0 | 139 | */ |
falingtrea | 0:bdd16076aaa5 | 140 | static std::string config_network_name = "TAB-CubeNet"; |
falingtrea | 0:bdd16076aaa5 | 141 | static std::string config_network_pass = "1nt3gral"; |
dkjendal | 6:b22dfacacae1 | 142 | static uint8_t config_frequency_sub_band = 0; |
dkjendal | 6:b22dfacacae1 | 143 | static bool config_adr_on = true; |
falingtrea | 0:bdd16076aaa5 | 144 | |
dkjendal | 4:9ea6ae34157c | 145 | /* config_app_id and config_app_key are for public networks. */ |
dkjendal | 4:9ea6ae34157c | 146 | static uint8_t app_id[8] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01}; |
dkjendal | 4:9ea6ae34157c | 147 | std::vector<uint8_t> config_app_id(app_id,app_id+sizeof(app_id)/sizeof(uint8_t)); |
dkjendal | 7:3ccfc47b8e0b | 148 | static uint8_t app_key[16] = {0xB4,0xAD,0x1A,0x25,0x69,0x7F,0xF6,0x8E,0xD3,0x4B,0x83,0xC4,0xB6,0xC0,0xF2,0x3C}; // 9C49 |
dkjendal | 7:3ccfc47b8e0b | 149 | //{0xA0,0xC8,0x3D,0xD1,0x7B,0x32,0x97,0x1B,0x71,0xDC,0x53,0xB5,0x4D,0x89,0xC6,0x19}; |
dkjendal | 7:3ccfc47b8e0b | 150 | //{0xFC,0xD1,0x8B,0x22,0x9E,0xA0,0xDA,0x4F,0x6B,0x8F,0x39,0x26,0xAF,0x07,0x8A,0x05}; //9CFF |
dkjendal | 6:b22dfacacae1 | 151 | //{0xB3,0x53,0x00,0xDA,0xF6,0x0A,0x6C,0xF9,0x54,0x57,0x79,0x50,0x6F,0x19,0xE1,0x3A}; //9C35 |
dkjendal | 4:9ea6ae34157c | 152 | std::vector<uint8_t> config_app_key(app_key,app_key+sizeof(app_key)/sizeof(uint8_t)); |
falingtrea | 2:75adc72aa6a0 | 153 | |
dkjendal | 4:9ea6ae34157c | 154 | uint8_t result, pckt_time=100; |
falingtrea | 0:bdd16076aaa5 | 155 | char data; |
falingtrea | 0:bdd16076aaa5 | 156 | unsigned char test; |
falingtrea | 0:bdd16076aaa5 | 157 | char txtstr[17]; |
falingtrea | 0:bdd16076aaa5 | 158 | int32_t num_whole, mdot_ret; |
falingtrea | 0:bdd16076aaa5 | 159 | uint32_t pressure; |
falingtrea | 0:bdd16076aaa5 | 160 | int16_t num_frac; |
falingtrea | 0:bdd16076aaa5 | 161 | |
dkjendal | 4:9ea6ae34157c | 162 | uint8_t position_value = 0xFF; // 00 unknown, 01 is flat, 02 is vertical |
dkjendal | 4:9ea6ae34157c | 163 | uint8_t reflected_value = 0xFE; |
dkjendal | 4:9ea6ae34157c | 164 | bool position_changed = true; |
dkjendal | 4:9ea6ae34157c | 165 | |
falingtrea | 0:bdd16076aaa5 | 166 | bool exit_program = false; |
falingtrea | 0:bdd16076aaa5 | 167 | |
Shaun Nelson |
10:658720d51610 | 168 | #ifdef MTDOT_EVB |
falingtrea | 0:bdd16076aaa5 | 169 | MMA845x_DATA accel_data; |
falingtrea | 0:bdd16076aaa5 | 170 | MPL3115A2_DATA baro_data; |
falingtrea | 0:bdd16076aaa5 | 171 | uint16_t lux_data; |
Shaun Nelson |
10:658720d51610 | 172 | MMA845x* evbAccel = NULL; |
Shaun Nelson |
10:658720d51610 | 173 | MPL3115A2* evbBaro = NULL; |
Shaun Nelson |
10:658720d51610 | 174 | ISL29011* evbAmbLight = NULL; |
Shaun Nelson |
10:658720d51610 | 175 | NCP5623B* evbBackLight = NULL; |
Shaun Nelson |
10:658720d51610 | 176 | DOGS102* evbLCD = NULL; |
Shaun Nelson |
10:658720d51610 | 177 | |
Shaun Nelson |
10:658720d51610 | 178 | #elif defined(MTDOT_UDK) |
Shaun Nelson |
10:658720d51610 | 179 | static X_NUCLEO_IKS01A1 *mems_shield; |
Shaun Nelson |
10:658720d51610 | 180 | #endif |
Shaun Nelson |
10:658720d51610 | 181 | |
falingtrea | 0:bdd16076aaa5 | 182 | mDot* mdot_radio; |
falingtrea | 0:bdd16076aaa5 | 183 | |
falingtrea | 0:bdd16076aaa5 | 184 | convert32 convertl; |
falingtrea | 0:bdd16076aaa5 | 185 | convert16 converts; |
falingtrea | 0:bdd16076aaa5 | 186 | |
falingtrea | 3:68e974f5f532 | 187 | // flags for pushbutton debounce code |
falingtrea | 3:68e974f5f532 | 188 | bool pb1_low = false; |
falingtrea | 3:68e974f5f532 | 189 | bool pb2_low = false; |
falingtrea | 3:68e974f5f532 | 190 | |
falingtrea | 0:bdd16076aaa5 | 191 | void pb1ISR(void); |
falingtrea | 0:bdd16076aaa5 | 192 | void pb2ISR(void); |
falingtrea | 3:68e974f5f532 | 193 | void pb1_debounce(void const *args); |
falingtrea | 3:68e974f5f532 | 194 | void pb2_debounce(void const *args); |
falingtrea | 3:68e974f5f532 | 195 | Thread* thread_3; |
falingtrea | 0:bdd16076aaa5 | 196 | |
falingtrea | 0:bdd16076aaa5 | 197 | void log_error(mDot* dot, const char* msg, int32_t retval); |
falingtrea | 0:bdd16076aaa5 | 198 | |
falingtrea | 0:bdd16076aaa5 | 199 | void config_pkt_xmit (void const *args); |
falingtrea | 0:bdd16076aaa5 | 200 | |
Shaun Nelson |
10:658720d51610 | 201 | void SendFrame(std::vector<uint8_t> frame) |
falingtrea | 0:bdd16076aaa5 | 202 | { |
Shaun Nelson |
10:658720d51610 | 203 | if ((mdot_ret = mdot_radio->send(frame)) != mDot::MDOT_OK) { |
Shaun Nelson |
10:658720d51610 | 204 | log_error(mdot_radio, "failed to send", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 205 | } |
Shaun Nelson |
10:658720d51610 | 206 | else { |
Shaun Nelson |
10:658720d51610 | 207 | printf("successfully sent data to gateway\r\n"); |
Shaun Nelson |
10:658720d51610 | 208 | frame.clear(); |
Shaun Nelson |
10:658720d51610 | 209 | if ((mdot_ret = mdot_radio->recv(frame)) != mDot::MDOT_OK) { |
Shaun Nelson |
10:658720d51610 | 210 | log_error(mdot_radio,"failed to recv:", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 211 | } else { |
Shaun Nelson |
10:658720d51610 | 212 | printf("recv data: "); |
Shaun Nelson |
10:658720d51610 | 213 | for(uint32_t i = 0;i < frame.size();i++) |
Shaun Nelson |
10:658720d51610 | 214 | printf("%02X",frame[i]); |
Shaun Nelson |
10:658720d51610 | 215 | printf("\r\n"); |
Shaun Nelson |
10:658720d51610 | 216 | reflected_value = frame[0]; |
Shaun Nelson |
10:658720d51610 | 217 | if(reflected_value == position_value) |
Shaun Nelson |
10:658720d51610 | 218 | { |
Shaun Nelson |
10:658720d51610 | 219 | #if MTDOT_EVB |
Shaun Nelson |
10:658720d51610 | 220 | evbBackLight->setLEDCurrent(16); |
Shaun Nelson |
10:658720d51610 | 221 | #endif |
Shaun Nelson |
10:658720d51610 | 222 | position_changed = false; |
Shaun Nelson |
10:658720d51610 | 223 | } |
Shaun Nelson |
10:658720d51610 | 224 | #if MTDOT_EVB |
Shaun Nelson |
10:658720d51610 | 225 | else |
Shaun Nelson |
10:658720d51610 | 226 | { |
Shaun Nelson |
10:658720d51610 | 227 | evbBackLight->setLEDCurrent(0); |
Shaun Nelson |
10:658720d51610 | 228 | } |
Shaun Nelson |
10:658720d51610 | 229 | #endif |
Shaun Nelson |
10:658720d51610 | 230 | } |
Shaun Nelson |
10:658720d51610 | 231 | } |
Shaun Nelson |
10:658720d51610 | 232 | } |
falingtrea | 0:bdd16076aaa5 | 233 | |
Shaun Nelson |
10:658720d51610 | 234 | #ifdef MDOT_EVB |
Shaun Nelson |
10:658720d51610 | 235 | void BoardInit() |
Shaun Nelson |
10:658720d51610 | 236 | { |
Shaun Nelson |
10:658720d51610 | 237 | static Thread thread_1(pb1_debounce); // threads for de-bouncing pushbutton switches |
Shaun Nelson |
10:658720d51610 | 238 | static Thread thread_2(pb2_debounce); |
falingtrea | 0:bdd16076aaa5 | 239 | |
dkjendal | 4:9ea6ae34157c | 240 | debugUART.baud(115200); |
falingtrea | 0:bdd16076aaa5 | 241 | // mDotUART.baud(9600); // mdot UART unused but available on external connector |
falingtrea | 0:bdd16076aaa5 | 242 | |
falingtrea | 3:68e974f5f532 | 243 | thread_3 = new Thread(config_pkt_xmit); // start thread that sends LoRa packet when SW2 pressed |
falingtrea | 2:75adc72aa6a0 | 244 | |
falingtrea | 1:ac9595d0f0e7 | 245 | evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer |
falingtrea | 0:bdd16076aaa5 | 246 | evbBaro = new MPL3115A2(mDoti2c); // setup Barometric sensor |
falingtrea | 1:ac9595d0f0e7 | 247 | evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor |
falingtrea | 0:bdd16076aaa5 | 248 | evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip |
falingtrea | 1:ac9595d0f0e7 | 249 | evbLCD = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD |
falingtrea | 0:bdd16076aaa5 | 250 | |
falingtrea | 2:75adc72aa6a0 | 251 | /* |
falingtrea | 2:75adc72aa6a0 | 252 | * Setup SW1 as program stop function |
falingtrea | 2:75adc72aa6a0 | 253 | */ |
falingtrea | 2:75adc72aa6a0 | 254 | mDot08.disable_irq(); |
falingtrea | 2:75adc72aa6a0 | 255 | mDot08.fall(&pb1ISR); |
falingtrea | 2:75adc72aa6a0 | 256 | |
falingtrea | 2:75adc72aa6a0 | 257 | /* |
falingtrea | 2:75adc72aa6a0 | 258 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 2:75adc72aa6a0 | 259 | * mode to PullNone |
falingtrea | 2:75adc72aa6a0 | 260 | */ |
falingtrea | 2:75adc72aa6a0 | 261 | mDot08.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 262 | |
falingtrea | 2:75adc72aa6a0 | 263 | mDot08.enable_irq(); |
falingtrea | 2:75adc72aa6a0 | 264 | |
falingtrea | 2:75adc72aa6a0 | 265 | /* |
falingtrea | 2:75adc72aa6a0 | 266 | * Setup SW2 as packet time change |
falingtrea | 2:75adc72aa6a0 | 267 | */ |
falingtrea | 2:75adc72aa6a0 | 268 | mDot09.disable_irq(); |
falingtrea | 2:75adc72aa6a0 | 269 | mDot09.fall(&pb2ISR); |
falingtrea | 2:75adc72aa6a0 | 270 | |
falingtrea | 2:75adc72aa6a0 | 271 | /* |
falingtrea | 2:75adc72aa6a0 | 272 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 2:75adc72aa6a0 | 273 | * mode to PullNone |
falingtrea | 2:75adc72aa6a0 | 274 | */ |
falingtrea | 2:75adc72aa6a0 | 275 | mDot09.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 276 | |
falingtrea | 2:75adc72aa6a0 | 277 | mDot09.enable_irq(); |
falingtrea | 2:75adc72aa6a0 | 278 | |
falingtrea | 2:75adc72aa6a0 | 279 | /* |
falingtrea | 2:75adc72aa6a0 | 280 | * Setting other InterruptIn pins with Pull Ups |
falingtrea | 2:75adc72aa6a0 | 281 | */ |
falingtrea | 2:75adc72aa6a0 | 282 | mDot12.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 283 | mDot15.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 284 | mDot16.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 285 | |
falingtrea | 2:75adc72aa6a0 | 286 | printf("font table address %p\n\r",&font_6x8); |
falingtrea | 2:75adc72aa6a0 | 287 | printf("bitmap address %p\n\r",&MultiTech_Logo); |
falingtrea | 2:75adc72aa6a0 | 288 | |
falingtrea | 2:75adc72aa6a0 | 289 | // Setup and display logo on LCD |
falingtrea | 2:75adc72aa6a0 | 290 | evbLCD->startUpdate(); |
falingtrea | 2:75adc72aa6a0 | 291 | |
falingtrea | 2:75adc72aa6a0 | 292 | evbLCD->writeBitmap(0,0,MultiTech_Logo); |
falingtrea | 2:75adc72aa6a0 | 293 | |
falingtrea | 2:75adc72aa6a0 | 294 | sprintf(txtstr,"MTDOT"); |
falingtrea | 2:75adc72aa6a0 | 295 | evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 296 | sprintf(txtstr,"Evaluation"); |
falingtrea | 2:75adc72aa6a0 | 297 | evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 298 | sprintf(txtstr,"Board"); |
falingtrea | 2:75adc72aa6a0 | 299 | evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 300 | |
falingtrea | 2:75adc72aa6a0 | 301 | evbLCD->endUpdate(); |
falingtrea | 2:75adc72aa6a0 | 302 | |
Shaun Nelson |
10:658720d51610 | 303 | pckt_time = 10; |
Shaun Nelson |
10:658720d51610 | 304 | } |
falingtrea | 2:75adc72aa6a0 | 305 | |
Shaun Nelson |
10:658720d51610 | 306 | void PostJoinInit() |
Shaun Nelson |
10:658720d51610 | 307 | { |
falingtrea | 0:bdd16076aaa5 | 308 | osDelay(200); |
falingtrea | 0:bdd16076aaa5 | 309 | evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM |
falingtrea | 0:bdd16076aaa5 | 310 | |
falingtrea | 2:75adc72aa6a0 | 311 | // sets LED2 to 50% max current |
falingtrea | 0:bdd16076aaa5 | 312 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 313 | |
falingtrea | 0:bdd16076aaa5 | 314 | printf("Start of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 315 | |
falingtrea | 2:75adc72aa6a0 | 316 | osDelay (500); // allows other threads to process |
falingtrea | 2:75adc72aa6a0 | 317 | printf("shutdown LED:\n\r"); |
falingtrea | 2:75adc72aa6a0 | 318 | evbBackLight->shutdown(); |
falingtrea | 0:bdd16076aaa5 | 319 | |
falingtrea | 0:bdd16076aaa5 | 320 | osDelay (500); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 321 | printf("Turn on LED2\n\r"); |
falingtrea | 0:bdd16076aaa5 | 322 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 323 | |
falingtrea | 0:bdd16076aaa5 | 324 | data = evbAccel->getWhoAmI(); |
falingtrea | 0:bdd16076aaa5 | 325 | printf("Accelerometer who_am_i value = %x \n\r", data); |
falingtrea | 0:bdd16076aaa5 | 326 | |
falingtrea | 0:bdd16076aaa5 | 327 | result = evbAccel->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 328 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 329 | |
falingtrea | 0:bdd16076aaa5 | 330 | printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE"); |
falingtrea | 0:bdd16076aaa5 | 331 | |
falingtrea | 0:bdd16076aaa5 | 332 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 333 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 334 | |
falingtrea | 0:bdd16076aaa5 | 335 | /* |
falingtrea | 0:bdd16076aaa5 | 336 | * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz |
falingtrea | 0:bdd16076aaa5 | 337 | * normal oversample mode, High pass filter off |
falingtrea | 0:bdd16076aaa5 | 338 | */ |
falingtrea | 0:bdd16076aaa5 | 339 | evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, |
falingtrea | 0:bdd16076aaa5 | 340 | MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); |
falingtrea | 0:bdd16076aaa5 | 341 | |
falingtrea | 0:bdd16076aaa5 | 342 | /* |
falingtrea | 0:bdd16076aaa5 | 343 | * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. |
falingtrea | 0:bdd16076aaa5 | 344 | * and a sample taken every second when in active mode |
falingtrea | 0:bdd16076aaa5 | 345 | */ |
falingtrea | 0:bdd16076aaa5 | 346 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 347 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 348 | /* |
falingtrea | 0:bdd16076aaa5 | 349 | * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, |
falingtrea | 0:bdd16076aaa5 | 350 | * and 16000 lux range |
falingtrea | 0:bdd16076aaa5 | 351 | */ |
falingtrea | 0:bdd16076aaa5 | 352 | |
falingtrea | 0:bdd16076aaa5 | 353 | evbAmbLight->setMode(ISL29011::ALS_CONT); |
falingtrea | 0:bdd16076aaa5 | 354 | evbAmbLight->setResolution(ISL29011::ADC_16BIT); |
falingtrea | 0:bdd16076aaa5 | 355 | evbAmbLight->setRange(ISL29011::RNG_16000); |
falingtrea | 0:bdd16076aaa5 | 356 | |
falingtrea | 0:bdd16076aaa5 | 357 | /* |
falingtrea | 0:bdd16076aaa5 | 358 | * Set the accelerometer for active mode |
falingtrea | 0:bdd16076aaa5 | 359 | */ |
falingtrea | 0:bdd16076aaa5 | 360 | evbAccel->activeMode(); |
falingtrea | 0:bdd16076aaa5 | 361 | |
falingtrea | 0:bdd16076aaa5 | 362 | /* |
falingtrea | 0:bdd16076aaa5 | 363 | * Clear the min-max registers in the Barometric Sensor |
falingtrea | 0:bdd16076aaa5 | 364 | */ |
falingtrea | 0:bdd16076aaa5 | 365 | evbBaro->clearMinMaxRegs(); |
falingtrea | 0:bdd16076aaa5 | 366 | |
falingtrea | 0:bdd16076aaa5 | 367 | evbBackLight->setLEDCurrent(0); |
falingtrea | 0:bdd16076aaa5 | 368 | |
falingtrea | 0:bdd16076aaa5 | 369 | /* |
falingtrea | 2:75adc72aa6a0 | 370 | * Check for PB1 press during network join attempt |
falingtrea | 2:75adc72aa6a0 | 371 | */ |
falingtrea | 2:75adc72aa6a0 | 372 | if (exit_program) { |
falingtrea | 2:75adc72aa6a0 | 373 | printf("Exiting program\n\r"); |
falingtrea | 2:75adc72aa6a0 | 374 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 375 | sprintf(txtstr,"Exiting Program"); |
falingtrea | 2:75adc72aa6a0 | 376 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 377 | exit(1); |
falingtrea | 2:75adc72aa6a0 | 378 | } |
Shaun Nelson |
10:658720d51610 | 379 | } |
Shaun Nelson |
10:658720d51610 | 380 | |
Shaun Nelson |
10:658720d51610 | 381 | void ReadSensors(BoardSensorData &sensorData) |
Shaun Nelson |
10:658720d51610 | 382 | { |
Shaun Nelson |
10:658720d51610 | 383 | MMA845x_DATA accel_data; |
Shaun Nelson |
10:658720d51610 | 384 | /* |
Shaun Nelson |
10:658720d51610 | 385 | * Test Accelerometer XYZ data ready bit to see if acquisition complete |
Shaun Nelson |
10:658720d51610 | 386 | */ |
Shaun Nelson |
10:658720d51610 | 387 | do { |
Shaun Nelson |
10:658720d51610 | 388 | osDelay(100); // allows other threads to process |
Shaun Nelson |
10:658720d51610 | 389 | result = evbAccel->getStatus(); |
Shaun Nelson |
10:658720d51610 | 390 | } while ((result & MMA845x::XYZDR) == 0 ); |
Shaun Nelson |
10:658720d51610 | 391 | |
Shaun Nelson |
10:658720d51610 | 392 | /* |
Shaun Nelson |
10:658720d51610 | 393 | * Retrieve and print out accelerometer data |
Shaun Nelson |
10:658720d51610 | 394 | */ |
Shaun Nelson |
10:658720d51610 | 395 | accel_data = evbAccel->getXYZ(); |
Shaun Nelson |
10:658720d51610 | 396 | |
Shaun Nelson |
10:658720d51610 | 397 | sprintf(txtstr,"Accelerometer"); |
Shaun Nelson |
10:658720d51610 | 398 | evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 399 | sprintf(txtstr, "x = %d", accel_data._x); |
Shaun Nelson |
10:658720d51610 | 400 | evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 401 | sprintf(txtstr, "y = %d", accel_data._y); |
Shaun Nelson |
10:658720d51610 | 402 | evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 403 | sprintf(txtstr, "z = %d", accel_data._z ); |
Shaun Nelson |
10:658720d51610 | 404 | evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 405 | |
Shaun Nelson |
10:658720d51610 | 406 | sensorData->accel_x = accel_data._x; |
Shaun Nelson |
10:658720d51610 | 407 | sensorData->accel_y = accel_data._y; |
Shaun Nelson |
10:658720d51610 | 408 | sensorData->accel_z = accel_data._z; |
Shaun Nelson |
10:658720d51610 | 409 | |
Shaun Nelson |
10:658720d51610 | 410 | // Update accelerometer state |
Shaun Nelson |
10:658720d51610 | 411 | evbLCD->startUpdate(); |
Shaun Nelson |
10:658720d51610 | 412 | evbLCD->clearBuffer(); |
Shaun Nelson |
10:658720d51610 | 413 | |
Shaun Nelson |
10:658720d51610 | 414 | // convert to simple position value for use in send/recv |
Shaun Nelson |
10:658720d51610 | 415 | if((accel_data._x > 500)&&(accel_data._z < 500)) |
Shaun Nelson |
10:658720d51610 | 416 | { |
Shaun Nelson |
10:658720d51610 | 417 | if(position_value != 0x02) |
Shaun Nelson |
10:658720d51610 | 418 | position_changed = true; |
Shaun Nelson |
10:658720d51610 | 419 | position_value = 0x02; |
Shaun Nelson |
10:658720d51610 | 420 | } |
Shaun Nelson |
10:658720d51610 | 421 | else if((accel_data._x < 500)&&(accel_data._z > 500)) |
Shaun Nelson |
10:658720d51610 | 422 | { |
Shaun Nelson |
10:658720d51610 | 423 | if(position_value != 0x01) |
Shaun Nelson |
10:658720d51610 | 424 | position_changed = true; |
Shaun Nelson |
10:658720d51610 | 425 | position_value = 0x01; |
Shaun Nelson |
10:658720d51610 | 426 | } |
Shaun Nelson |
10:658720d51610 | 427 | else |
Shaun Nelson |
10:658720d51610 | 428 | { |
Shaun Nelson |
10:658720d51610 | 429 | if(position_value != 0x00) |
Shaun Nelson |
10:658720d51610 | 430 | position_changed = true; |
Shaun Nelson |
10:658720d51610 | 431 | position_value= 0x00; |
Shaun Nelson |
10:658720d51610 | 432 | } |
Shaun Nelson |
10:658720d51610 | 433 | |
Shaun Nelson |
10:658720d51610 | 434 | if(changed) |
Shaun Nelson |
10:658720d51610 | 435 | evbBackLight->setLEDCurrent(0); |
Shaun Nelson |
10:658720d51610 | 436 | /* |
Shaun Nelson |
10:658720d51610 | 437 | * Trigger a Pressure reading |
Shaun Nelson |
10:658720d51610 | 438 | */ |
Shaun Nelson |
10:658720d51610 | 439 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
Shaun Nelson |
10:658720d51610 | 440 | MPL3115A2::AT_1); |
Shaun Nelson |
10:658720d51610 | 441 | evbBaro->triggerOneShot(); |
Shaun Nelson |
10:658720d51610 | 442 | |
Shaun Nelson |
10:658720d51610 | 443 | /* |
Shaun Nelson |
10:658720d51610 | 444 | * Test barometer device status to see if acquisition is complete |
Shaun Nelson |
10:658720d51610 | 445 | */ |
Shaun Nelson |
10:658720d51610 | 446 | do { |
Shaun Nelson |
10:658720d51610 | 447 | osDelay(100); // allows other threads to process |
Shaun Nelson |
10:658720d51610 | 448 | result = evbBaro->getStatus(); |
Shaun Nelson |
10:658720d51610 | 449 | } while ((result & MPL3115A2::PTDR) == 0 ); |
Shaun Nelson |
10:658720d51610 | 450 | |
Shaun Nelson |
10:658720d51610 | 451 | /* |
Shaun Nelson |
10:658720d51610 | 452 | * Retrieve and print out barometric pressure |
Shaun Nelson |
10:658720d51610 | 453 | */ |
Shaun Nelson |
10:658720d51610 | 454 | pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value |
Shaun Nelson |
10:658720d51610 | 455 | num_whole = pressure >> 2; // 18 bit integer significant |
Shaun Nelson |
10:658720d51610 | 456 | num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit |
Shaun Nelson |
10:658720d51610 | 457 | sensorData.pressure = pressure + (.25 * num_frac); |
Shaun Nelson |
10:658720d51610 | 458 | |
Shaun Nelson |
10:658720d51610 | 459 | sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac); |
Shaun Nelson |
10:658720d51610 | 460 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 461 | |
Shaun Nelson |
10:658720d51610 | 462 | /* |
Shaun Nelson |
10:658720d51610 | 463 | * Trigger a Altitude reading |
Shaun Nelson |
10:658720d51610 | 464 | */ |
Shaun Nelson |
10:658720d51610 | 465 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, |
Shaun Nelson |
10:658720d51610 | 466 | MPL3115A2::AT_1); |
Shaun Nelson |
10:658720d51610 | 467 | evbBaro->triggerOneShot(); |
Shaun Nelson |
10:658720d51610 | 468 | |
Shaun Nelson |
10:658720d51610 | 469 | /* |
Shaun Nelson |
10:658720d51610 | 470 | * Test barometer device status to see if acquisition is complete |
Shaun Nelson |
10:658720d51610 | 471 | */ |
Shaun Nelson |
10:658720d51610 | 472 | do { |
Shaun Nelson |
10:658720d51610 | 473 | osDelay(100); // allows other threads to process |
Shaun Nelson |
10:658720d51610 | 474 | result = evbBaro->getStatus(); |
Shaun Nelson |
10:658720d51610 | 475 | } while ((result & MPL3115A2::PTDR) == 0 ); |
Shaun Nelson |
10:658720d51610 | 476 | |
Shaun Nelson |
10:658720d51610 | 477 | /* |
Shaun Nelson |
10:658720d51610 | 478 | * Retrieve and print out altitude and temperature |
Shaun Nelson |
10:658720d51610 | 479 | */ |
Shaun Nelson |
10:658720d51610 | 480 | baro_data = evbBaro->getAllData(false); |
Shaun Nelson |
10:658720d51610 | 481 | baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value |
Shaun Nelson |
10:658720d51610 | 482 | num_whole = baro_data._baro / 16; // 18 bit signed significant integer |
Shaun Nelson |
10:658720d51610 | 483 | num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit |
Shaun Nelson |
10:658720d51610 | 484 | sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac); |
Shaun Nelson |
10:658720d51610 | 485 | evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 486 | num_whole = baro_data._temp / 16; // 8 bit signed significant integer |
Shaun Nelson |
10:658720d51610 | 487 | num_frac = (baro_data._temp & 0x0F) * 625 / 100; // 4 bit fractional .0625 per bit |
Shaun Nelson |
10:658720d51610 | 488 | sensor.temperature = num_whole + ((float)num_frac / 100); |
Shaun Nelson |
10:658720d51610 | 489 | sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac); |
Shaun Nelson |
10:658720d51610 | 490 | evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 491 | |
Shaun Nelson |
10:658720d51610 | 492 | /* |
Shaun Nelson |
10:658720d51610 | 493 | * retrieve and print out Ambient Light level |
Shaun Nelson |
10:658720d51610 | 494 | */ |
Shaun Nelson |
10:658720d51610 | 495 | lux_data = evbAmbLight->getData(); |
Shaun Nelson |
10:658720d51610 | 496 | num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit |
Shaun Nelson |
10:658720d51610 | 497 | num_frac = lux_data * 24 % 100; |
Shaun Nelson |
10:658720d51610 | 498 | sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac ); |
Shaun Nelson |
10:658720d51610 | 499 | evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 500 | |
Shaun Nelson |
10:658720d51610 | 501 | evbLCD->endUpdate(); |
Shaun Nelson |
10:658720d51610 | 502 | printf("finished iteration %d\n\r",(++i)); |
Shaun Nelson |
10:658720d51610 | 503 | } |
Shaun Nelson |
10:658720d51610 | 504 | |
Shaun Nelson |
10:658720d51610 | 505 | uint32_t PrepareFrame(std::vector<uint8_t> frame, BoardSensorData &data) |
Shaun Nelson |
10:658720d51610 | 506 | { |
Shaun Nelson |
10:658720d51610 | 507 | if (i % pckt_time != 0) |
Shaun Nelson |
10:658720d51610 | 508 | return 0; |
Shaun Nelson |
10:658720d51610 | 509 | |
Shaun Nelson |
10:658720d51610 | 510 | frame.clear(); |
Shaun Nelson |
10:658720d51610 | 511 | // we will send a simple byte descriptor of the current position of the device: 01 is laying flat, 02 is vertically oriented |
Shaun Nelson |
10:658720d51610 | 512 | frame.push_back(0x00); |
Shaun Nelson |
10:658720d51610 | 513 | |
Shaun Nelson |
10:658720d51610 | 514 | frame.push_back(position_value); |
Shaun Nelson |
10:658720d51610 | 515 | /*frame.push_back(0x0E); // key for Current Acceleration 3-Axis Value |
Shaun Nelson |
10:658720d51610 | 516 | converts.f_s = accel_data._x *4; // shift data 2 bits while retaining sign |
Shaun Nelson |
10:658720d51610 | 517 | frame.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
Shaun Nelson |
10:658720d51610 | 518 | converts.f_s = accel_data._y * 4; // shift data 2 bits while retaining sign |
Shaun Nelson |
10:658720d51610 | 519 | frame.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
Shaun Nelson |
10:658720d51610 | 520 | converts.f_s = accel_data._z * 4; // shift data 2 bits while retaining sign |
Shaun Nelson |
10:658720d51610 | 521 | frame.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
Shaun Nelson |
10:658720d51610 | 522 | */ |
Shaun Nelson |
10:658720d51610 | 523 | /* |
Shaun Nelson |
10:658720d51610 | 524 | frame.push_back(0x08); // key for Current Pressure Value |
Shaun Nelson |
10:658720d51610 | 525 | convertl.f_u = pressure; // pressure data is 20 bits unsigned |
Shaun Nelson |
10:658720d51610 | 526 | frame.push_back(convertl.t_u[2]); |
Shaun Nelson |
10:658720d51610 | 527 | frame.push_back(convertl.t_u[1]); |
Shaun Nelson |
10:658720d51610 | 528 | frame.push_back(convertl.t_u[0]); |
Shaun Nelson |
10:658720d51610 | 529 | frame.push_back(0x05); // key for Current Ambient Light Value |
Shaun Nelson |
10:658720d51610 | 530 | converts.f_u = lux_data; // data is 16 bits unsigned |
Shaun Nelson |
10:658720d51610 | 531 | frame.push_back(converts.t_u[1]); |
Shaun Nelson |
10:658720d51610 | 532 | frame.push_back(converts.t_u[0]); |
Shaun Nelson |
10:658720d51610 | 533 | frame.push_back(0x0B); // key for Current Temperature Value |
Shaun Nelson |
10:658720d51610 | 534 | converts.f_s = baro_data._temp; // temperature is signed 12 bit |
Shaun Nelson |
10:658720d51610 | 535 | frame.push_back(converts.t_u[1]); |
Shaun Nelson |
10:658720d51610 | 536 | frame.push_back(converts.t_u[0]); |
Shaun Nelson |
10:658720d51610 | 537 | */ |
Shaun Nelson |
10:658720d51610 | 538 | |
Shaun Nelson |
10:658720d51610 | 539 | if((reflected_value != position_value)||(position_changed)||(0 == (i%100))) |
Shaun Nelson |
10:658720d51610 | 540 | { |
Shaun Nelson |
10:658720d51610 | 541 | } |
Shaun Nelson |
10:658720d51610 | 542 | } |
Shaun Nelson |
10:658720d51610 | 543 | |
Shaun Nelson |
10:658720d51610 | 544 | #elif defined(MTDOT_UDK) |
Shaun Nelson |
10:658720d51610 | 545 | |
Shaun Nelson |
10:658720d51610 | 546 | void BoardInit() |
Shaun Nelson |
10:658720d51610 | 547 | { |
Shaun Nelson |
10:658720d51610 | 548 | debugUART.baud(115200); |
Shaun Nelson |
10:658720d51610 | 549 | |
Shaun Nelson |
10:658720d51610 | 550 | // ST X-NUCLEO-IKS01A1 MEMS Shield |
Shaun Nelson |
10:658720d51610 | 551 | mems_shield = X_NUCLEO_IKS01A1::Instance(); |
Shaun Nelson |
10:658720d51610 | 552 | } |
Shaun Nelson |
10:658720d51610 | 553 | |
Shaun Nelson |
10:658720d51610 | 554 | void PostJoinInit() { } |
Shaun Nelson |
10:658720d51610 | 555 | |
Shaun Nelson |
10:658720d51610 | 556 | |
Shaun Nelson |
10:658720d51610 | 557 | void ReadSensors(BoardSensorData &data) |
Shaun Nelson |
10:658720d51610 | 558 | { |
Shaun Nelson |
10:658720d51610 | 559 | uint32_t ret = 0; |
Shaun Nelson |
10:658720d51610 | 560 | int32_t accel_data[3]; |
Shaun Nelson |
10:658720d51610 | 561 | |
Shaun Nelson |
10:658720d51610 | 562 | // Temperature |
Shaun Nelson |
10:658720d51610 | 563 | ret |= (!CALL_METH(mems_shield->pt_sensor, GetTemperature, &data.temperature, 0.0f) ? 0x0 : 0x1); |
Shaun Nelson |
10:658720d51610 | 564 | |
Shaun Nelson |
10:658720d51610 | 565 | // Pressure |
Shaun Nelson |
10:658720d51610 | 566 | ret |= (!CALL_METH(mems_shield->pt_sensor, GetPressure, &data.pressure, 0.0f) ? 0x0 : 0x1); |
Shaun Nelson |
10:658720d51610 | 567 | |
Shaun Nelson |
10:658720d51610 | 568 | // Accelerometer |
Shaun Nelson |
10:658720d51610 | 569 | MotionSensor *motionSensor = mems_shield->GetAccelerometer(); |
Shaun Nelson |
10:658720d51610 | 570 | if( motionSensor != NULL) |
Shaun Nelson |
10:658720d51610 | 571 | { |
Shaun Nelson |
10:658720d51610 | 572 | motionSensor->Get_X_Axes(accel_data); |
Shaun Nelson |
10:658720d51610 | 573 | |
Shaun Nelson |
10:658720d51610 | 574 | data.accel_x = accel_data[0]; |
Shaun Nelson |
10:658720d51610 | 575 | data.accel_y = accel_data[1]; |
Shaun Nelson |
10:658720d51610 | 576 | data.accel_z = accel_data[2]; |
Shaun Nelson |
10:658720d51610 | 577 | } |
Shaun Nelson |
10:658720d51610 | 578 | |
Shaun Nelson |
10:658720d51610 | 579 | printf("%s: Temperature=%f, Pressure=%f, x=%ld, y=%ld, z=%ld\r\n",__func__, |
Shaun Nelson |
10:658720d51610 | 580 | data.temperature, data.pressure, |
Shaun Nelson |
10:658720d51610 | 581 | data.accel_x, data.accel_y, data.accel_z); |
Shaun Nelson |
10:658720d51610 | 582 | |
Shaun Nelson |
10:658720d51610 | 583 | osDelay(5000); |
Shaun Nelson |
10:658720d51610 | 584 | |
Shaun Nelson |
10:658720d51610 | 585 | } |
Shaun Nelson |
10:658720d51610 | 586 | |
Shaun Nelson |
10:658720d51610 | 587 | uint32_t PrepareFrame(std::vector<uint8_t> frame, BoardSensorData &data) |
Shaun Nelson |
10:658720d51610 | 588 | { |
Shaun Nelson |
10:658720d51610 | 589 | static uint8_t buffer[64]; |
Shaun Nelson |
10:658720d51610 | 590 | uint16_t xyz_mask = 0; |
Shaun Nelson |
10:658720d51610 | 591 | |
Shaun Nelson |
10:658720d51610 | 592 | // Sensor packet type serialized to the LMIC frame buffer |
Shaun Nelson |
10:658720d51610 | 593 | SensorPacket packet(buffer, sizeof(frame)); |
Shaun Nelson |
10:658720d51610 | 594 | |
Shaun Nelson |
10:658720d51610 | 595 | packet.setPrimarySensor(xyz_mask); |
Shaun Nelson |
10:658720d51610 | 596 | packet.setTemperature(data.temperature); |
Shaun Nelson |
10:658720d51610 | 597 | packet.setPressure(data.pressure); |
Shaun Nelson |
10:658720d51610 | 598 | |
Shaun Nelson |
10:658720d51610 | 599 | // Serialize packet to LMIC transmit buffer |
Shaun Nelson |
10:658720d51610 | 600 | packet.serialize(); |
Shaun Nelson |
10:658720d51610 | 601 | |
Shaun Nelson |
10:658720d51610 | 602 | frame.assign(packet.payload(), packet.payload() + packet.length()); |
Shaun Nelson |
10:658720d51610 | 603 | |
Shaun Nelson |
10:658720d51610 | 604 | return frame.size(); |
Shaun Nelson |
10:658720d51610 | 605 | } |
Shaun Nelson |
10:658720d51610 | 606 | |
Shaun Nelson |
10:658720d51610 | 607 | |
Shaun Nelson |
10:658720d51610 | 608 | |
Shaun Nelson |
10:658720d51610 | 609 | #else |
Shaun Nelson |
10:658720d51610 | 610 | #error Board type not defined! |
Shaun Nelson |
10:658720d51610 | 611 | #endif |
Shaun Nelson |
10:658720d51610 | 612 | |
Shaun Nelson |
10:658720d51610 | 613 | void mDotConfigureAndJoin() |
Shaun Nelson |
10:658720d51610 | 614 | { |
Shaun Nelson |
10:658720d51610 | 615 | bool ok; |
Shaun Nelson |
10:658720d51610 | 616 | |
Shaun Nelson |
10:658720d51610 | 617 | // get mDot handle |
Shaun Nelson |
10:658720d51610 | 618 | mdot_radio = mDot::getInstance(); |
Shaun Nelson |
10:658720d51610 | 619 | if(mdot_radio == NULL) |
Shaun Nelson |
10:658720d51610 | 620 | { |
Shaun Nelson |
10:658720d51610 | 621 | while(1) { |
Shaun Nelson |
10:658720d51610 | 622 | printf("radio setup failed\n\r"); |
Shaun Nelson |
10:658720d51610 | 623 | osDelay(1000); |
Shaun Nelson |
10:658720d51610 | 624 | } |
Shaun Nelson |
10:658720d51610 | 625 | } |
Shaun Nelson |
10:658720d51610 | 626 | |
Shaun Nelson |
10:658720d51610 | 627 | do{ |
Shaun Nelson |
10:658720d51610 | 628 | ok = true; |
Shaun Nelson |
10:658720d51610 | 629 | |
Shaun Nelson |
10:658720d51610 | 630 | printf("\n\r setup mdot\n\r"); |
Shaun Nelson |
10:658720d51610 | 631 | |
Shaun Nelson |
10:658720d51610 | 632 | // reset to default config so we know what state we're in |
Shaun Nelson |
10:658720d51610 | 633 | mdot_radio->resetConfig(); |
Shaun Nelson |
10:658720d51610 | 634 | mdot_radio->setLogLevel(6); |
Shaun Nelson |
10:658720d51610 | 635 | |
Shaun Nelson |
10:658720d51610 | 636 | mdot_radio->setAntennaGain(-3); |
Shaun Nelson |
10:658720d51610 | 637 | |
Shaun Nelson |
10:658720d51610 | 638 | // Setting up LED1 as activity LED |
Shaun Nelson |
10:658720d51610 | 639 | #ifdef MDOT_EVB |
Shaun Nelson |
10:658720d51610 | 640 | mdot_radio->setActivityLedPin(PB_0); |
Shaun Nelson |
10:658720d51610 | 641 | mdot_radio->setActivityLedEnable(true); |
Shaun Nelson |
10:658720d51610 | 642 | #endif |
Shaun Nelson |
10:658720d51610 | 643 | |
Shaun Nelson |
10:658720d51610 | 644 | // Read node ID |
Shaun Nelson |
10:658720d51610 | 645 | std::vector<uint8_t> mdot_EUI; |
Shaun Nelson |
10:658720d51610 | 646 | mdot_EUI = mdot_radio->getDeviceId(); |
Shaun Nelson |
10:658720d51610 | 647 | printf("mDot EUI = "); |
Shaun Nelson |
10:658720d51610 | 648 | |
Shaun Nelson |
10:658720d51610 | 649 | for (uint8_t i=0; i<mdot_EUI.size(); i++) { |
Shaun Nelson |
10:658720d51610 | 650 | printf("%02x ", mdot_EUI[i]); |
Shaun Nelson |
10:658720d51610 | 651 | } |
Shaun Nelson |
10:658720d51610 | 652 | printf("\n\r"); |
Shaun Nelson |
10:658720d51610 | 653 | |
Shaun Nelson |
10:658720d51610 | 654 | |
Shaun Nelson |
10:658720d51610 | 655 | // Setting up the mDot with network information. |
Shaun Nelson |
10:658720d51610 | 656 | |
Shaun Nelson |
10:658720d51610 | 657 | /* |
Shaun Nelson |
10:658720d51610 | 658 | * This call sets up private or public mode on the MTDOT. Set the function to true if |
Shaun Nelson |
10:658720d51610 | 659 | * connecting to a public network |
Shaun Nelson |
10:658720d51610 | 660 | */ |
Shaun Nelson |
10:658720d51610 | 661 | printf("setting Public Network Mode\r\n"); |
Shaun Nelson |
10:658720d51610 | 662 | if ((mdot_ret = mdot_radio->setPublicNetwork(true)) != mDot::MDOT_OK) { |
Shaun Nelson |
10:658720d51610 | 663 | log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 664 | } |
Shaun Nelson |
10:658720d51610 | 665 | mdot_radio->setTxDataRate(mDot::DR0); |
Shaun Nelson |
10:658720d51610 | 666 | mdot_radio->setTxPower(14); |
Shaun Nelson |
10:658720d51610 | 667 | mdot_radio->setJoinRetries(1); |
Shaun Nelson |
10:658720d51610 | 668 | mdot_radio->setJoinMode(mDot::OTA); |
Shaun Nelson |
10:658720d51610 | 669 | |
Shaun Nelson |
10:658720d51610 | 670 | /* |
Shaun Nelson |
10:658720d51610 | 671 | * Frequency sub-band is valid for NAM only and for Private networks should be set to a value |
Shaun Nelson |
10:658720d51610 | 672 | * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only. |
Shaun Nelson |
10:658720d51610 | 673 | * This function can be commented out for EU networks |
Shaun Nelson |
10:658720d51610 | 674 | */ |
Shaun Nelson |
10:658720d51610 | 675 | printf("setting frequency sub band\r\n"); |
Shaun Nelson |
10:658720d51610 | 676 | if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
Shaun Nelson |
10:658720d51610 | 677 | log_error(mdot_radio, "failed to set frequency sub band", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 678 | ok = false; |
Shaun Nelson |
10:658720d51610 | 679 | } |
Shaun Nelson |
10:658720d51610 | 680 | |
Shaun Nelson |
10:658720d51610 | 681 | printf("setting ADR\r\n"); |
Shaun Nelson |
10:658720d51610 | 682 | if ((mdot_ret = mdot_radio->setAdr(config_adr_on)) != mDot::MDOT_OK) { |
Shaun Nelson |
10:658720d51610 | 683 | log_error(mdot_radio, "failed to set ADR", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 684 | ok = false; |
Shaun Nelson |
10:658720d51610 | 685 | } |
Shaun Nelson |
10:658720d51610 | 686 | /* |
Shaun Nelson |
10:658720d51610 | 687 | * setNetworkName is used for private networks. |
Shaun Nelson |
10:658720d51610 | 688 | * Use setNetworkID(AppID) for public networks |
Shaun Nelson |
10:658720d51610 | 689 | */ |
Shaun Nelson |
10:658720d51610 | 690 | printf("setting network name\r\n"); |
Shaun Nelson |
10:658720d51610 | 691 | if ((mdot_ret = mdot_radio->setNetworkId(config_app_id)) != mDot::MDOT_OK) { |
Shaun Nelson |
10:658720d51610 | 692 | log_error(mdot_radio, "failed to set network name", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 693 | ok = false; |
Shaun Nelson |
10:658720d51610 | 694 | } |
Shaun Nelson |
10:658720d51610 | 695 | |
Shaun Nelson |
10:658720d51610 | 696 | /* |
Shaun Nelson |
10:658720d51610 | 697 | * setNetworkPassphrase is used for private networks |
Shaun Nelson |
10:658720d51610 | 698 | * Use setNetworkKey for public networks |
Shaun Nelson |
10:658720d51610 | 699 | */ |
Shaun Nelson |
10:658720d51610 | 700 | printf("setting network password\r\n"); |
Shaun Nelson |
10:658720d51610 | 701 | if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) { |
Shaun Nelson |
10:658720d51610 | 702 | log_error(mdot_radio, "failed to set network password", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 703 | ok = false; |
Shaun Nelson |
10:658720d51610 | 704 | } |
Shaun Nelson |
10:658720d51610 | 705 | }while(ok == false); |
Shaun Nelson |
10:658720d51610 | 706 | |
Shaun Nelson |
10:658720d51610 | 707 | // attempt to join the network |
Shaun Nelson |
10:658720d51610 | 708 | printf("joining network\r\n"); |
Shaun Nelson |
10:658720d51610 | 709 | while (((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) && (!exit_program)) { |
Shaun Nelson |
10:658720d51610 | 710 | log_error(mdot_radio,"failed to join network:", mdot_ret); |
Shaun Nelson |
10:658720d51610 | 711 | if (mdot_radio->getFrequencyBand() == mDot::FB_868){ |
Shaun Nelson |
10:658720d51610 | 712 | mdot_ret = mdot_radio->getNextTxMs(); |
Shaun Nelson |
10:658720d51610 | 713 | } |
Shaun Nelson |
10:658720d51610 | 714 | else { |
Shaun Nelson |
10:658720d51610 | 715 | mdot_ret = 0; |
Shaun Nelson |
10:658720d51610 | 716 | } |
Shaun Nelson |
10:658720d51610 | 717 | |
Shaun Nelson |
10:658720d51610 | 718 | printf("delay = %lu\n\r",mdot_ret); |
Shaun Nelson |
10:658720d51610 | 719 | osDelay(mdot_ret + 10000); |
Shaun Nelson |
10:658720d51610 | 720 | } |
Shaun Nelson |
10:658720d51610 | 721 | } |
Shaun Nelson |
10:658720d51610 | 722 | |
Shaun Nelson |
10:658720d51610 | 723 | int main() |
Shaun Nelson |
10:658720d51610 | 724 | { |
Shaun Nelson |
10:658720d51610 | 725 | BoardSensorData sensorData; |
Shaun Nelson |
10:658720d51610 | 726 | |
Shaun Nelson |
10:658720d51610 | 727 | // Board specific initialization |
Shaun Nelson |
10:658720d51610 | 728 | BoardInit(); |
Shaun Nelson |
10:658720d51610 | 729 | |
Shaun Nelson |
10:658720d51610 | 730 | // mDot configuration and join |
Shaun Nelson |
10:658720d51610 | 731 | //mDotConfigureAndJoin(); |
Shaun Nelson |
10:658720d51610 | 732 | |
Shaun Nelson |
10:658720d51610 | 733 | PostJoinInit(); |
Shaun Nelson |
10:658720d51610 | 734 | |
Shaun Nelson |
10:658720d51610 | 735 | /* |
Shaun Nelson |
10:658720d51610 | 736 | * Check for PB1 press during network join attempt |
Shaun Nelson |
10:658720d51610 | 737 | */ |
Shaun Nelson |
10:658720d51610 | 738 | if (exit_program) { |
Shaun Nelson |
10:658720d51610 | 739 | printf("Exiting program\n\r"); |
Shaun Nelson |
10:658720d51610 | 740 | |
Shaun Nelson |
10:658720d51610 | 741 | #ifdef MDOT_EVB |
Shaun Nelson |
10:658720d51610 | 742 | evbLCD->clearBuffer(); |
Shaun Nelson |
10:658720d51610 | 743 | sprintf(txtstr,"Exiting Program"); |
Shaun Nelson |
10:658720d51610 | 744 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 745 | #endif |
Shaun Nelson |
10:658720d51610 | 746 | exit(1); |
Shaun Nelson |
10:658720d51610 | 747 | } |
falingtrea | 2:75adc72aa6a0 | 748 | |
falingtrea | 2:75adc72aa6a0 | 749 | /* |
falingtrea | 0:bdd16076aaa5 | 750 | * Main data acquisition loop |
falingtrea | 0:bdd16076aaa5 | 751 | */ |
falingtrea | 0:bdd16076aaa5 | 752 | do { |
Shaun Nelson |
10:658720d51610 | 753 | std::vector<uint8_t> frame; |
Shaun Nelson |
10:658720d51610 | 754 | |
Shaun Nelson |
10:658720d51610 | 755 | // Acquire sensor values |
Shaun Nelson |
10:658720d51610 | 756 | ReadSensors(sensorData); |
Shaun Nelson |
10:658720d51610 | 757 | |
Shaun Nelson |
10:658720d51610 | 758 | // Send sensor packets |
Shaun Nelson |
10:658720d51610 | 759 | if( PrepareFrame(frame, sensorData) > 0 ){ |
Shaun Nelson |
10:658720d51610 | 760 | SendFrame( frame ); |
Shaun Nelson |
10:658720d51610 | 761 | } |
falingtrea | 0:bdd16076aaa5 | 762 | |
Shaun Nelson |
10:658720d51610 | 763 | } while(!exit_program); |
falingtrea | 0:bdd16076aaa5 | 764 | |
falingtrea | 0:bdd16076aaa5 | 765 | |
Shaun Nelson |
10:658720d51610 | 766 | #ifdef MDOT_EVB |
falingtrea | 0:bdd16076aaa5 | 767 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 768 | do { |
falingtrea | 0:bdd16076aaa5 | 769 | osDelay(200); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 770 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 771 | } while ((result & MPL3115A2::PTDR) == 0 ); |
falingtrea | 0:bdd16076aaa5 | 772 | |
falingtrea | 0:bdd16076aaa5 | 773 | baro_data = evbBaro->getAllData(true); |
falingtrea | 0:bdd16076aaa5 | 774 | printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro, |
falingtrea | 0:bdd16076aaa5 | 775 | baro_data._mintemp, baro_data._maxtemp); |
falingtrea | 2:75adc72aa6a0 | 776 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 777 | sprintf(txtstr,"Exiting Program"); |
falingtrea | 2:75adc72aa6a0 | 778 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
Shaun Nelson |
10:658720d51610 | 779 | #endif |
falingtrea | 2:75adc72aa6a0 | 780 | |
Shaun Nelson |
10:658720d51610 | 781 | printf("End of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 782 | } |
falingtrea | 0:bdd16076aaa5 | 783 | |
Shaun Nelson |
10:658720d51610 | 784 | #ifdef MDOT_EVB |
falingtrea | 0:bdd16076aaa5 | 785 | /* |
falingtrea | 3:68e974f5f532 | 786 | * Sets pb1_low flag. Slag is cleared in pb1_debounce thread |
falingtrea | 0:bdd16076aaa5 | 787 | */ |
falingtrea | 0:bdd16076aaa5 | 788 | void pb1ISR(void) |
falingtrea | 0:bdd16076aaa5 | 789 | { |
falingtrea | 3:68e974f5f532 | 790 | if (!pb1_low) |
falingtrea | 3:68e974f5f532 | 791 | pb1_low = true; |
falingtrea | 3:68e974f5f532 | 792 | } |
falingtrea | 0:bdd16076aaa5 | 793 | |
falingtrea | 3:68e974f5f532 | 794 | /* |
falingtrea | 3:68e974f5f532 | 795 | * Debounces pb1. Also exits program if pushbutton 1 is pressed |
falingtrea | 3:68e974f5f532 | 796 | */ |
falingtrea | 3:68e974f5f532 | 797 | void pb1_debounce(void const *args) |
falingtrea | 3:68e974f5f532 | 798 | { |
falingtrea | 3:68e974f5f532 | 799 | |
falingtrea | 3:68e974f5f532 | 800 | static uint8_t count = 0; |
falingtrea | 3:68e974f5f532 | 801 | |
falingtrea | 3:68e974f5f532 | 802 | while (true) { |
falingtrea | 3:68e974f5f532 | 803 | |
falingtrea | 3:68e974f5f532 | 804 | if (pb1_low && (mDot08 == 0)) |
falingtrea | 3:68e974f5f532 | 805 | count++; |
falingtrea | 3:68e974f5f532 | 806 | else { |
falingtrea | 3:68e974f5f532 | 807 | count = 0; |
falingtrea | 3:68e974f5f532 | 808 | pb1_low = false; |
falingtrea | 3:68e974f5f532 | 809 | } |
falingtrea | 3:68e974f5f532 | 810 | |
falingtrea | 3:68e974f5f532 | 811 | if (count == 5) |
falingtrea | 3:68e974f5f532 | 812 | exit_program = true; |
falingtrea | 3:68e974f5f532 | 813 | |
falingtrea | 3:68e974f5f532 | 814 | Thread::wait(5); |
falingtrea | 3:68e974f5f532 | 815 | } |
falingtrea | 0:bdd16076aaa5 | 816 | } |
falingtrea | 0:bdd16076aaa5 | 817 | |
falingtrea | 0:bdd16076aaa5 | 818 | /* |
falingtrea | 3:68e974f5f532 | 819 | * Sets pb2_low flag. Flag is cleared in pb2_debounce thread |
falingtrea | 0:bdd16076aaa5 | 820 | */ |
falingtrea | 0:bdd16076aaa5 | 821 | void pb2ISR(void) |
falingtrea | 0:bdd16076aaa5 | 822 | { |
falingtrea | 3:68e974f5f532 | 823 | if (!pb2_low) |
falingtrea | 3:68e974f5f532 | 824 | pb2_low = true; |
falingtrea | 3:68e974f5f532 | 825 | } |
falingtrea | 0:bdd16076aaa5 | 826 | |
falingtrea | 3:68e974f5f532 | 827 | /* |
falingtrea | 3:68e974f5f532 | 828 | * Debounces pb2. Also changes packet transmit time to every other, |
falingtrea | 3:68e974f5f532 | 829 | * every fifth, or every tenth sample when SW2 pushed |
falingtrea | 3:68e974f5f532 | 830 | * Also triggers a thread to transmit a configuration packet |
falingtrea | 3:68e974f5f532 | 831 | */ |
falingtrea | 3:68e974f5f532 | 832 | void pb2_debounce(void const *args) |
falingtrea | 3:68e974f5f532 | 833 | { |
falingtrea | 3:68e974f5f532 | 834 | |
falingtrea | 3:68e974f5f532 | 835 | static uint8_t count = 0; |
falingtrea | 3:68e974f5f532 | 836 | |
falingtrea | 3:68e974f5f532 | 837 | while (true) { |
falingtrea | 0:bdd16076aaa5 | 838 | |
falingtrea | 3:68e974f5f532 | 839 | if (pb2_low && (mDot09 == 0)) |
falingtrea | 3:68e974f5f532 | 840 | count++; |
falingtrea | 3:68e974f5f532 | 841 | else { |
falingtrea | 3:68e974f5f532 | 842 | count = 0; |
falingtrea | 3:68e974f5f532 | 843 | pb2_low = false; |
falingtrea | 3:68e974f5f532 | 844 | } |
falingtrea | 3:68e974f5f532 | 845 | |
falingtrea | 3:68e974f5f532 | 846 | if (count == 5){ |
falingtrea | 0:bdd16076aaa5 | 847 | |
falingtrea | 3:68e974f5f532 | 848 | if (pckt_time >= 5) |
falingtrea | 3:68e974f5f532 | 849 | pckt_time /= 2; |
falingtrea | 3:68e974f5f532 | 850 | else pckt_time = 20; |
falingtrea | 3:68e974f5f532 | 851 | |
dkjendal | 6:b22dfacacae1 | 852 | //thread_3->signal_set(0x10); // signal config_pkt_xmit to send packet |
dkjendal | 6:b22dfacacae1 | 853 | position_changed = true; |
falingtrea | 3:68e974f5f532 | 854 | } |
falingtrea | 3:68e974f5f532 | 855 | |
falingtrea | 3:68e974f5f532 | 856 | Thread::wait(5); |
falingtrea | 3:68e974f5f532 | 857 | } |
falingtrea | 3:68e974f5f532 | 858 | } |
Shaun Nelson |
10:658720d51610 | 859 | #endif |
falingtrea | 0:bdd16076aaa5 | 860 | |
falingtrea | 2:75adc72aa6a0 | 861 | /* |
falingtrea | 2:75adc72aa6a0 | 862 | * Function that print clear text verion of mDot errors |
falingtrea | 2:75adc72aa6a0 | 863 | */ |
falingtrea | 0:bdd16076aaa5 | 864 | void log_error(mDot* dot, const char* msg, int32_t retval) |
falingtrea | 0:bdd16076aaa5 | 865 | { |
falingtrea | 0:bdd16076aaa5 | 866 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
falingtrea | 0:bdd16076aaa5 | 867 | } |
falingtrea | 0:bdd16076aaa5 | 868 | |
falingtrea | 2:75adc72aa6a0 | 869 | /* |
falingtrea | 2:75adc72aa6a0 | 870 | * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting |
falingtrea | 2:75adc72aa6a0 | 871 | */ |
falingtrea | 0:bdd16076aaa5 | 872 | void config_pkt_xmit (void const *args) |
falingtrea | 0:bdd16076aaa5 | 873 | { |
falingtrea | 0:bdd16076aaa5 | 874 | |
falingtrea | 0:bdd16076aaa5 | 875 | std::vector<uint8_t> data; |
falingtrea | 0:bdd16076aaa5 | 876 | |
falingtrea | 0:bdd16076aaa5 | 877 | while (true) { |
falingtrea | 0:bdd16076aaa5 | 878 | Thread::signal_wait(0x10); // wait for pb2ISR to signal send |
falingtrea | 0:bdd16076aaa5 | 879 | data.clear(); |
falingtrea | 0:bdd16076aaa5 | 880 | data.push_back(0x0F); // key for Configuration data (packet transmission timer) |
falingtrea | 0:bdd16076aaa5 | 881 | data.push_back(pckt_time); |
falingtrea | 0:bdd16076aaa5 | 882 | |
falingtrea | 0:bdd16076aaa5 | 883 | if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 884 | log_error(mdot_radio, "failed to send config data", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 885 | } else { |
falingtrea | 0:bdd16076aaa5 | 886 | printf("sent config data to gateway\r\n"); |
falingtrea | 0:bdd16076aaa5 | 887 | } |
falingtrea | 0:bdd16076aaa5 | 888 | } |
falingtrea | 0:bdd16076aaa5 | 889 | } |