send GPS data via LoRaWAN
Dependencies: TinyGPS mbed ADXL345_I2C Sht31
EEAB-P1/Eeabp1.cpp@1:195da8230785, 2017-06-24 (annotated)
- Committer:
- koyo_take
- Date:
- Sat Jun 24 23:42:01 2017 +0000
- Revision:
- 1:195da8230785
- Parent:
- 0:97a57be77fbb
add samples to handle sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
koyo_take | 0:97a57be77fbb | 1 | #include "Eeabp1.h" |
koyo_take | 0:97a57be77fbb | 2 | //#include "rtos.h" |
koyo_take | 0:97a57be77fbb | 3 | |
koyo_take | 0:97a57be77fbb | 4 | //Thread thread; |
koyo_take | 0:97a57be77fbb | 5 | static const int lora_msg_max_len = 11; |
koyo_take | 0:97a57be77fbb | 6 | |
koyo_take | 1:195da8230785 | 7 | Eeabp1::Eeabp1() : pwr_en(P0_0), led(P0_23), |
koyo_take | 1:195da8230785 | 8 | lora_power(P0_5), lora_reset(P0_22), grove_power(P0_6), |
koyo_take | 1:195da8230785 | 9 | grv_sel2a(P0_19), grv_sel2g(P0_18), grv_sel1a(P0_17), grv_sel1g(P0_16), |
koyo_take | 1:195da8230785 | 10 | grv_p1s1a(P0_2), grv_p1s2a(P0_1), |
koyo_take | 1:195da8230785 | 11 | grv_p2s1a(P0_4), grv_p2s2a(P0_3) |
koyo_take | 0:97a57be77fbb | 12 | { |
koyo_take | 0:97a57be77fbb | 13 | this->led_state = LED_OFF; |
koyo_take | 0:97a57be77fbb | 14 | this->led = 0; |
koyo_take | 0:97a57be77fbb | 15 | this->serial = new RawSerial(P0_9, P0_11); |
koyo_take | 0:97a57be77fbb | 16 | this->serial->baud(9600); /* use for LoRa */ |
koyo_take | 0:97a57be77fbb | 17 | this->pwr_en = 1; // メイン電源投入 |
koyo_take | 0:97a57be77fbb | 18 | this->lora_power = 0; |
koyo_take | 0:97a57be77fbb | 19 | this->lora_reset = 0; |
koyo_take | 0:97a57be77fbb | 20 | this->grove_power = 0; |
koyo_take | 0:97a57be77fbb | 21 | this->lora_enabled = false; |
koyo_take | 0:97a57be77fbb | 22 | this->grove_enabled = false; |
koyo_take | 1:195da8230785 | 23 | // Grove端子をHiZに |
koyo_take | 1:195da8230785 | 24 | this->grv_sel1g = 1; |
koyo_take | 1:195da8230785 | 25 | this->grv_sel1a = 1; |
koyo_take | 1:195da8230785 | 26 | this->grv_sel2g = 1; |
koyo_take | 1:195da8230785 | 27 | this->grv_sel2a = 1; |
koyo_take | 1:195da8230785 | 28 | grv_p1s1do = NULL, grv_p1s2do = NULL, grv_p1s1di = NULL, grv_p1s2di = NULL; |
koyo_take | 1:195da8230785 | 29 | grv_p2s1do = NULL, grv_p2s2do = NULL, grv_p2s1di = NULL, grv_p2s2di = NULL; |
koyo_take | 1:195da8230785 | 30 | temp_humid_sensor = NULL; |
koyo_take | 0:97a57be77fbb | 31 | } |
koyo_take | 0:97a57be77fbb | 32 | |
koyo_take | 0:97a57be77fbb | 33 | int Eeabp1::setLedState(EeabLedState state) |
koyo_take | 0:97a57be77fbb | 34 | { |
koyo_take | 0:97a57be77fbb | 35 | this->led_state = state; |
koyo_take | 0:97a57be77fbb | 36 | |
koyo_take | 0:97a57be77fbb | 37 | if (this->led_state == LED_OFF) { |
koyo_take | 0:97a57be77fbb | 38 | this->led = 0; |
koyo_take | 0:97a57be77fbb | 39 | } else { |
koyo_take | 0:97a57be77fbb | 40 | this->led = 1; |
koyo_take | 0:97a57be77fbb | 41 | } |
koyo_take | 0:97a57be77fbb | 42 | |
koyo_take | 0:97a57be77fbb | 43 | return 0; |
koyo_take | 0:97a57be77fbb | 44 | } |
koyo_take | 0:97a57be77fbb | 45 | |
koyo_take | 0:97a57be77fbb | 46 | void Eeabp1::loop(void) |
koyo_take | 0:97a57be77fbb | 47 | { |
koyo_take | 0:97a57be77fbb | 48 | switch (this->led_state) { |
koyo_take | 0:97a57be77fbb | 49 | case LED_OFF: |
koyo_take | 0:97a57be77fbb | 50 | /* fall through */ |
koyo_take | 0:97a57be77fbb | 51 | case LED_ON: |
koyo_take | 0:97a57be77fbb | 52 | break; |
koyo_take | 0:97a57be77fbb | 53 | case LED_BLINK_FAST: |
koyo_take | 0:97a57be77fbb | 54 | case LED_BLINK_MID: |
koyo_take | 0:97a57be77fbb | 55 | case LED_BLINK_SLOW: |
koyo_take | 0:97a57be77fbb | 56 | this->led = !this->led; |
koyo_take | 0:97a57be77fbb | 57 | break; |
koyo_take | 0:97a57be77fbb | 58 | } |
koyo_take | 0:97a57be77fbb | 59 | } |
koyo_take | 0:97a57be77fbb | 60 | |
koyo_take | 0:97a57be77fbb | 61 | int Eeabp1::debug(const char * format, ...) |
koyo_take | 0:97a57be77fbb | 62 | { |
koyo_take | 0:97a57be77fbb | 63 | char tmp[80]; |
koyo_take | 0:97a57be77fbb | 64 | int ret; |
koyo_take | 0:97a57be77fbb | 65 | |
koyo_take | 0:97a57be77fbb | 66 | std::va_list arg; |
koyo_take | 0:97a57be77fbb | 67 | va_start(arg, format); |
koyo_take | 0:97a57be77fbb | 68 | vsnprintf(tmp, sizeof(tmp), format, arg); |
koyo_take | 0:97a57be77fbb | 69 | va_end(arg); |
koyo_take | 0:97a57be77fbb | 70 | |
koyo_take | 0:97a57be77fbb | 71 | delete this->serial; |
koyo_take | 0:97a57be77fbb | 72 | this->serial = new RawSerial(P0_8, P0_10); |
koyo_take | 0:97a57be77fbb | 73 | ret = this->serial->puts(tmp); |
koyo_take | 0:97a57be77fbb | 74 | delete this->serial; |
koyo_take | 0:97a57be77fbb | 75 | this->serial = new RawSerial(P0_9, P0_11); |
koyo_take | 0:97a57be77fbb | 76 | |
koyo_take | 0:97a57be77fbb | 77 | return ret; |
koyo_take | 0:97a57be77fbb | 78 | } |
koyo_take | 0:97a57be77fbb | 79 | |
koyo_take | 0:97a57be77fbb | 80 | int Eeabp1::setLoRaPower(bool on) |
koyo_take | 0:97a57be77fbb | 81 | { |
koyo_take | 0:97a57be77fbb | 82 | int ret; |
koyo_take | 0:97a57be77fbb | 83 | |
koyo_take | 0:97a57be77fbb | 84 | if (on) { |
koyo_take | 0:97a57be77fbb | 85 | if (lora_enabled) |
koyo_take | 0:97a57be77fbb | 86 | return 0; /* power is already on, do nothing */ |
koyo_take | 0:97a57be77fbb | 87 | |
koyo_take | 0:97a57be77fbb | 88 | this->lora_power= 1; |
koyo_take | 0:97a57be77fbb | 89 | this->lora_reset = 1; |
koyo_take | 0:97a57be77fbb | 90 | wait_us(500000); |
koyo_take | 0:97a57be77fbb | 91 | serial->printf("mod set_echo off\r\n"); // ローカルエコー:無効 |
koyo_take | 0:97a57be77fbb | 92 | wait(1); |
koyo_take | 0:97a57be77fbb | 93 | flushSerial(); |
koyo_take | 1:195da8230785 | 94 | serial->printf("mod factory_reset\r\n"); // ファクトリーリセットを行い鍵をリフレッシュ |
koyo_take | 1:195da8230785 | 95 | ret = chkSerialCharOk(); |
koyo_take | 1:195da8230785 | 96 | if (ret != 0) |
koyo_take | 1:195da8230785 | 97 | return ret; |
koyo_take | 1:195da8230785 | 98 | serial->printf("mod set_echo off\r\n"); // ローカルエコー:無効 |
koyo_take | 1:195da8230785 | 99 | wait(1); |
koyo_take | 0:97a57be77fbb | 100 | serial->printf("lorawan join otaa\r\n"); // Gatewayに接続(OTAA) |
koyo_take | 0:97a57be77fbb | 101 | ret = chkSerialCharRes('a'); // 成功 ">> accepted" 失敗 ">> unsuccess" |
koyo_take | 0:97a57be77fbb | 102 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 103 | return ret; |
koyo_take | 0:97a57be77fbb | 104 | serial->printf("lorawan set_dr 2\r\n"); // データレートの設定(11byte) |
koyo_take | 0:97a57be77fbb | 105 | ret = chkSerialCharOk(); |
koyo_take | 0:97a57be77fbb | 106 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 107 | return ret; |
koyo_take | 0:97a57be77fbb | 108 | |
koyo_take | 0:97a57be77fbb | 109 | this->lora_enabled = true; |
koyo_take | 0:97a57be77fbb | 110 | } else { /* off */ |
koyo_take | 0:97a57be77fbb | 111 | if (!lora_enabled) |
koyo_take | 0:97a57be77fbb | 112 | return 0; /* power is already off, do nothing */ |
koyo_take | 0:97a57be77fbb | 113 | |
koyo_take | 0:97a57be77fbb | 114 | lora_power= 0; |
koyo_take | 0:97a57be77fbb | 115 | lora_enabled = false; |
koyo_take | 0:97a57be77fbb | 116 | } |
koyo_take | 0:97a57be77fbb | 117 | |
koyo_take | 0:97a57be77fbb | 118 | return 0; |
koyo_take | 0:97a57be77fbb | 119 | } |
koyo_take | 0:97a57be77fbb | 120 | |
koyo_take | 0:97a57be77fbb | 121 | int Eeabp1::sendLoRaString(const char * format, ...) |
koyo_take | 0:97a57be77fbb | 122 | { |
koyo_take | 0:97a57be77fbb | 123 | char str[lora_msg_max_len+1] = {0x00, }; |
koyo_take | 0:97a57be77fbb | 124 | char msg[64]; |
koyo_take | 0:97a57be77fbb | 125 | char *p = msg; |
koyo_take | 0:97a57be77fbb | 126 | |
koyo_take | 0:97a57be77fbb | 127 | std::va_list arg; |
koyo_take | 0:97a57be77fbb | 128 | va_start(arg, format); |
koyo_take | 0:97a57be77fbb | 129 | vsnprintf(str, sizeof(str), format, arg); |
koyo_take | 0:97a57be77fbb | 130 | va_end(arg); |
koyo_take | 0:97a57be77fbb | 131 | |
koyo_take | 0:97a57be77fbb | 132 | p += sprintf(msg, "lorawan tx ucnf 16 "); |
koyo_take | 0:97a57be77fbb | 133 | for (unsigned int i = 0; i < strlen(str); i++) { |
koyo_take | 0:97a57be77fbb | 134 | p += sprintf(p, "%02x", str[i]); |
koyo_take | 0:97a57be77fbb | 135 | } |
koyo_take | 0:97a57be77fbb | 136 | sprintf(p, "\r\n"); |
koyo_take | 0:97a57be77fbb | 137 | |
koyo_take | 0:97a57be77fbb | 138 | flushSerial(); |
koyo_take | 0:97a57be77fbb | 139 | serial->puts(msg); |
koyo_take | 0:97a57be77fbb | 140 | |
koyo_take | 0:97a57be77fbb | 141 | return 0; |
koyo_take | 0:97a57be77fbb | 142 | } |
koyo_take | 0:97a57be77fbb | 143 | |
koyo_take | 0:97a57be77fbb | 144 | void Eeabp1::setGrovePower(bool on) |
koyo_take | 0:97a57be77fbb | 145 | { |
koyo_take | 0:97a57be77fbb | 146 | if (on) { |
koyo_take | 0:97a57be77fbb | 147 | if (grove_enabled) |
koyo_take | 0:97a57be77fbb | 148 | return; /* power is already on, do nothing */ |
koyo_take | 0:97a57be77fbb | 149 | |
koyo_take | 0:97a57be77fbb | 150 | grove_power = 1; |
koyo_take | 0:97a57be77fbb | 151 | } else { |
koyo_take | 0:97a57be77fbb | 152 | if (!grove_enabled) |
koyo_take | 0:97a57be77fbb | 153 | return; /* power is already off, do nothing */ |
koyo_take | 0:97a57be77fbb | 154 | |
koyo_take | 1:195da8230785 | 155 | // Grove端子をHiZに |
koyo_take | 1:195da8230785 | 156 | grv_sel1g = 1; |
koyo_take | 1:195da8230785 | 157 | grv_sel1a = 1; |
koyo_take | 1:195da8230785 | 158 | grv_sel2g = 1; |
koyo_take | 1:195da8230785 | 159 | grv_sel2a = 1; |
koyo_take | 0:97a57be77fbb | 160 | grove_power = 0; |
koyo_take | 0:97a57be77fbb | 161 | } |
koyo_take | 0:97a57be77fbb | 162 | } |
koyo_take | 0:97a57be77fbb | 163 | |
koyo_take | 1:195da8230785 | 164 | int Eeabp1::setGrovePortType(EeabGrovePort port, EeabGrovePortType type) |
koyo_take | 1:195da8230785 | 165 | { |
koyo_take | 1:195da8230785 | 166 | switch (port) { |
koyo_take | 1:195da8230785 | 167 | case GROVE_CH1: |
koyo_take | 1:195da8230785 | 168 | if (type == GROVE_ANALOG) { |
koyo_take | 1:195da8230785 | 169 | grv_sel1g = 1; |
koyo_take | 1:195da8230785 | 170 | grv_sel1a = 0; |
koyo_take | 1:195da8230785 | 171 | } else { |
koyo_take | 1:195da8230785 | 172 | grv_sel1g = 0; |
koyo_take | 1:195da8230785 | 173 | grv_sel1a = 1; |
koyo_take | 1:195da8230785 | 174 | } |
koyo_take | 1:195da8230785 | 175 | break; |
koyo_take | 1:195da8230785 | 176 | case GROVE_CH2: |
koyo_take | 1:195da8230785 | 177 | if (type == GROVE_ANALOG) { |
koyo_take | 1:195da8230785 | 178 | grv_sel2g = 1; |
koyo_take | 1:195da8230785 | 179 | grv_sel2a = 0; |
koyo_take | 1:195da8230785 | 180 | } else { |
koyo_take | 1:195da8230785 | 181 | grv_sel2g = 0; |
koyo_take | 1:195da8230785 | 182 | grv_sel2a = 1; |
koyo_take | 1:195da8230785 | 183 | } |
koyo_take | 1:195da8230785 | 184 | break; |
koyo_take | 1:195da8230785 | 185 | default: |
koyo_take | 1:195da8230785 | 186 | /* do nothing */ |
koyo_take | 1:195da8230785 | 187 | break; |
koyo_take | 1:195da8230785 | 188 | } |
koyo_take | 1:195da8230785 | 189 | |
koyo_take | 1:195da8230785 | 190 | return 0; |
koyo_take | 1:195da8230785 | 191 | } |
koyo_take | 1:195da8230785 | 192 | |
koyo_take | 1:195da8230785 | 193 | int Eeabp1::setGroveDioDirection(EeabGrovePort port, EeabGroveDioDirection dir, Callback<void()> f) |
koyo_take | 1:195da8230785 | 194 | { |
koyo_take | 1:195da8230785 | 195 | switch (port) { |
koyo_take | 1:195da8230785 | 196 | case GROVE_CH1: |
koyo_take | 1:195da8230785 | 197 | if(dir == GROVE_DIO_OUT) { |
koyo_take | 1:195da8230785 | 198 | if (grv_p1s1di) { |
koyo_take | 1:195da8230785 | 199 | delete grv_p1s1di; |
koyo_take | 1:195da8230785 | 200 | grv_p1s1di = NULL; |
koyo_take | 1:195da8230785 | 201 | } |
koyo_take | 1:195da8230785 | 202 | grv_p1s1do = new DigitalOut(P0_13); |
koyo_take | 1:195da8230785 | 203 | |
koyo_take | 1:195da8230785 | 204 | if (grv_p1s2di) { |
koyo_take | 1:195da8230785 | 205 | delete grv_p1s2di; |
koyo_take | 1:195da8230785 | 206 | grv_p1s2di = NULL; |
koyo_take | 1:195da8230785 | 207 | } |
koyo_take | 1:195da8230785 | 208 | grv_p1s2do = new DigitalOut(P0_12); |
koyo_take | 1:195da8230785 | 209 | } else { /* GROVE_DIO_IN */ |
koyo_take | 1:195da8230785 | 210 | if (grv_p1s1do) { |
koyo_take | 1:195da8230785 | 211 | delete grv_p1s1do; |
koyo_take | 1:195da8230785 | 212 | grv_p1s1do = NULL; |
koyo_take | 1:195da8230785 | 213 | } |
koyo_take | 1:195da8230785 | 214 | grv_p1s1di = new InterruptIn(P0_13); |
koyo_take | 1:195da8230785 | 215 | if (f) |
koyo_take | 1:195da8230785 | 216 | grv_p1s1di->rise(f); |
koyo_take | 1:195da8230785 | 217 | |
koyo_take | 1:195da8230785 | 218 | if (grv_p1s2do) { |
koyo_take | 1:195da8230785 | 219 | delete grv_p1s2do; |
koyo_take | 1:195da8230785 | 220 | grv_p1s2do = NULL; |
koyo_take | 1:195da8230785 | 221 | } |
koyo_take | 1:195da8230785 | 222 | grv_p1s2di = new InterruptIn(P0_12); |
koyo_take | 1:195da8230785 | 223 | grv_p1s2di->rise(f); |
koyo_take | 1:195da8230785 | 224 | } |
koyo_take | 1:195da8230785 | 225 | break; |
koyo_take | 1:195da8230785 | 226 | case GROVE_CH2: |
koyo_take | 1:195da8230785 | 227 | if(dir == GROVE_DIO_OUT) { |
koyo_take | 1:195da8230785 | 228 | if (grv_p2s1di) { |
koyo_take | 1:195da8230785 | 229 | delete grv_p2s1di; |
koyo_take | 1:195da8230785 | 230 | grv_p2s1di = NULL; |
koyo_take | 1:195da8230785 | 231 | } |
koyo_take | 1:195da8230785 | 232 | grv_p2s1do = new DigitalOut(P0_15); |
koyo_take | 1:195da8230785 | 233 | |
koyo_take | 1:195da8230785 | 234 | if (grv_p2s2di) { |
koyo_take | 1:195da8230785 | 235 | delete grv_p2s2di; |
koyo_take | 1:195da8230785 | 236 | grv_p2s2di = NULL; |
koyo_take | 1:195da8230785 | 237 | } |
koyo_take | 1:195da8230785 | 238 | grv_p2s2do = new DigitalOut(P0_14); |
koyo_take | 1:195da8230785 | 239 | } else { /* GROVE_DIO_IN */ |
koyo_take | 1:195da8230785 | 240 | if (grv_p2s1do) { |
koyo_take | 1:195da8230785 | 241 | delete grv_p2s1do; |
koyo_take | 1:195da8230785 | 242 | grv_p2s1do = NULL; |
koyo_take | 1:195da8230785 | 243 | } |
koyo_take | 1:195da8230785 | 244 | grv_p2s1di = new InterruptIn(P0_15); |
koyo_take | 1:195da8230785 | 245 | if (f) |
koyo_take | 1:195da8230785 | 246 | grv_p2s1di->rise(f); |
koyo_take | 1:195da8230785 | 247 | |
koyo_take | 1:195da8230785 | 248 | if (grv_p2s2do) { |
koyo_take | 1:195da8230785 | 249 | delete grv_p2s2do; |
koyo_take | 1:195da8230785 | 250 | grv_p2s2do = NULL; |
koyo_take | 1:195da8230785 | 251 | } |
koyo_take | 1:195da8230785 | 252 | grv_p2s2di = new InterruptIn(P0_14); |
koyo_take | 1:195da8230785 | 253 | grv_p2s2di->rise(f); |
koyo_take | 1:195da8230785 | 254 | } |
koyo_take | 1:195da8230785 | 255 | break; |
koyo_take | 1:195da8230785 | 256 | default: |
koyo_take | 1:195da8230785 | 257 | /* do nothing */ |
koyo_take | 1:195da8230785 | 258 | break; |
koyo_take | 1:195da8230785 | 259 | } |
koyo_take | 1:195da8230785 | 260 | |
koyo_take | 1:195da8230785 | 261 | return 0; |
koyo_take | 1:195da8230785 | 262 | } |
koyo_take | 1:195da8230785 | 263 | |
koyo_take | 1:195da8230785 | 264 | int Eeabp1::setGroveDio(EeabGrovePort port, EeabGroveDio val) |
koyo_take | 1:195da8230785 | 265 | { |
koyo_take | 1:195da8230785 | 266 | switch (port) { |
koyo_take | 1:195da8230785 | 267 | case GROVE_CH1: |
koyo_take | 1:195da8230785 | 268 | *grv_p1s1do = val; |
koyo_take | 1:195da8230785 | 269 | *grv_p1s2do = val; |
koyo_take | 1:195da8230785 | 270 | break; |
koyo_take | 1:195da8230785 | 271 | case GROVE_CH2: |
koyo_take | 1:195da8230785 | 272 | *grv_p2s1do = val; |
koyo_take | 1:195da8230785 | 273 | *grv_p2s2do = val; |
koyo_take | 1:195da8230785 | 274 | break; |
koyo_take | 1:195da8230785 | 275 | default: |
koyo_take | 1:195da8230785 | 276 | break; |
koyo_take | 1:195da8230785 | 277 | } |
koyo_take | 1:195da8230785 | 278 | |
koyo_take | 1:195da8230785 | 279 | return 0; |
koyo_take | 1:195da8230785 | 280 | } |
koyo_take | 1:195da8230785 | 281 | |
koyo_take | 1:195da8230785 | 282 | int Eeabp1::getGroveDio(EeabGrovePort port, EeabGroveSig sig) |
koyo_take | 1:195da8230785 | 283 | { |
koyo_take | 1:195da8230785 | 284 | switch (port) { |
koyo_take | 1:195da8230785 | 285 | case GROVE_CH1: |
koyo_take | 1:195da8230785 | 286 | if (grv_p1s1di == NULL) |
koyo_take | 1:195da8230785 | 287 | return -1; |
koyo_take | 1:195da8230785 | 288 | return (sig == GROVE_SIG1) ? |
koyo_take | 1:195da8230785 | 289 | grv_p1s1di->read() : grv_p1s2di->read(); |
koyo_take | 1:195da8230785 | 290 | case GROVE_CH2: |
koyo_take | 1:195da8230785 | 291 | if (grv_p2s1di == NULL) |
koyo_take | 1:195da8230785 | 292 | return -1; |
koyo_take | 1:195da8230785 | 293 | return (sig == GROVE_SIG1) ? |
koyo_take | 1:195da8230785 | 294 | grv_p2s1di->read() : grv_p2s2di->read(); |
koyo_take | 1:195da8230785 | 295 | default: |
koyo_take | 1:195da8230785 | 296 | break; |
koyo_take | 1:195da8230785 | 297 | } |
koyo_take | 1:195da8230785 | 298 | |
koyo_take | 1:195da8230785 | 299 | return -1; |
koyo_take | 1:195da8230785 | 300 | } |
koyo_take | 1:195da8230785 | 301 | |
koyo_take | 1:195da8230785 | 302 | float Eeabp1::getGroveAnalog(EeabGrovePort port, EeabGroveSig sig) |
koyo_take | 1:195da8230785 | 303 | { |
koyo_take | 1:195da8230785 | 304 | switch (port) { |
koyo_take | 1:195da8230785 | 305 | case GROVE_CH1: |
koyo_take | 1:195da8230785 | 306 | return (sig == GROVE_SIG1) ? |
koyo_take | 1:195da8230785 | 307 | grv_p1s1a.read() : grv_p1s2a.read(); |
koyo_take | 1:195da8230785 | 308 | case GROVE_CH2: |
koyo_take | 1:195da8230785 | 309 | return (sig == GROVE_SIG1) ? |
koyo_take | 1:195da8230785 | 310 | grv_p2s1a.read() : grv_p2s2a.read(); |
koyo_take | 1:195da8230785 | 311 | default: |
koyo_take | 1:195da8230785 | 312 | break; |
koyo_take | 1:195da8230785 | 313 | } |
koyo_take | 1:195da8230785 | 314 | |
koyo_take | 1:195da8230785 | 315 | return -1; |
koyo_take | 1:195da8230785 | 316 | } |
koyo_take | 1:195da8230785 | 317 | |
koyo_take | 1:195da8230785 | 318 | int Eeabp1::enableTempHumidSensor(void) |
koyo_take | 1:195da8230785 | 319 | { |
koyo_take | 1:195da8230785 | 320 | temp_humid_sensor = new Sht31(P0_30, P0_7); |
koyo_take | 1:195da8230785 | 321 | return 0; |
koyo_take | 1:195da8230785 | 322 | } |
koyo_take | 1:195da8230785 | 323 | |
koyo_take | 1:195da8230785 | 324 | float Eeabp1::getTemp(void) |
koyo_take | 1:195da8230785 | 325 | { |
koyo_take | 1:195da8230785 | 326 | if (!temp_humid_sensor) |
koyo_take | 1:195da8230785 | 327 | return -1; |
koyo_take | 1:195da8230785 | 328 | |
koyo_take | 1:195da8230785 | 329 | return temp_humid_sensor->readTemperature(); |
koyo_take | 1:195da8230785 | 330 | } |
koyo_take | 1:195da8230785 | 331 | |
koyo_take | 1:195da8230785 | 332 | float Eeabp1::getHumid(void) |
koyo_take | 1:195da8230785 | 333 | { |
koyo_take | 1:195da8230785 | 334 | if (!temp_humid_sensor) |
koyo_take | 1:195da8230785 | 335 | return -1; |
koyo_take | 1:195da8230785 | 336 | |
koyo_take | 1:195da8230785 | 337 | return temp_humid_sensor->readHumidity(); |
koyo_take | 1:195da8230785 | 338 | } |
koyo_take | 1:195da8230785 | 339 | |
koyo_take | 1:195da8230785 | 340 | int Eeabp1::enableAccelerometer(void) |
koyo_take | 1:195da8230785 | 341 | { |
koyo_take | 1:195da8230785 | 342 | accelerometer = new ADXL345_I2C(P0_30, P0_7); |
koyo_take | 1:195da8230785 | 343 | |
koyo_take | 1:195da8230785 | 344 | this->debug("Device ID is: 0x%02x\n", accelerometer->getDeviceID()); |
koyo_take | 1:195da8230785 | 345 | wait(.001); |
koyo_take | 1:195da8230785 | 346 | |
koyo_take | 1:195da8230785 | 347 | // These are here to test whether any of the initialization fails. It will print the failure |
koyo_take | 1:195da8230785 | 348 | if (accelerometer->setPowerControl(0x00)) { |
koyo_take | 1:195da8230785 | 349 | this->debug("didn't intitialize power control\n"); |
koyo_take | 1:195da8230785 | 350 | return -1; |
koyo_take | 1:195da8230785 | 351 | } |
koyo_take | 1:195da8230785 | 352 | wait(.001); |
koyo_take | 1:195da8230785 | 353 | |
koyo_take | 1:195da8230785 | 354 | //Full resolution, +/-16g, 4mg/LSB. |
koyo_take | 1:195da8230785 | 355 | if(accelerometer->setDataFormatControl(0x0B)) { |
koyo_take | 1:195da8230785 | 356 | this->debug("didn't set data format\n"); |
koyo_take | 1:195da8230785 | 357 | return -1; |
koyo_take | 1:195da8230785 | 358 | } |
koyo_take | 1:195da8230785 | 359 | wait(.001); |
koyo_take | 1:195da8230785 | 360 | |
koyo_take | 1:195da8230785 | 361 | //3.2kHz data rate. |
koyo_take | 1:195da8230785 | 362 | if(accelerometer->setDataRate(ADXL345_3200HZ)) { |
koyo_take | 1:195da8230785 | 363 | this->debug("didn't set data rate\n"); |
koyo_take | 1:195da8230785 | 364 | return -1; |
koyo_take | 1:195da8230785 | 365 | } |
koyo_take | 1:195da8230785 | 366 | wait(.001); |
koyo_take | 1:195da8230785 | 367 | |
koyo_take | 1:195da8230785 | 368 | //Measurement mode. |
koyo_take | 1:195da8230785 | 369 | if(accelerometer->setPowerControl(MeasurementMode)) { |
koyo_take | 1:195da8230785 | 370 | this->debug("didn't set the power control to measurement\n"); |
koyo_take | 1:195da8230785 | 371 | return -1; |
koyo_take | 1:195da8230785 | 372 | } |
koyo_take | 1:195da8230785 | 373 | |
koyo_take | 1:195da8230785 | 374 | return 0; |
koyo_take | 1:195da8230785 | 375 | } |
koyo_take | 1:195da8230785 | 376 | |
koyo_take | 1:195da8230785 | 377 | int Eeabp1::getAcc(int *x, int *y, int *z) |
koyo_take | 1:195da8230785 | 378 | { |
koyo_take | 1:195da8230785 | 379 | int readings[3] = {0, 0, 0}; |
koyo_take | 1:195da8230785 | 380 | accelerometer->getOutput(readings); |
koyo_take | 1:195da8230785 | 381 | *x = readings[0]; |
koyo_take | 1:195da8230785 | 382 | *y = readings[1]; |
koyo_take | 1:195da8230785 | 383 | *z = readings[2]; |
koyo_take | 1:195da8230785 | 384 | |
koyo_take | 1:195da8230785 | 385 | return 0; |
koyo_take | 1:195da8230785 | 386 | } |
koyo_take | 1:195da8230785 | 387 | |
koyo_take | 0:97a57be77fbb | 388 | /* private functions */ |
koyo_take | 0:97a57be77fbb | 389 | void Eeabp1::flushSerial() |
koyo_take | 0:97a57be77fbb | 390 | { |
koyo_take | 0:97a57be77fbb | 391 | while(serial->readable() == true) { |
koyo_take | 0:97a57be77fbb | 392 | serial->getc(); |
koyo_take | 0:97a57be77fbb | 393 | wait_us(1000); |
koyo_take | 0:97a57be77fbb | 394 | } |
koyo_take | 0:97a57be77fbb | 395 | } |
koyo_take | 0:97a57be77fbb | 396 | |
koyo_take | 0:97a57be77fbb | 397 | int Eeabp1::chkSerialChar(const char ch,uint16_t timeout_ms) |
koyo_take | 0:97a57be77fbb | 398 | { |
koyo_take | 0:97a57be77fbb | 399 | uint32_t timeoutCount = 0; |
koyo_take | 0:97a57be77fbb | 400 | |
koyo_take | 0:97a57be77fbb | 401 | while(serial->readable() == false) { |
koyo_take | 0:97a57be77fbb | 402 | wait_us(50); |
koyo_take | 0:97a57be77fbb | 403 | timeoutCount++; |
koyo_take | 0:97a57be77fbb | 404 | if((timeoutCount / 20) >= timeout_ms) |
koyo_take | 0:97a57be77fbb | 405 | return -2; |
koyo_take | 0:97a57be77fbb | 406 | } |
koyo_take | 0:97a57be77fbb | 407 | |
koyo_take | 0:97a57be77fbb | 408 | return (serial->getc() == ch) ? 0 : -1; |
koyo_take | 0:97a57be77fbb | 409 | } |
koyo_take | 0:97a57be77fbb | 410 | |
koyo_take | 0:97a57be77fbb | 411 | int Eeabp1::waitSerialChar(const char ch,uint16_t timeout_ms) |
koyo_take | 0:97a57be77fbb | 412 | { |
koyo_take | 0:97a57be77fbb | 413 | uint32_t timeoutCount = 0; |
koyo_take | 0:97a57be77fbb | 414 | |
koyo_take | 0:97a57be77fbb | 415 | do { |
koyo_take | 0:97a57be77fbb | 416 | while(serial->readable() == false) { |
koyo_take | 0:97a57be77fbb | 417 | wait_us(50); |
koyo_take | 0:97a57be77fbb | 418 | timeoutCount++; |
koyo_take | 0:97a57be77fbb | 419 | if((timeoutCount / 20) >= timeout_ms) |
koyo_take | 0:97a57be77fbb | 420 | return -2; |
koyo_take | 0:97a57be77fbb | 421 | } |
koyo_take | 0:97a57be77fbb | 422 | } while(serial->getc() != ch); |
koyo_take | 0:97a57be77fbb | 423 | |
koyo_take | 0:97a57be77fbb | 424 | return 0; |
koyo_take | 0:97a57be77fbb | 425 | } |
koyo_take | 0:97a57be77fbb | 426 | |
koyo_take | 0:97a57be77fbb | 427 | int Eeabp1::chkSerialCharOk() |
koyo_take | 0:97a57be77fbb | 428 | { |
koyo_take | 0:97a57be77fbb | 429 | int ret; |
koyo_take | 0:97a57be77fbb | 430 | ret = waitSerialChar('>',4000); |
koyo_take | 0:97a57be77fbb | 431 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 432 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 433 | |
koyo_take | 0:97a57be77fbb | 434 | ret = chkSerialChar('>',2000); |
koyo_take | 0:97a57be77fbb | 435 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 436 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 437 | |
koyo_take | 0:97a57be77fbb | 438 | ret = chkSerialChar(' ',2000); |
koyo_take | 0:97a57be77fbb | 439 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 440 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 441 | |
koyo_take | 0:97a57be77fbb | 442 | ret = chkSerialChar('O',2000); |
koyo_take | 0:97a57be77fbb | 443 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 444 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 445 | |
koyo_take | 0:97a57be77fbb | 446 | ret = chkSerialChar('k',2000); |
koyo_take | 0:97a57be77fbb | 447 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 448 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 449 | |
koyo_take | 0:97a57be77fbb | 450 | ret = waitSerialChar('>',4000); |
koyo_take | 0:97a57be77fbb | 451 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 452 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 453 | |
koyo_take | 0:97a57be77fbb | 454 | wait_us(1000); |
koyo_take | 0:97a57be77fbb | 455 | |
koyo_take | 0:97a57be77fbb | 456 | return 0; |
koyo_take | 0:97a57be77fbb | 457 | } |
koyo_take | 0:97a57be77fbb | 458 | |
koyo_take | 0:97a57be77fbb | 459 | int Eeabp1::chkSerialCharRes(char chkchr) |
koyo_take | 0:97a57be77fbb | 460 | { |
koyo_take | 0:97a57be77fbb | 461 | int ret; |
koyo_take | 0:97a57be77fbb | 462 | ret = waitSerialChar('>', 10000); |
koyo_take | 0:97a57be77fbb | 463 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 464 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 465 | |
koyo_take | 0:97a57be77fbb | 466 | ret = chkSerialChar('>',2000); |
koyo_take | 0:97a57be77fbb | 467 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 468 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 469 | |
koyo_take | 0:97a57be77fbb | 470 | ret = chkSerialChar(' ',2000); |
koyo_take | 0:97a57be77fbb | 471 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 472 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 473 | |
koyo_take | 0:97a57be77fbb | 474 | ret = chkSerialChar('O',2000); |
koyo_take | 0:97a57be77fbb | 475 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 476 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 477 | |
koyo_take | 0:97a57be77fbb | 478 | ret = chkSerialChar('k',2000); |
koyo_take | 0:97a57be77fbb | 479 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 480 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 481 | |
koyo_take | 0:97a57be77fbb | 482 | ret = waitSerialChar('>',16000); |
koyo_take | 0:97a57be77fbb | 483 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 484 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 485 | |
koyo_take | 0:97a57be77fbb | 486 | ret = chkSerialChar('>',2000); |
koyo_take | 0:97a57be77fbb | 487 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 488 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 489 | |
koyo_take | 0:97a57be77fbb | 490 | ret = chkSerialChar(' ',2000); |
koyo_take | 0:97a57be77fbb | 491 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 492 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 493 | |
koyo_take | 1:195da8230785 | 494 | ret = waitSerialChar(chkchr,4000); |
koyo_take | 0:97a57be77fbb | 495 | if (ret != 0) |
koyo_take | 1:195da8230785 | 496 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 497 | |
koyo_take | 0:97a57be77fbb | 498 | ret = waitSerialChar('>',4000); |
koyo_take | 0:97a57be77fbb | 499 | if (ret != 0) |
koyo_take | 0:97a57be77fbb | 500 | return __LINE__; |
koyo_take | 0:97a57be77fbb | 501 | |
koyo_take | 0:97a57be77fbb | 502 | wait_us(1000); |
koyo_take | 0:97a57be77fbb | 503 | |
koyo_take | 0:97a57be77fbb | 504 | return 0; |
koyo_take | 0:97a57be77fbb | 505 | } |