Smartage application
Dependencies: BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2
Fork of STM32L0_LoRa by
main.cpp@24:bb733d746bda, 2018-05-25 (annotated)
- Committer:
- marcozecchini
- Date:
- Fri May 25 15:13:25 2018 +0000
- Revision:
- 24:bb733d746bda
- Parent:
- 22:2c1359292de1
- Child:
- 25:18a57be7bb17
sensor measuring added
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 | InterruptIn mybutton(USER_BUTTON); |
marcozecchini | 24:bb733d746bda | 17 | |
marcozecchini | 24:bb733d746bda | 18 | /* Sensor initialization */ |
marcozecchini | 24:bb733d746bda | 19 | HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
marcozecchini | 24:bb733d746bda | 20 | LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
marcozecchini | 24:bb733d746bda | 21 | |
marcozecchini | 24:bb733d746bda | 22 | /* User button callback. */ |
marcozecchini | 24:bb733d746bda | 23 | void pressed_cb() { |
marcozecchini | 24:bb733d746bda | 24 | notification = 0; |
marcozecchini | 24:bb733d746bda | 25 | } |
marcozecchini | 24:bb733d746bda | 26 | |
marcozecchini | 24:bb733d746bda | 27 | |
marcozecchini | 24:bb733d746bda | 28 | /* Interrupt 1 callback. */ |
marcozecchini | 24:bb733d746bda | 29 | void int1_cb() { |
marcozecchini | 24:bb733d746bda | 30 | mems_event = 1; |
marcozecchini | 24:bb733d746bda | 31 | } |
Helmut64 | 0:c43b6919ae15 | 32 | |
Helmut64 | 17:98f2528e8399 | 33 | int main() { |
Helmut64 | 17:98f2528e8399 | 34 | #ifdef HELTEC_STM32L4 |
Helmut64 | 17:98f2528e8399 | 35 | DigitalOut vext(POWER_VEXT); |
Helmut64 | 17:98f2528e8399 | 36 | vext = POWER_VEXT_ON; |
Helmut64 | 17:98f2528e8399 | 37 | #endif |
Helmut64 | 17:98f2528e8399 | 38 | /* |
Helmut64 | 17:98f2528e8399 | 39 | * inits the Serial or USBSerial when available (230400 baud). |
Helmut64 | 17:98f2528e8399 | 40 | * If the serial uart is not is not connected it swiches to USB Serial |
Helmut64 | 17:98f2528e8399 | 41 | * blinking LED means USBSerial detected, waiting for a connect. |
Helmut64 | 17:98f2528e8399 | 42 | * It waits up to 30 seconds for a USB terminal connections |
Helmut64 | 17:98f2528e8399 | 43 | */ |
marcozecchini | 19:7763501775e5 | 44 | InitSerial(30*1000, &myled); |
marcozecchini | 24:bb733d746bda | 45 | hum_temp->enable(); |
marcozecchini | 24:bb733d746bda | 46 | |
marcozecchini | 24:bb733d746bda | 47 | /* Attach callback to LSM6DSL INT1 */ |
marcozecchini | 24:bb733d746bda | 48 | acc_gyro->attach_int1_irq(&int1_cb); |
marcozecchini | 24:bb733d746bda | 49 | /* Enable LSM6DSL accelerometer */ |
marcozecchini | 24:bb733d746bda | 50 | acc_gyro->enable_x(); |
marcozecchini | 24:bb733d746bda | 51 | /* Enable Tilt Detection. */ |
marcozecchini | 24:bb733d746bda | 52 | acc_gyro->enable_free_fall_detection(); |
marcozecchini | 24:bb733d746bda | 53 | |
marcozecchini | 24:bb733d746bda | 54 | /* Attach callback to User button press */ |
marcozecchini | 24:bb733d746bda | 55 | mybutton.fall(&pressed_cb); |
marcozecchini | 24:bb733d746bda | 56 | |
marcozecchini | 20:1557c9d9c183 | 57 | print_stuff(); |
marcozecchini | 24:bb733d746bda | 58 | bool empty = true; |
marcozecchini | 24:bb733d746bda | 59 | //ADD THE WAIT |
marcozecchini | 19:7763501775e5 | 60 | while(1){ |
marcozecchini | 19:7763501775e5 | 61 | |
marcozecchini | 24:bb733d746bda | 62 | char distance[4], empty_distance[4], temperature[4];//Message to be sent |
marcozecchini | 24:bb733d746bda | 63 | get_distance(distance); |
marcozecchini | 24:bb733d746bda | 64 | get_temperature(temperature); |
marcozecchini | 24:bb733d746bda | 65 | |
marcozecchini | 24:bb733d746bda | 66 | |
marcozecchini | 24:bb733d746bda | 67 | if (mems_event) { |
marcozecchini | 24:bb733d746bda | 68 | mems_event = 0; |
marcozecchini | 24:bb733d746bda | 69 | LSM6DSL_Event_Status_t status; |
marcozecchini | 24:bb733d746bda | 70 | acc_gyro->get_event_status(&status); |
marcozecchini | 24:bb733d746bda | 71 | if (status.FreeFallStatus) { |
marcozecchini | 24:bb733d746bda | 72 | notification = 1; |
marcozecchini | 24:bb733d746bda | 73 | printf("Tilt Detected!\r\n"); |
marcozecchini | 24:bb733d746bda | 74 | } |
marcozecchini | 24:bb733d746bda | 75 | } |
marcozecchini | 24:bb733d746bda | 76 | |
marcozecchini | 24:bb733d746bda | 77 | if(empty) { |
marcozecchini | 24:bb733d746bda | 78 | memcpy(empty_distance, distance, 4); |
marcozecchini | 24:bb733d746bda | 79 | SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); //invia due volte |
marcozecchini | 24:bb733d746bda | 80 | empty = false; |
marcozecchini | 24:bb733d746bda | 81 | } |
marcozecchini | 24:bb733d746bda | 82 | else{ |
marcozecchini | 24:bb733d746bda | 83 | SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); |
marcozecchini | 24:bb733d746bda | 84 | } |
marcozecchini | 24:bb733d746bda | 85 | |
marcozecchini | 19:7763501775e5 | 86 | } |
marcozecchini | 22:2c1359292de1 | 87 | } |
marcozecchini | 22:2c1359292de1 | 88 | |
marcozecchini | 22:2c1359292de1 | 89 | void get_distance(char message[]){ |
marcozecchini | 22:2c1359292de1 | 90 | //Ultrasonic measurement |
marcozecchini | 24:bb733d746bda | 91 | long distance = 401; |
marcozecchini | 24:bb733d746bda | 92 | while (distance >= 400) distance = sensor.distance(); |
marcozecchini | 22:2c1359292de1 | 93 | dprintf("distance %d \n",distance); |
marcozecchini | 22:2c1359292de1 | 94 | |
marcozecchini | 22:2c1359292de1 | 95 | sprintf(message, "%d", distance); |
marcozecchini | 24:bb733d746bda | 96 | } |
marcozecchini | 24:bb733d746bda | 97 | |
marcozecchini | 24:bb733d746bda | 98 | void get_temperature(char message[]){ |
marcozecchini | 24:bb733d746bda | 99 | float value; |
marcozecchini | 24:bb733d746bda | 100 | hum_temp->get_temperature(&value); |
marcozecchini | 24:bb733d746bda | 101 | dprintf("temperature %f", value); |
marcozecchini | 24:bb733d746bda | 102 | |
marcozecchini | 24:bb733d746bda | 103 | sprintf(message, "%f", value); |
marcozecchini | 24:bb733d746bda | 104 | dprintf(message); |
marcozecchini | 24:bb733d746bda | 105 | |
marcozecchini | 19:7763501775e5 | 106 | } |