Smartage application
Dependencies: BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2
Fork of STM32L0_LoRa by
Diff: main.cpp
- Revision:
- 24:bb733d746bda
- Parent:
- 22:2c1359292de1
- Child:
- 25:18a57be7bb17
--- a/main.cpp Wed May 23 13:24:29 2018 +0000 +++ b/main.cpp Fri May 25 15:13:25 2018 +0000 @@ -9,6 +9,26 @@ DigitalOut myled(LED); //D12 TRIGGER D11 ECHO HCSR04 sensor(D12, D11); +/* Instantiate the expansion board */ +XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); +volatile int mems_event = 0; +volatile int notification = 0; +InterruptIn mybutton(USER_BUTTON); + +/* Sensor initialization */ +HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; +LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; + +/* User button callback. */ +void pressed_cb() { + notification = 0; +} + + +/* Interrupt 1 callback. */ +void int1_cb() { + mems_event = 1; +} int main() { #ifdef HELTEC_STM32L4 @@ -22,21 +42,65 @@ * It waits up to 30 seconds for a USB terminal connections */ InitSerial(30*1000, &myled); + hum_temp->enable(); + + /* Attach callback to LSM6DSL INT1 */ + acc_gyro->attach_int1_irq(&int1_cb); + /* Enable LSM6DSL accelerometer */ + acc_gyro->enable_x(); + /* Enable Tilt Detection. */ + acc_gyro->enable_free_fall_detection(); + + /* Attach callback to User button press */ + mybutton.fall(&pressed_cb); + print_stuff(); - + bool empty = true; + //ADD THE WAIT while(1){ - char message[8];//Message to be sent - get_distance(message); - - SendAndBack((uint8_t*)message); + char distance[4], empty_distance[4], temperature[4];//Message to be sent + get_distance(distance); + get_temperature(temperature); + + + if (mems_event) { + mems_event = 0; + LSM6DSL_Event_Status_t status; + acc_gyro->get_event_status(&status); + if (status.FreeFallStatus) { + notification = 1; + printf("Tilt Detected!\r\n"); + } + } + + if(empty) { + memcpy(empty_distance, distance, 4); + SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); //invia due volte + empty = false; + } + else{ + SendAndBack((uint8_t*)distance, (uint8_t*)empty_distance, (uint8_t*)temperature, notification); + } + } } void get_distance(char message[]){ //Ultrasonic measurement - long distance = sensor.distance(); + long distance = 401; + while (distance >= 400) distance = sensor.distance(); dprintf("distance %d \n",distance); sprintf(message, "%d", distance); +} + +void get_temperature(char message[]){ + float value; + hum_temp->get_temperature(&value); + dprintf("temperature %f", value); + + sprintf(message, "%f", value); + dprintf(message); + } \ No newline at end of file