Smartage application
Dependencies: BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2
Fork of STM32L0_LoRa by
main.cpp@26:d93f1206909c, 2018-05-30 (annotated)
- Committer:
- marcozecchini
- Date:
- Wed May 30 14:07:12 2018 +0000
- Revision:
- 26:d93f1206909c
- Parent:
- 25:18a57be7bb17
- Child:
- 29:04e1489f8fe2
WORKING WITHOUT ENC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 0:c43b6919ae15 | 1 | /* |
Helmut64 | 17:98f2528e8399 | 2 | * Copyright (c) 2018 HELIOS Software GmbH |
Helmut64 | 0:c43b6919ae15 | 3 | * 30826 Garbsen (Hannover) Germany |
Helmut64 | 0:c43b6919ae15 | 4 | * Licensed under the Apache License, Version 2.0); |
Helmut64 | 0:c43b6919ae15 | 5 | */ |
Helmut64 | 0:c43b6919ae15 | 6 | #include "main.h" |
Helmut64 | 0:c43b6919ae15 | 7 | |
Helmut64 | 0:c43b6919ae15 | 8 | |
Helmut64 | 17:98f2528e8399 | 9 | DigitalOut myled(LED); |
marcozecchini | 19:7763501775e5 | 10 | //D12 TRIGGER D11 ECHO |
marcozecchini | 19:7763501775e5 | 11 | HCSR04 sensor(D12, D11); |
marcozecchini | 24:bb733d746bda | 12 | /* Instantiate the expansion board */ |
marcozecchini | 24:bb733d746bda | 13 | XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
marcozecchini | 24:bb733d746bda | 14 | volatile int mems_event = 0; |
marcozecchini | 24:bb733d746bda | 15 | volatile int notification = 0; |
marcozecchini | 24:bb733d746bda | 16 | |
marcozecchini | 24:bb733d746bda | 17 | /* Sensor initialization */ |
marcozecchini | 24:bb733d746bda | 18 | HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
marcozecchini | 24:bb733d746bda | 19 | LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
marcozecchini | 24:bb733d746bda | 20 | |
Helmut64 | 17:98f2528e8399 | 21 | int main() { |
Helmut64 | 17:98f2528e8399 | 22 | #ifdef HELTEC_STM32L4 |
Helmut64 | 17:98f2528e8399 | 23 | DigitalOut vext(POWER_VEXT); |
Helmut64 | 17:98f2528e8399 | 24 | vext = POWER_VEXT_ON; |
Helmut64 | 17:98f2528e8399 | 25 | #endif |
Helmut64 | 17:98f2528e8399 | 26 | /* |
Helmut64 | 17:98f2528e8399 | 27 | * inits the Serial or USBSerial when available (230400 baud). |
Helmut64 | 17:98f2528e8399 | 28 | * If the serial uart is not is not connected it swiches to USB Serial |
Helmut64 | 17:98f2528e8399 | 29 | * blinking LED means USBSerial detected, waiting for a connect. |
Helmut64 | 17:98f2528e8399 | 30 | * It waits up to 30 seconds for a USB terminal connections |
Helmut64 | 17:98f2528e8399 | 31 | */ |
marcozecchini | 19:7763501775e5 | 32 | InitSerial(30*1000, &myled); |
marcozecchini | 24:bb733d746bda | 33 | hum_temp->enable(); |
marcozecchini | 24:bb733d746bda | 34 | |
marcozecchini | 24:bb733d746bda | 35 | /* Enable LSM6DSL accelerometer */ |
marcozecchini | 24:bb733d746bda | 36 | acc_gyro->enable_x(); |
marcozecchini | 25:18a57be7bb17 | 37 | /* Enable 6D Orientation. */ |
marcozecchini | 25:18a57be7bb17 | 38 | acc_gyro->enable_6d_orientation(); |
marcozecchini | 24:bb733d746bda | 39 | |
marcozecchini | 20:1557c9d9c183 | 40 | print_stuff(); |
marcozecchini | 24:bb733d746bda | 41 | bool empty = true; |
marcozecchini | 26:d93f1206909c | 42 | //WAIT |
marcozecchini | 26:d93f1206909c | 43 | wait(7.5f); |
marcozecchini | 19:7763501775e5 | 44 | while(1){ |
marcozecchini | 19:7763501775e5 | 45 | |
marcozecchini | 24:bb733d746bda | 46 | char distance[4], empty_distance[4], temperature[4];//Message to be sent |
marcozecchini | 24:bb733d746bda | 47 | get_distance(distance); |
marcozecchini | 24:bb733d746bda | 48 | get_temperature(temperature); |
marcozecchini | 25:18a57be7bb17 | 49 | notification = send_orientation(); |
marcozecchini | 24:bb733d746bda | 50 | |
marcozecchini | 24:bb733d746bda | 51 | if(empty) { |
marcozecchini | 24:bb733d746bda | 52 | memcpy(empty_distance, distance, 4); |
marcozecchini | 24:bb733d746bda | 53 | SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); //invia due volte |
marcozecchini | 24:bb733d746bda | 54 | empty = false; |
marcozecchini | 24:bb733d746bda | 55 | } |
marcozecchini | 24:bb733d746bda | 56 | else{ |
marcozecchini | 24:bb733d746bda | 57 | SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); |
marcozecchini | 24:bb733d746bda | 58 | } |
marcozecchini | 24:bb733d746bda | 59 | |
marcozecchini | 19:7763501775e5 | 60 | } |
marcozecchini | 22:2c1359292de1 | 61 | } |
marcozecchini | 22:2c1359292de1 | 62 | |
marcozecchini | 22:2c1359292de1 | 63 | void get_distance(char message[]){ |
marcozecchini | 22:2c1359292de1 | 64 | //Ultrasonic measurement |
marcozecchini | 24:bb733d746bda | 65 | long distance = 401; |
marcozecchini | 24:bb733d746bda | 66 | while (distance >= 400) distance = sensor.distance(); |
marcozecchini | 22:2c1359292de1 | 67 | dprintf("distance %d \n",distance); |
marcozecchini | 22:2c1359292de1 | 68 | |
marcozecchini | 22:2c1359292de1 | 69 | sprintf(message, "%d", distance); |
marcozecchini | 24:bb733d746bda | 70 | } |
marcozecchini | 24:bb733d746bda | 71 | |
marcozecchini | 24:bb733d746bda | 72 | void get_temperature(char message[]){ |
marcozecchini | 24:bb733d746bda | 73 | float value; |
marcozecchini | 24:bb733d746bda | 74 | hum_temp->get_temperature(&value); |
marcozecchini | 24:bb733d746bda | 75 | dprintf("temperature %f", value); |
marcozecchini | 24:bb733d746bda | 76 | |
marcozecchini | 24:bb733d746bda | 77 | sprintf(message, "%f", value); |
marcozecchini | 24:bb733d746bda | 78 | dprintf(message); |
marcozecchini | 24:bb733d746bda | 79 | |
marcozecchini | 25:18a57be7bb17 | 80 | } |
marcozecchini | 25:18a57be7bb17 | 81 | |
marcozecchini | 25:18a57be7bb17 | 82 | /* Print the orientation. */ |
marcozecchini | 25:18a57be7bb17 | 83 | bool send_orientation() { |
marcozecchini | 25:18a57be7bb17 | 84 | uint8_t xl = 0; |
marcozecchini | 25:18a57be7bb17 | 85 | uint8_t xh = 0; |
marcozecchini | 25:18a57be7bb17 | 86 | uint8_t yl = 0; |
marcozecchini | 25:18a57be7bb17 | 87 | uint8_t yh = 0; |
marcozecchini | 25:18a57be7bb17 | 88 | uint8_t zl = 0; |
marcozecchini | 25:18a57be7bb17 | 89 | uint8_t zh = 0; |
marcozecchini | 25:18a57be7bb17 | 90 | |
marcozecchini | 25:18a57be7bb17 | 91 | acc_gyro->get_6d_orientation_xl(&xl); |
marcozecchini | 25:18a57be7bb17 | 92 | acc_gyro->get_6d_orientation_xh(&xh); |
marcozecchini | 25:18a57be7bb17 | 93 | acc_gyro->get_6d_orientation_yl(&yl); |
marcozecchini | 25:18a57be7bb17 | 94 | acc_gyro->get_6d_orientation_yh(&yh); |
marcozecchini | 25:18a57be7bb17 | 95 | acc_gyro->get_6d_orientation_zl(&zl); |
marcozecchini | 25:18a57be7bb17 | 96 | acc_gyro->get_6d_orientation_zh(&zh); |
marcozecchini | 25:18a57be7bb17 | 97 | |
marcozecchini | 25:18a57be7bb17 | 98 | |
marcozecchini | 25:18a57be7bb17 | 99 | if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) { |
marcozecchini | 25:18a57be7bb17 | 100 | return false; |
marcozecchini | 25:18a57be7bb17 | 101 | } |
marcozecchini | 25:18a57be7bb17 | 102 | |
marcozecchini | 25:18a57be7bb17 | 103 | else { |
marcozecchini | 25:18a57be7bb17 | 104 | return true; |
marcozecchini | 25:18a57be7bb17 | 105 | } |
marcozecchini | 19:7763501775e5 | 106 | } |