]> git.earman.xyz Git - sensor-watch.git/commitdiff
lis2dh driver: add basic interrupt functionality
authorJoey Castillo <joeycastillo@utexas.edu>
Tue, 30 Nov 2021 23:53:43 +0000 (18:53 -0500)
committerJoey Castillo <joeycastillo@utexas.edu>
Wed, 1 Dec 2021 00:33:49 +0000 (19:33 -0500)
apps/Sensor Watch Accelerometer Test/app.c
watch-library/driver/lis2dh.c
watch-library/driver/lis2dh.h

index 9ad0d56fbfa296eeb62aed0154461b469792673d..6a4c9e3a44a872d51d37a8a69c0db4db0f59733c 100644 (file)
 // Also note that this board has its INT1 pin wired to A1, which is not an external
 // wake pin. Future accelerometer boards will wire interrupt pins to A2 and A4.
 
-uint8_t axis = 0;
-
 void cb_light_pressed() {
-    axis = 1;
 }
 
 void cb_mode_pressed() {
-    axis = 2;
 }
 
 void cb_alarm_pressed() {
-    axis = 3;
+}
+
+uint16_t interrupts = 0;
+uint16_t ticks = 0;
+
+void cb_interrupt_1() {
+    interrupts++;
+}
+
+void cb_tick() {
+    if (++ticks == 30) {
+        interrupts = 0;
+        ticks = 0;
+        watch_display_string("IN t   0", 0);
+    }
 }
 
 void app_init() {
@@ -31,6 +41,7 @@ void app_init() {
     gpio_set_pin_level(A0, true);
 
     watch_enable_display();
+    watch_display_string("IN 0   0", 0);
 
     watch_enable_external_interrupts();
     watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
@@ -40,9 +51,15 @@ void app_init() {
     watch_enable_i2c();
 
     lis2dh_begin();
-
-    lis2dh_set_range(LIS2DH_RANGE_2_G);
     lis2dh_set_data_rate(LIS2DH_DATA_RATE_10_HZ);
+    lis2dh_configure_aoi_int1(
+        LIS2DH_INTERRUPT_CONFIGURATION_OR |
+        LIS2DH_INTERRUPT_CONFIGURATION_X_HIGH_ENABLE |
+        LIS2DH_INTERRUPT_CONFIGURATION_Y_HIGH_ENABLE |
+        LIS2DH_INTERRUPT_CONFIGURATION_Z_HIGH_ENABLE, 96, 0);
+
+    watch_register_interrupt_callback(A1, cb_interrupt_1, INTERRUPT_TRIGGER_RISING);
+    watch_rtc_register_tick_callback(cb_tick);
 }
 
 void app_wake_from_backup() {
@@ -58,34 +75,10 @@ void app_wake_from_standby() {
 }
 
 bool app_loop() {
-    if (lis2dh_have_new_data()) {
-        lis2dh_reading reading;
-        lis2dh_acceleration_measurement measurement = lis2dh_get_acceleration_measurement(&reading);
-
-        // printf("%d,%d,%d\n", reading.x, reading.y, reading.z);
-        printf("%f,%f,%f\n", measurement.x, measurement.y, measurement.z);
-
-        char buf[11] = {0};
-        switch (axis) {
-            case 1:
-                sprintf(buf, "AC X%-6d", reading.x);
-                break;
-            case 2:
-                sprintf(buf, "AC Y%-6d", reading.y);
-                break;
-            case 3:
-                sprintf(buf, "AC Z%-6d", reading.z);
-                break;
-            default:
-                sprintf(buf, "    %2d%2d%2d", abs(reading.x >> 9), abs(reading.y >> 9), abs(reading.z >> 9));
-                if (reading.x < 0) buf[0] = '_';
-                if (reading.y < 0) buf[1] = '_';
-                if (reading.z < 0) buf[3] = '_';
-                break;
-        }
-
-        watch_display_string(buf, 0);
-    }
+    char buf[13] = {0};
+
+    sprintf(buf, "IN%2d%4d", ticks, interrupts);
+    watch_display_string(buf, 0);
 
-    return false;
+    return true;
 }
index 2593fc69f8f41a04e5ca773d894fab31f4862ee3..2ebf53fbafe2bafdd44143d0dc369e60c05abf79 100644 (file)
@@ -114,3 +114,14 @@ void lis2dh_set_data_rate(lis2dh_data_rate_t dataRate) {
 lis2dh_data_rate_t lis2dh_get_data_rate() {
     return watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1) >> 4;
 }
+
+void lis2dh_configure_aoi_int1(lis2dh_interrupt_configuration configuration, uint8_t threshold, uint8_t duration) {
+    watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL3, LIS2DH_CTRL3_VAL_I1_AOI1);
+    watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_CFG, configuration);
+    watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_THS, threshold);
+    watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_DUR, duration);
+}
+
+lis2dh_interrupt_state lis2dh_get_int1_state() {
+    return (lis2dh_interrupt_state) watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_SRC);
+}
index 57ea85eeca2265b848070ba86f9e611ba5d4b372..b8162a4c7fade2c024d3da912ff87bc2ed71f0d3 100644 (file)
@@ -61,6 +61,29 @@ typedef enum {
 
 } lis2dh_data_rate_t;
 
+typedef enum {
+  LIS2DH_INTERRUPT_CONFIGURATION_OR            = 0b00000000,
+  LIS2DH_INTERRUPT_CONFIGURATION_AND           = 0b10000000,
+  LIS2DH_INTERRUPT_CONFIGURATION_6D_MOVEMENT   = 0b01000000,
+  LIS2DH_INTERRUPT_CONFIGURATION_6D_POSITION   = 0b11000000, // in 6D mode, these have an alternate meaning:
+  LIS2DH_INTERRUPT_CONFIGURATION_Z_HIGH_ENABLE = 0b00100000, // Z up enable
+  LIS2DH_INTERRUPT_CONFIGURATION_Z_LOW_ENABLE  = 0b00010000, // Z down enable
+  LIS2DH_INTERRUPT_CONFIGURATION_Y_HIGH_ENABLE = 0b00001000, // Y up enable
+  LIS2DH_INTERRUPT_CONFIGURATION_Y_LOW_ENABLE  = 0b00000100, // Y down enable
+  LIS2DH_INTERRUPT_CONFIGURATION_X_HIGH_ENABLE = 0b00000010, // X up enable
+  LIS2DH_INTERRUPT_CONFIGURATION_X_LOW_ENABLE  = 0b00000001, // X down enable
+} lis2dh_interrupt_configuration;
+
+typedef enum {
+  LIS2DH_INTERRUPT_STATE_ACTIVE = 0b01000000,
+  LIS2DH_INTERRUPT_STATE_Z_HIGH = 0b00100000, // Z up
+  LIS2DH_INTERRUPT_STATE_Z_LOW  = 0b00010000, // Z down
+  LIS2DH_INTERRUPT_STATE_Y_HIGH = 0b00001000, // Y up
+  LIS2DH_INTERRUPT_STATE_Y_LOW  = 0b00000100, // Y down
+  LIS2DH_INTERRUPT_STATE_X_HIGH = 0b00000010, // X up
+  LIS2DH_INTERRUPT_STATE_X_LOW  = 0b00000001, // X down
+} lis2dh_interrupt_state;
+
 bool lis2dh_begin();
 
 uint8_t lis2dh_get_device_id();
@@ -79,6 +102,10 @@ void lis2dh_set_data_rate(lis2dh_data_rate_t dataRate);
 
 lis2dh_data_rate_t lis2dh_get_data_rate();
 
+void lis2dh_configure_aoi_int1(lis2dh_interrupt_configuration configuration, uint8_t threshold, uint8_t duration);
+
+lis2dh_interrupt_state lis2dh_get_int1_state();
+
 // Assumes SA0 is high; if low, its 0x18
 #define LIS2DH_ADDRESS (0x19)