Ag demo with soil moisture
Dependencies: DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot mbed-rtos mbed-src
Fork of MTDOT-EVBDemo-DRH by
main.cpp@1:ac9595d0f0e7, 2015-07-06 (annotated)
- Committer:
- falingtrea
- Date:
- Mon Jul 06 21:10:10 2015 +0000
- Revision:
- 1:ac9595d0f0e7
- Parent:
- 0:bdd16076aaa5
- Child:
- 2:75adc72aa6a0
Ver 1.01 Removed "NULL" from instantiation of evbAmbientLight
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 | 1:ac9595d0f0e7 | 5 | * @version 1.01 |
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 | 0:bdd16076aaa5 | 23 | */ |
falingtrea | 0:bdd16076aaa5 | 24 | |
falingtrea | 0:bdd16076aaa5 | 25 | #include "mbed.h" |
falingtrea | 0:bdd16076aaa5 | 26 | #include "MMA845x.h" |
falingtrea | 0:bdd16076aaa5 | 27 | #include "MPL3115A2.h" |
falingtrea | 0:bdd16076aaa5 | 28 | #include "ISL29011.h" |
falingtrea | 0:bdd16076aaa5 | 29 | #include "NCP5623B.h" |
falingtrea | 0:bdd16076aaa5 | 30 | #include "DOGS102.h" |
falingtrea | 0:bdd16076aaa5 | 31 | #include "font_6x8.h" |
falingtrea | 0:bdd16076aaa5 | 32 | #include "MultiTech_Logo.h" |
falingtrea | 0:bdd16076aaa5 | 33 | #include "mDot.h" |
falingtrea | 0:bdd16076aaa5 | 34 | #include "rtos.h" |
falingtrea | 0:bdd16076aaa5 | 35 | #include <string> |
falingtrea | 0:bdd16076aaa5 | 36 | #include <vector> |
falingtrea | 0:bdd16076aaa5 | 37 | |
falingtrea | 0:bdd16076aaa5 | 38 | enum LED1_COLOR { |
falingtrea | 0:bdd16076aaa5 | 39 | RED = 0, |
falingtrea | 0:bdd16076aaa5 | 40 | GREEN = 1 |
falingtrea | 0:bdd16076aaa5 | 41 | }; |
falingtrea | 0:bdd16076aaa5 | 42 | |
falingtrea | 0:bdd16076aaa5 | 43 | /* |
falingtrea | 0:bdd16076aaa5 | 44 | * union for converting from 32-bit to 4 8-bit values |
falingtrea | 0:bdd16076aaa5 | 45 | */ |
falingtrea | 0:bdd16076aaa5 | 46 | union convert32 { |
falingtrea | 0:bdd16076aaa5 | 47 | int32_t f_s; // convert from signed 32 bit int |
falingtrea | 0:bdd16076aaa5 | 48 | uint32_t f_u; // convert from unsigned 32 bit int |
falingtrea | 0:bdd16076aaa5 | 49 | uint8_t t_u[4]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 50 | }; |
falingtrea | 0:bdd16076aaa5 | 51 | |
falingtrea | 0:bdd16076aaa5 | 52 | /* |
falingtrea | 0:bdd16076aaa5 | 53 | * union for converting from 16- bit to 2 8-bit values |
falingtrea | 0:bdd16076aaa5 | 54 | */ |
falingtrea | 0:bdd16076aaa5 | 55 | union convert16 { |
falingtrea | 0:bdd16076aaa5 | 56 | int16_t f_s; // convert from signed 16 bit int |
falingtrea | 0:bdd16076aaa5 | 57 | uint16_t f_u; // convert from unsigned 16 bit int |
falingtrea | 0:bdd16076aaa5 | 58 | uint8_t t_u[2]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 59 | }; |
falingtrea | 0:bdd16076aaa5 | 60 | |
falingtrea | 0:bdd16076aaa5 | 61 | //DigitalIn mDot02(PA_2); // GPIO/UART_TX |
falingtrea | 0:bdd16076aaa5 | 62 | //DigitalOut mDot03(PA_3); // GPIO/UART_RX |
falingtrea | 0:bdd16076aaa5 | 63 | //DigitalIn mDot04(PA_6); // GPIO/SPI_MISO |
falingtrea | 0:bdd16076aaa5 | 64 | //DigitalIn mDot06(PA_8); // GPIO/I2C_SCL |
falingtrea | 0:bdd16076aaa5 | 65 | //DigitalIn mDot07(PC_9); // GPIO/I2C_SDA |
falingtrea | 0:bdd16076aaa5 | 66 | |
falingtrea | 0:bdd16076aaa5 | 67 | InterruptIn mDot08(PA_12); // GPIO/USB PB S1 on EVB |
falingtrea | 0:bdd16076aaa5 | 68 | InterruptIn mDot09(PA_11); // GPIO/USB PB S2 on EVB |
falingtrea | 0:bdd16076aaa5 | 69 | |
falingtrea | 0:bdd16076aaa5 | 70 | //DigitalIn mDot11(PA_7); // GPIO/SPI_MOSI |
falingtrea | 0:bdd16076aaa5 | 71 | |
falingtrea | 0:bdd16076aaa5 | 72 | InterruptIn mDot12(PA_0); // GPIO/UART_CTS PRESSURE_INT2 on EVB |
falingtrea | 0:bdd16076aaa5 | 73 | DigitalOut mDot13(PC_13,1); // GPIO LCD_C/D |
falingtrea | 0:bdd16076aaa5 | 74 | InterruptIn mDot15(PC_1); // GPIO LIGHT_PROX_INT on EVB |
falingtrea | 0:bdd16076aaa5 | 75 | InterruptIn mDot16(PA_1); // GPIO/UART_RTS ACCEL_INT2 on EVB |
falingtrea | 0:bdd16076aaa5 | 76 | DigitalOut mDot17(PA_4,1); // GPIO/SPI_NCS LCD_CS on EVB |
falingtrea | 0:bdd16076aaa5 | 77 | |
falingtrea | 0:bdd16076aaa5 | 78 | //DigitalIn mDot18(PA_5); // GPIO/SPI_SCK |
falingtrea | 0:bdd16076aaa5 | 79 | |
falingtrea | 0:bdd16076aaa5 | 80 | //DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO PushPull LED Low=Red High=Green set MODE=INPUT to turn off |
falingtrea | 0:bdd16076aaa5 | 81 | AnalogIn mDot20(PB_1); // GPIO Current Sense Analog in on EVB |
falingtrea | 0:bdd16076aaa5 | 82 | |
falingtrea | 0:bdd16076aaa5 | 83 | Serial debugUART(PA_9, PA_10); // mDot debug UART |
falingtrea | 0:bdd16076aaa5 | 84 | |
falingtrea | 0:bdd16076aaa5 | 85 | //Serial mDotUART(PA_2, PA_3); // mDot external UART mDot02 and mDot03 |
falingtrea | 0:bdd16076aaa5 | 86 | |
falingtrea | 0:bdd16076aaa5 | 87 | I2C mDoti2c(PC_9,PA_8); // mDot External I2C mDot6 and mDot7 |
falingtrea | 0:bdd16076aaa5 | 88 | |
falingtrea | 0:bdd16076aaa5 | 89 | SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18 |
falingtrea | 0:bdd16076aaa5 | 90 | |
falingtrea | 0:bdd16076aaa5 | 91 | // replace these values with the proper network settings |
falingtrea | 0:bdd16076aaa5 | 92 | static std::string config_network_name = "TAB-CubeNet"; |
falingtrea | 0:bdd16076aaa5 | 93 | static std::string config_network_pass = "1nt3gral"; |
falingtrea | 0:bdd16076aaa5 | 94 | static uint8_t config_frequency_sub_band = 5; |
falingtrea | 0:bdd16076aaa5 | 95 | |
falingtrea | 0:bdd16076aaa5 | 96 | uint8_t result, pckt_time=10; |
falingtrea | 0:bdd16076aaa5 | 97 | char data; |
falingtrea | 0:bdd16076aaa5 | 98 | unsigned char test; |
falingtrea | 0:bdd16076aaa5 | 99 | char txtstr[17]; |
falingtrea | 0:bdd16076aaa5 | 100 | int32_t num_whole, mdot_ret; |
falingtrea | 0:bdd16076aaa5 | 101 | uint32_t pressure; |
falingtrea | 0:bdd16076aaa5 | 102 | int16_t num_frac; |
falingtrea | 0:bdd16076aaa5 | 103 | |
falingtrea | 0:bdd16076aaa5 | 104 | bool exit_program = false; |
falingtrea | 0:bdd16076aaa5 | 105 | |
falingtrea | 0:bdd16076aaa5 | 106 | MMA845x_DATA accel_data; |
falingtrea | 0:bdd16076aaa5 | 107 | MPL3115A2_DATA baro_data; |
falingtrea | 0:bdd16076aaa5 | 108 | uint16_t lux_data; |
falingtrea | 0:bdd16076aaa5 | 109 | MMA845x* evbAccel; |
falingtrea | 0:bdd16076aaa5 | 110 | MPL3115A2* evbBaro; |
falingtrea | 0:bdd16076aaa5 | 111 | ISL29011* evbAmbLight; |
falingtrea | 0:bdd16076aaa5 | 112 | NCP5623B* evbBackLight; |
falingtrea | 0:bdd16076aaa5 | 113 | DOGS102* evbLCD; |
falingtrea | 0:bdd16076aaa5 | 114 | mDot* mdot_radio; |
falingtrea | 0:bdd16076aaa5 | 115 | |
falingtrea | 0:bdd16076aaa5 | 116 | convert32 convertl; |
falingtrea | 0:bdd16076aaa5 | 117 | convert16 converts; |
falingtrea | 0:bdd16076aaa5 | 118 | |
falingtrea | 0:bdd16076aaa5 | 119 | void pb1ISR(void); |
falingtrea | 0:bdd16076aaa5 | 120 | void pb2ISR(void); |
falingtrea | 0:bdd16076aaa5 | 121 | |
falingtrea | 0:bdd16076aaa5 | 122 | void log_error(mDot* dot, const char* msg, int32_t retval); |
falingtrea | 0:bdd16076aaa5 | 123 | |
falingtrea | 0:bdd16076aaa5 | 124 | void config_pkt_xmit (void const *args); |
falingtrea | 0:bdd16076aaa5 | 125 | |
falingtrea | 0:bdd16076aaa5 | 126 | Thread* thread1; |
falingtrea | 0:bdd16076aaa5 | 127 | |
falingtrea | 0:bdd16076aaa5 | 128 | int main() |
falingtrea | 0:bdd16076aaa5 | 129 | { |
falingtrea | 0:bdd16076aaa5 | 130 | |
falingtrea | 0:bdd16076aaa5 | 131 | std::vector<uint8_t> mdot_data; |
falingtrea | 0:bdd16076aaa5 | 132 | std::vector<uint8_t> mdot_EUI; |
falingtrea | 0:bdd16076aaa5 | 133 | uint16_t i = 0; |
falingtrea | 0:bdd16076aaa5 | 134 | |
falingtrea | 0:bdd16076aaa5 | 135 | debugUART.baud(921600); |
falingtrea | 0:bdd16076aaa5 | 136 | // mDotUART.baud(9600); // mdot UART unused but available on external connector |
falingtrea | 0:bdd16076aaa5 | 137 | |
falingtrea | 0:bdd16076aaa5 | 138 | thread1 = new Thread(config_pkt_xmit); |
falingtrea | 1:ac9595d0f0e7 | 139 | evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer |
falingtrea | 0:bdd16076aaa5 | 140 | evbBaro = new MPL3115A2(mDoti2c); // setup Barometric sensor |
falingtrea | 1:ac9595d0f0e7 | 141 | evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor |
falingtrea | 0:bdd16076aaa5 | 142 | evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip |
falingtrea | 1:ac9595d0f0e7 | 143 | evbLCD = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD |
falingtrea | 0:bdd16076aaa5 | 144 | |
falingtrea | 0:bdd16076aaa5 | 145 | printf("\n\r setup mdot\n\r"); |
falingtrea | 0:bdd16076aaa5 | 146 | |
falingtrea | 0:bdd16076aaa5 | 147 | // get a mDot handle |
falingtrea | 0:bdd16076aaa5 | 148 | mdot_radio = mDot::getInstance(); |
falingtrea | 0:bdd16076aaa5 | 149 | |
falingtrea | 0:bdd16076aaa5 | 150 | if (mdot_radio) { |
falingtrea | 0:bdd16076aaa5 | 151 | // reset to default config so we know what state we're in |
falingtrea | 0:bdd16076aaa5 | 152 | mdot_radio->resetConfig(); |
falingtrea | 0:bdd16076aaa5 | 153 | |
falingtrea | 0:bdd16076aaa5 | 154 | // Setting up LED1 as activity LED |
falingtrea | 0:bdd16076aaa5 | 155 | mdot_radio->setActivityLedPin(PB_0); |
falingtrea | 0:bdd16076aaa5 | 156 | mdot_radio->setActivityLedEnable(true); |
falingtrea | 0:bdd16076aaa5 | 157 | |
falingtrea | 0:bdd16076aaa5 | 158 | // Read node ID |
falingtrea | 0:bdd16076aaa5 | 159 | mdot_EUI = mdot_radio->getDeviceId(); |
falingtrea | 0:bdd16076aaa5 | 160 | printf("mDot EUI = "); |
falingtrea | 0:bdd16076aaa5 | 161 | |
falingtrea | 0:bdd16076aaa5 | 162 | for (i=0; i<mdot_EUI.size(); i++) { |
falingtrea | 0:bdd16076aaa5 | 163 | printf("%02x ", mdot_EUI[i]); |
falingtrea | 0:bdd16076aaa5 | 164 | } |
falingtrea | 0:bdd16076aaa5 | 165 | printf("\n\r"); |
falingtrea | 0:bdd16076aaa5 | 166 | |
falingtrea | 0:bdd16076aaa5 | 167 | |
falingtrea | 0:bdd16076aaa5 | 168 | // set up the mDot with our network information |
falingtrea | 0:bdd16076aaa5 | 169 | printf("setting frequency sub band\r\n"); |
falingtrea | 0:bdd16076aaa5 | 170 | if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 171 | log_error(mdot_radio, "failed to set frequency sub band", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 172 | } |
falingtrea | 0:bdd16076aaa5 | 173 | |
falingtrea | 0:bdd16076aaa5 | 174 | printf("setting network name\r\n"); |
falingtrea | 0:bdd16076aaa5 | 175 | if ((mdot_ret = mdot_radio->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 176 | log_error(mdot_radio, "failed to set network name", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 177 | } |
falingtrea | 0:bdd16076aaa5 | 178 | |
falingtrea | 0:bdd16076aaa5 | 179 | printf("setting network password\r\n"); |
falingtrea | 0:bdd16076aaa5 | 180 | if ((mdot_ret = mdot_radio->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 181 | log_error(mdot_radio, "failed to set network password", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 182 | } |
falingtrea | 0:bdd16076aaa5 | 183 | |
falingtrea | 0:bdd16076aaa5 | 184 | // attempt to join the network |
falingtrea | 0:bdd16076aaa5 | 185 | printf("joining network\r\n"); |
falingtrea | 0:bdd16076aaa5 | 186 | while ((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 187 | log_error(mdot_radio,"failed to join network:", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 188 | if (mdot_radio->getFrequencyBand() == mDot::FB_868){ |
falingtrea | 0:bdd16076aaa5 | 189 | mdot_ret = mdot_radio->getNextTxMs(); |
falingtrea | 0:bdd16076aaa5 | 190 | } |
falingtrea | 0:bdd16076aaa5 | 191 | else { |
falingtrea | 0:bdd16076aaa5 | 192 | mdot_ret = 0; |
falingtrea | 0:bdd16076aaa5 | 193 | } |
falingtrea | 0:bdd16076aaa5 | 194 | |
falingtrea | 0:bdd16076aaa5 | 195 | printf("delay = %lu\n\r",mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 196 | osDelay(mdot_ret + 1); |
falingtrea | 0:bdd16076aaa5 | 197 | } |
falingtrea | 0:bdd16076aaa5 | 198 | } else { |
falingtrea | 0:bdd16076aaa5 | 199 | printf("radio setup failed\n\r"); |
falingtrea | 0:bdd16076aaa5 | 200 | //exit(1); |
falingtrea | 0:bdd16076aaa5 | 201 | } |
falingtrea | 0:bdd16076aaa5 | 202 | |
falingtrea | 0:bdd16076aaa5 | 203 | osDelay(200); |
falingtrea | 0:bdd16076aaa5 | 204 | evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM |
falingtrea | 0:bdd16076aaa5 | 205 | |
falingtrea | 0:bdd16076aaa5 | 206 | /* |
falingtrea | 0:bdd16076aaa5 | 207 | * Setup SW1 as program stop function |
falingtrea | 0:bdd16076aaa5 | 208 | */ |
falingtrea | 0:bdd16076aaa5 | 209 | mDot08.disable_irq(); |
falingtrea | 0:bdd16076aaa5 | 210 | mDot08.fall(&pb1ISR); |
falingtrea | 0:bdd16076aaa5 | 211 | |
falingtrea | 0:bdd16076aaa5 | 212 | /* |
falingtrea | 0:bdd16076aaa5 | 213 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 0:bdd16076aaa5 | 214 | * mode to PullNone |
falingtrea | 0:bdd16076aaa5 | 215 | */ |
falingtrea | 0:bdd16076aaa5 | 216 | mDot08.mode(PullUp); |
falingtrea | 0:bdd16076aaa5 | 217 | mDot08.enable_irq(); |
falingtrea | 0:bdd16076aaa5 | 218 | |
falingtrea | 0:bdd16076aaa5 | 219 | /* |
falingtrea | 0:bdd16076aaa5 | 220 | * Setup SW2 as packet time change |
falingtrea | 0:bdd16076aaa5 | 221 | */ |
falingtrea | 0:bdd16076aaa5 | 222 | mDot09.disable_irq(); |
falingtrea | 0:bdd16076aaa5 | 223 | mDot09.fall(&pb2ISR); |
falingtrea | 0:bdd16076aaa5 | 224 | |
falingtrea | 0:bdd16076aaa5 | 225 | /* |
falingtrea | 0:bdd16076aaa5 | 226 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 0:bdd16076aaa5 | 227 | * mode to PullNone |
falingtrea | 0:bdd16076aaa5 | 228 | */ |
falingtrea | 0:bdd16076aaa5 | 229 | mDot09.mode(PullUp); |
falingtrea | 0:bdd16076aaa5 | 230 | mDot09.enable_irq(); |
falingtrea | 0:bdd16076aaa5 | 231 | |
falingtrea | 0:bdd16076aaa5 | 232 | /* |
falingtrea | 0:bdd16076aaa5 | 233 | * Setting other InterruptIn pins with Pull Ups |
falingtrea | 0:bdd16076aaa5 | 234 | */ |
falingtrea | 0:bdd16076aaa5 | 235 | mDot12.mode(PullUp); |
falingtrea | 0:bdd16076aaa5 | 236 | mDot15.mode(PullUp); |
falingtrea | 0:bdd16076aaa5 | 237 | mDot16.mode(PullUp); |
falingtrea | 0:bdd16076aaa5 | 238 | |
falingtrea | 0:bdd16076aaa5 | 239 | |
falingtrea | 0:bdd16076aaa5 | 240 | // sets LED2 to ramp up to 50% max current at 32 mS per step |
falingtrea | 0:bdd16076aaa5 | 241 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 242 | |
falingtrea | 0:bdd16076aaa5 | 243 | printf("font table address %p\n\r",&font_6x8); |
falingtrea | 0:bdd16076aaa5 | 244 | printf("bitmap address %p\n\r",&MultiTech_Logo); |
falingtrea | 0:bdd16076aaa5 | 245 | |
falingtrea | 0:bdd16076aaa5 | 246 | printf("Start of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 247 | |
falingtrea | 0:bdd16076aaa5 | 248 | evbLCD->startUpdate(); |
falingtrea | 0:bdd16076aaa5 | 249 | evbLCD->writeBitmap(0,0,MultiTech_Logo); |
falingtrea | 0:bdd16076aaa5 | 250 | |
falingtrea | 0:bdd16076aaa5 | 251 | sprintf(txtstr,"MTDOT"); |
falingtrea | 0:bdd16076aaa5 | 252 | evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 253 | sprintf(txtstr,"Evaluation"); |
falingtrea | 0:bdd16076aaa5 | 254 | evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 255 | sprintf(txtstr,"Board"); |
falingtrea | 0:bdd16076aaa5 | 256 | evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 257 | |
falingtrea | 0:bdd16076aaa5 | 258 | osDelay (500); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 259 | printf("shutdown LEDs and update LCD:\n\r"); |
falingtrea | 0:bdd16076aaa5 | 260 | evbBackLight->shutdown(); |
falingtrea | 0:bdd16076aaa5 | 261 | evbLCD->endUpdate(); |
falingtrea | 0:bdd16076aaa5 | 262 | |
falingtrea | 0:bdd16076aaa5 | 263 | osDelay (1000); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 264 | printf("Turn on LED2\n\r"); |
falingtrea | 0:bdd16076aaa5 | 265 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 266 | |
falingtrea | 0:bdd16076aaa5 | 267 | data = evbAccel->getWhoAmI(); |
falingtrea | 0:bdd16076aaa5 | 268 | printf("Accelerometer who_am_i value = %x \n\r", data); |
falingtrea | 0:bdd16076aaa5 | 269 | |
falingtrea | 0:bdd16076aaa5 | 270 | result = evbAccel->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 271 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 272 | |
falingtrea | 0:bdd16076aaa5 | 273 | printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE"); |
falingtrea | 0:bdd16076aaa5 | 274 | |
falingtrea | 0:bdd16076aaa5 | 275 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 276 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 277 | |
falingtrea | 0:bdd16076aaa5 | 278 | /* |
falingtrea | 0:bdd16076aaa5 | 279 | * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz |
falingtrea | 0:bdd16076aaa5 | 280 | * normal oversample mode, High pass filter off |
falingtrea | 0:bdd16076aaa5 | 281 | */ |
falingtrea | 0:bdd16076aaa5 | 282 | evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, |
falingtrea | 0:bdd16076aaa5 | 283 | MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); |
falingtrea | 0:bdd16076aaa5 | 284 | |
falingtrea | 0:bdd16076aaa5 | 285 | /* |
falingtrea | 0:bdd16076aaa5 | 286 | * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. |
falingtrea | 0:bdd16076aaa5 | 287 | * and a sample taken every second when in active mode |
falingtrea | 0:bdd16076aaa5 | 288 | */ |
falingtrea | 0:bdd16076aaa5 | 289 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 290 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 291 | /* |
falingtrea | 0:bdd16076aaa5 | 292 | * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, |
falingtrea | 0:bdd16076aaa5 | 293 | * and 16000 lux range |
falingtrea | 0:bdd16076aaa5 | 294 | */ |
falingtrea | 0:bdd16076aaa5 | 295 | |
falingtrea | 0:bdd16076aaa5 | 296 | evbAmbLight->setMode(ISL29011::ALS_CONT); |
falingtrea | 0:bdd16076aaa5 | 297 | evbAmbLight->setResolution(ISL29011::ADC_16BIT); |
falingtrea | 0:bdd16076aaa5 | 298 | evbAmbLight->setRange(ISL29011::RNG_16000); |
falingtrea | 0:bdd16076aaa5 | 299 | |
falingtrea | 0:bdd16076aaa5 | 300 | /* |
falingtrea | 0:bdd16076aaa5 | 301 | * Set the accelerometer for active mode |
falingtrea | 0:bdd16076aaa5 | 302 | */ |
falingtrea | 0:bdd16076aaa5 | 303 | evbAccel->activeMode(); |
falingtrea | 0:bdd16076aaa5 | 304 | |
falingtrea | 0:bdd16076aaa5 | 305 | /* |
falingtrea | 0:bdd16076aaa5 | 306 | * Clear the min-max registers in the Barometric Sensor |
falingtrea | 0:bdd16076aaa5 | 307 | */ |
falingtrea | 0:bdd16076aaa5 | 308 | evbBaro->clearMinMaxRegs(); |
falingtrea | 0:bdd16076aaa5 | 309 | |
falingtrea | 0:bdd16076aaa5 | 310 | evbBackLight->setLEDCurrent(0); |
falingtrea | 0:bdd16076aaa5 | 311 | |
falingtrea | 0:bdd16076aaa5 | 312 | /* |
falingtrea | 0:bdd16076aaa5 | 313 | * Main data acquisition loop |
falingtrea | 0:bdd16076aaa5 | 314 | */ |
falingtrea | 0:bdd16076aaa5 | 315 | pckt_time = 10; |
falingtrea | 0:bdd16076aaa5 | 316 | i = 0; |
falingtrea | 0:bdd16076aaa5 | 317 | |
falingtrea | 0:bdd16076aaa5 | 318 | do { |
falingtrea | 0:bdd16076aaa5 | 319 | evbLCD->startUpdate(); |
falingtrea | 0:bdd16076aaa5 | 320 | evbLCD->clearBuffer(); |
falingtrea | 0:bdd16076aaa5 | 321 | |
falingtrea | 0:bdd16076aaa5 | 322 | /* |
falingtrea | 0:bdd16076aaa5 | 323 | * Test Accelerometer XYZ data ready bit to see if acquisition complete |
falingtrea | 0:bdd16076aaa5 | 324 | */ |
falingtrea | 0:bdd16076aaa5 | 325 | do { |
falingtrea | 0:bdd16076aaa5 | 326 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 327 | result = evbAccel->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 328 | } while ((result & MMA845x::XYZDR) == 0 ); |
falingtrea | 0:bdd16076aaa5 | 329 | |
falingtrea | 0:bdd16076aaa5 | 330 | /* |
falingtrea | 0:bdd16076aaa5 | 331 | * Retrieve and print out accelerometer data |
falingtrea | 0:bdd16076aaa5 | 332 | */ |
falingtrea | 0:bdd16076aaa5 | 333 | accel_data = evbAccel->getXYZ(); |
falingtrea | 0:bdd16076aaa5 | 334 | |
falingtrea | 0:bdd16076aaa5 | 335 | sprintf(txtstr,"Accelerometer"); |
falingtrea | 0:bdd16076aaa5 | 336 | evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 337 | sprintf(txtstr, "x = %d", accel_data._x); |
falingtrea | 0:bdd16076aaa5 | 338 | evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 339 | sprintf(txtstr, "y = %d", accel_data._y); |
falingtrea | 0:bdd16076aaa5 | 340 | evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 341 | sprintf(txtstr, "z = %d", accel_data._z ); |
falingtrea | 0:bdd16076aaa5 | 342 | evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 343 | |
falingtrea | 0:bdd16076aaa5 | 344 | /* |
falingtrea | 0:bdd16076aaa5 | 345 | * Trigger a Pressure reading |
falingtrea | 0:bdd16076aaa5 | 346 | */ |
falingtrea | 0:bdd16076aaa5 | 347 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 348 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 349 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 350 | |
falingtrea | 0:bdd16076aaa5 | 351 | /* |
falingtrea | 0:bdd16076aaa5 | 352 | * Test barometer device status to see if acquisition is complete |
falingtrea | 0:bdd16076aaa5 | 353 | */ |
falingtrea | 0:bdd16076aaa5 | 354 | do { |
falingtrea | 0:bdd16076aaa5 | 355 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 356 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 357 | } while ((result & MPL3115A2::PTDR) == 0 ); |
falingtrea | 0:bdd16076aaa5 | 358 | |
falingtrea | 0:bdd16076aaa5 | 359 | /* |
falingtrea | 0:bdd16076aaa5 | 360 | * Retrieve and print out barometric pressure |
falingtrea | 0:bdd16076aaa5 | 361 | */ |
falingtrea | 0:bdd16076aaa5 | 362 | pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value |
falingtrea | 0:bdd16076aaa5 | 363 | num_whole = pressure >> 2; // 18 bit integer significant |
falingtrea | 0:bdd16076aaa5 | 364 | num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit |
falingtrea | 0:bdd16076aaa5 | 365 | sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac); |
falingtrea | 0:bdd16076aaa5 | 366 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 367 | |
falingtrea | 0:bdd16076aaa5 | 368 | /* |
falingtrea | 0:bdd16076aaa5 | 369 | * Trigger a Altitude reading |
falingtrea | 0:bdd16076aaa5 | 370 | */ |
falingtrea | 0:bdd16076aaa5 | 371 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 372 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 373 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 374 | |
falingtrea | 0:bdd16076aaa5 | 375 | /* |
falingtrea | 0:bdd16076aaa5 | 376 | * Test barometer device status to see if acquisition is complete |
falingtrea | 0:bdd16076aaa5 | 377 | */ |
falingtrea | 0:bdd16076aaa5 | 378 | do { |
falingtrea | 0:bdd16076aaa5 | 379 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 380 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 381 | } while ((result & MPL3115A2::PTDR) == 0 ); |
falingtrea | 0:bdd16076aaa5 | 382 | |
falingtrea | 0:bdd16076aaa5 | 383 | /* |
falingtrea | 0:bdd16076aaa5 | 384 | * Retrieve and print out altitude and temperature |
falingtrea | 0:bdd16076aaa5 | 385 | */ |
falingtrea | 0:bdd16076aaa5 | 386 | baro_data = evbBaro->getAllData(false); |
falingtrea | 0:bdd16076aaa5 | 387 | baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value |
falingtrea | 0:bdd16076aaa5 | 388 | num_whole = baro_data._baro / 16; // 18 bit signed significant integer |
falingtrea | 0:bdd16076aaa5 | 389 | num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit |
falingtrea | 0:bdd16076aaa5 | 390 | sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac); |
falingtrea | 0:bdd16076aaa5 | 391 | evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 392 | num_whole = baro_data._temp / 16; // 8 bit signed significant integer |
falingtrea | 0:bdd16076aaa5 | 393 | num_frac = (baro_data._temp & 0x0F) * 625 / 100; // 4 bit fractional .0625 per bit |
falingtrea | 0:bdd16076aaa5 | 394 | sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac); |
falingtrea | 0:bdd16076aaa5 | 395 | evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 396 | |
falingtrea | 0:bdd16076aaa5 | 397 | /* |
falingtrea | 0:bdd16076aaa5 | 398 | * retrieve and print out Ambient Light level |
falingtrea | 0:bdd16076aaa5 | 399 | */ |
falingtrea | 0:bdd16076aaa5 | 400 | lux_data = evbAmbLight->getData(); |
falingtrea | 0:bdd16076aaa5 | 401 | num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit |
falingtrea | 0:bdd16076aaa5 | 402 | num_frac = lux_data * 24 % 100; |
falingtrea | 0:bdd16076aaa5 | 403 | sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac ); |
falingtrea | 0:bdd16076aaa5 | 404 | evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 405 | |
falingtrea | 0:bdd16076aaa5 | 406 | evbLCD->endUpdate(); |
falingtrea | 0:bdd16076aaa5 | 407 | printf("finished iteration %d\n\r",(++i)); |
falingtrea | 0:bdd16076aaa5 | 408 | |
falingtrea | 0:bdd16076aaa5 | 409 | if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops |
falingtrea | 0:bdd16076aaa5 | 410 | mdot_data.clear(); |
falingtrea | 0:bdd16076aaa5 | 411 | mdot_data.push_back(0x0E); // key for Current Acceleration 3-Axis Value |
falingtrea | 0:bdd16076aaa5 | 412 | converts.f_s = accel_data._x *4; // shift data 2 bits while retaining sign |
falingtrea | 0:bdd16076aaa5 | 413 | mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
falingtrea | 0:bdd16076aaa5 | 414 | converts.f_s = accel_data._y * 4; // shift data 2 bits while retaining sign |
falingtrea | 0:bdd16076aaa5 | 415 | mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
falingtrea | 0:bdd16076aaa5 | 416 | converts.f_s = accel_data._z * 4; // shift data 2 bits while retaining sign |
falingtrea | 0:bdd16076aaa5 | 417 | mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
falingtrea | 0:bdd16076aaa5 | 418 | mdot_data.push_back(0x08); // key for Current Pressure Value |
falingtrea | 0:bdd16076aaa5 | 419 | convertl.f_u = pressure; // pressure data is 20 bits unsigned |
falingtrea | 0:bdd16076aaa5 | 420 | mdot_data.push_back(convertl.t_u[2]); |
falingtrea | 0:bdd16076aaa5 | 421 | mdot_data.push_back(convertl.t_u[1]); |
falingtrea | 0:bdd16076aaa5 | 422 | mdot_data.push_back(convertl.t_u[0]); |
falingtrea | 0:bdd16076aaa5 | 423 | mdot_data.push_back(0x05); // key for Current Ambient Light Value |
falingtrea | 0:bdd16076aaa5 | 424 | converts.f_u = lux_data; // data is 16 bits unsigned |
falingtrea | 0:bdd16076aaa5 | 425 | mdot_data.push_back(converts.t_u[1]); |
falingtrea | 0:bdd16076aaa5 | 426 | mdot_data.push_back(converts.t_u[0]); |
falingtrea | 0:bdd16076aaa5 | 427 | mdot_data.push_back(0x0B); // key for Current Temperature Value |
falingtrea | 0:bdd16076aaa5 | 428 | converts.f_s = baro_data._temp; // temperature is signed 12 bit |
falingtrea | 0:bdd16076aaa5 | 429 | mdot_data.push_back(converts.t_u[1]); |
falingtrea | 0:bdd16076aaa5 | 430 | mdot_data.push_back(converts.t_u[0]); |
falingtrea | 0:bdd16076aaa5 | 431 | |
falingtrea | 0:bdd16076aaa5 | 432 | if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 433 | log_error(mdot_radio, "failed to send", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 434 | } else { |
falingtrea | 0:bdd16076aaa5 | 435 | printf("successfully sent data to gateway\r\n"); |
falingtrea | 0:bdd16076aaa5 | 436 | } |
falingtrea | 0:bdd16076aaa5 | 437 | } |
falingtrea | 0:bdd16076aaa5 | 438 | } while(!exit_program | (i > 65000)); |
falingtrea | 0:bdd16076aaa5 | 439 | |
falingtrea | 0:bdd16076aaa5 | 440 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 441 | |
falingtrea | 0:bdd16076aaa5 | 442 | do { |
falingtrea | 0:bdd16076aaa5 | 443 | osDelay(200); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 444 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 445 | } while ((result & MPL3115A2::PTDR) == 0 ); |
falingtrea | 0:bdd16076aaa5 | 446 | |
falingtrea | 0:bdd16076aaa5 | 447 | baro_data = evbBaro->getAllData(true); |
falingtrea | 0:bdd16076aaa5 | 448 | printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro, |
falingtrea | 0:bdd16076aaa5 | 449 | baro_data._mintemp, baro_data._maxtemp); |
falingtrea | 0:bdd16076aaa5 | 450 | |
falingtrea | 0:bdd16076aaa5 | 451 | printf("End of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 452 | |
falingtrea | 0:bdd16076aaa5 | 453 | } |
falingtrea | 0:bdd16076aaa5 | 454 | |
falingtrea | 0:bdd16076aaa5 | 455 | /* |
falingtrea | 0:bdd16076aaa5 | 456 | * Ends program when button 1 pushed |
falingtrea | 0:bdd16076aaa5 | 457 | */ |
falingtrea | 0:bdd16076aaa5 | 458 | void pb1ISR(void) |
falingtrea | 0:bdd16076aaa5 | 459 | { |
falingtrea | 0:bdd16076aaa5 | 460 | printf("calling ISR for SW1\n\r"); |
falingtrea | 0:bdd16076aaa5 | 461 | |
falingtrea | 0:bdd16076aaa5 | 462 | exit_program = true; |
falingtrea | 0:bdd16076aaa5 | 463 | } |
falingtrea | 0:bdd16076aaa5 | 464 | |
falingtrea | 0:bdd16076aaa5 | 465 | /* |
falingtrea | 0:bdd16076aaa5 | 466 | * changes packet transmit time to every other, every fifth, or every tenth sample when button 2 pushed |
falingtrea | 0:bdd16076aaa5 | 467 | * Also triggers a thread to transmit a configuration packet |
falingtrea | 0:bdd16076aaa5 | 468 | */ |
falingtrea | 0:bdd16076aaa5 | 469 | void pb2ISR(void) |
falingtrea | 0:bdd16076aaa5 | 470 | { |
falingtrea | 0:bdd16076aaa5 | 471 | |
falingtrea | 0:bdd16076aaa5 | 472 | printf("calling ISR for SW2\n\r"); |
falingtrea | 0:bdd16076aaa5 | 473 | |
falingtrea | 0:bdd16076aaa5 | 474 | if (pckt_time >= 5) |
falingtrea | 0:bdd16076aaa5 | 475 | pckt_time /= 2; |
falingtrea | 0:bdd16076aaa5 | 476 | else pckt_time = 20; |
falingtrea | 0:bdd16076aaa5 | 477 | |
falingtrea | 0:bdd16076aaa5 | 478 | thread1->signal_set(0x10); // signal config_pkt_xmit to send packet |
falingtrea | 0:bdd16076aaa5 | 479 | |
falingtrea | 0:bdd16076aaa5 | 480 | printf ("pckt_time = %d\n\r",pckt_time); |
falingtrea | 0:bdd16076aaa5 | 481 | |
falingtrea | 0:bdd16076aaa5 | 482 | } |
falingtrea | 0:bdd16076aaa5 | 483 | |
falingtrea | 0:bdd16076aaa5 | 484 | void log_error(mDot* dot, const char* msg, int32_t retval) |
falingtrea | 0:bdd16076aaa5 | 485 | { |
falingtrea | 0:bdd16076aaa5 | 486 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
falingtrea | 0:bdd16076aaa5 | 487 | } |
falingtrea | 0:bdd16076aaa5 | 488 | |
falingtrea | 0:bdd16076aaa5 | 489 | void config_pkt_xmit (void const *args) |
falingtrea | 0:bdd16076aaa5 | 490 | { |
falingtrea | 0:bdd16076aaa5 | 491 | |
falingtrea | 0:bdd16076aaa5 | 492 | std::vector<uint8_t> data; |
falingtrea | 0:bdd16076aaa5 | 493 | |
falingtrea | 0:bdd16076aaa5 | 494 | while (true) { |
falingtrea | 0:bdd16076aaa5 | 495 | Thread::signal_wait(0x10); // wait for pb2ISR to signal send |
falingtrea | 0:bdd16076aaa5 | 496 | data.clear(); |
falingtrea | 0:bdd16076aaa5 | 497 | data.push_back(0x0F); // key for Configuration data (packet transmission timer) |
falingtrea | 0:bdd16076aaa5 | 498 | data.push_back(pckt_time); |
falingtrea | 0:bdd16076aaa5 | 499 | |
falingtrea | 0:bdd16076aaa5 | 500 | if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 501 | log_error(mdot_radio, "failed to send config data", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 502 | } else { |
falingtrea | 0:bdd16076aaa5 | 503 | printf("sent config data to gateway\r\n"); |
falingtrea | 0:bdd16076aaa5 | 504 | } |
falingtrea | 0:bdd16076aaa5 | 505 | } |
falingtrea | 0:bdd16076aaa5 | 506 | } |