V1 test

Dependencies:   mbed X_NUCLEO_53L0A1

Committer:
lcouturier
Date:
Thu Nov 29 13:30:11 2018 +0000
Revision:
0:7fa8f6402b51
Child:
1:b594314c402b
V1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lcouturier 0:7fa8f6402b51 1 #include "mbed.h"
lcouturier 0:7fa8f6402b51 2 #include "XNucleo53L0A1.h"
lcouturier 0:7fa8f6402b51 3 #include <stdio.h>
lcouturier 0:7fa8f6402b51 4
lcouturier 0:7fa8f6402b51 5 #define VL53L0_I2C_SDA D14
lcouturier 0:7fa8f6402b51 6 #define VL53L0_I2C_SCL D15
lcouturier 0:7fa8f6402b51 7
lcouturier 0:7fa8f6402b51 8 static XNucleo53L0A1 *board=NULL;
lcouturier 0:7fa8f6402b51 9
lcouturier 0:7fa8f6402b51 10 DigitalOut led1(LED1);
lcouturier 0:7fa8f6402b51 11
lcouturier 0:7fa8f6402b51 12 InterruptIn button1(USER_BUTTON);
lcouturier 0:7fa8f6402b51 13 volatile bool button1_pressed = false; // Used in the main loop
lcouturier 0:7fa8f6402b51 14 volatile bool button1_enabled = true; // Used for debouncing
lcouturier 0:7fa8f6402b51 15 Timeout button1_timeout; // Used for debouncing
lcouturier 0:7fa8f6402b51 16
lcouturier 0:7fa8f6402b51 17 // Enables button when bouncing is over
lcouturier 0:7fa8f6402b51 18 void button1_enabled_cb(void)
lcouturier 0:7fa8f6402b51 19 {
lcouturier 0:7fa8f6402b51 20 button1_enabled = true;
lcouturier 0:7fa8f6402b51 21 }
lcouturier 0:7fa8f6402b51 22
lcouturier 0:7fa8f6402b51 23 // ISR handling button pressed event
lcouturier 0:7fa8f6402b51 24 void button1_onpressed_cb(void)
lcouturier 0:7fa8f6402b51 25 {
lcouturier 0:7fa8f6402b51 26 if (button1_enabled) { // Disabled while the button is bouncing
lcouturier 0:7fa8f6402b51 27 button1_enabled = false;
lcouturier 0:7fa8f6402b51 28 button1_pressed = true; // To be read by the main loop
lcouturier 0:7fa8f6402b51 29 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
lcouturier 0:7fa8f6402b51 30 }
lcouturier 0:7fa8f6402b51 31 }
lcouturier 0:7fa8f6402b51 32
lcouturier 0:7fa8f6402b51 33 int main()
lcouturier 0:7fa8f6402b51 34 {
lcouturier 0:7fa8f6402b51 35
lcouturier 0:7fa8f6402b51 36 //button1.mode(PullUp); // Activate pull-up
lcouturier 0:7fa8f6402b51 37 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
lcouturier 0:7fa8f6402b51 38
lcouturier 0:7fa8f6402b51 39 int idx = 0; // Just for printf below
lcouturier 0:7fa8f6402b51 40 int status;
lcouturier 0:7fa8f6402b51 41 uint32_t distance;
lcouturier 0:7fa8f6402b51 42
lcouturier 0:7fa8f6402b51 43 DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
lcouturier 0:7fa8f6402b51 44
lcouturier 0:7fa8f6402b51 45 /* creates the 53L0A1 expansion board singleton obj */
lcouturier 0:7fa8f6402b51 46 board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
lcouturier 0:7fa8f6402b51 47
lcouturier 0:7fa8f6402b51 48 /* init the 53L0A1 expansion board with default values */
lcouturier 0:7fa8f6402b51 49 status = board->init_board();
lcouturier 0:7fa8f6402b51 50 if (status) {
lcouturier 0:7fa8f6402b51 51 printf("Failed to init board!\r\n");
lcouturier 0:7fa8f6402b51 52 return 0;
lcouturier 0:7fa8f6402b51 53 }
lcouturier 0:7fa8f6402b51 54
lcouturier 0:7fa8f6402b51 55 while (1) {
lcouturier 0:7fa8f6402b51 56 status = board->sensor_centre->get_distance(&distance);
lcouturier 0:7fa8f6402b51 57 if (status == VL53L0X_ERROR_NONE) {
lcouturier 0:7fa8f6402b51 58 printf("Distance : %ld\r\n", distance);
lcouturier 0:7fa8f6402b51 59 }
lcouturier 0:7fa8f6402b51 60 if (button1_pressed) { // Set when button is pressed
lcouturier 0:7fa8f6402b51 61 button1_pressed = false;
lcouturier 0:7fa8f6402b51 62 printf("Button pressed %d\n", idx++);
lcouturier 0:7fa8f6402b51 63 led1 = !led1;
lcouturier 0:7fa8f6402b51 64 }
lcouturier 0:7fa8f6402b51 65 }
lcouturier 0:7fa8f6402b51 66 }