adafruit / Adafruit_LIS3DH

Arduino Library for Adafruit LIS3DH breakout board
Other
72 stars 57 forks source link

Add clearInterrupt() function #46

Closed patricklaf closed 2 years ago

patricklaf commented 2 years ago

Add a function to get and clear interrupt source register in case the LIS3DH is used in interrupt mode.

Works OK with an I2C device, attached to an ESP32, configured to detect click:

void sensors_setup() {
    for (uint8_t address = 0x18; address <= 0x19; ++address) {
        if (lis3dh.begin(address)) {
            Serial.print("LIS3DH found at 0x");
            Serial.println(address, HEX);
            break;
        }
    }
    lis3dh.enableDRDY(false);
    lis3dh.setRange(LIS3DH_RANGE_2_G);
    lis3dh.setClick(1, 80);
}

void IRAM_ATTR isr_click() {
    ets_printf("click (LIS3DH) ISR\n");
    worker.click = CLICK_ON;
}

void isr_setup() {
    // Click
    pinMode(GPIO_NUM_33, INPUT);
    attachInterrupt(GPIO_NUM_33, &isr_click, RISING);
}

void task_loop() {
    switch (worker.click) {
    case CLICK_ON:
        uint8_t source = lis3dh.readAndClearInterrupt();
        Serial.print("Interrupt source (0x");
        Serial.print(source, HEX);
        Serial.println(")");
        uint8_t click = lis3dh.getClick();
        Serial.print("Click detected (0x");
        Serial.print(click, HEX);
        Serial.print("): ");
        if (click & 0x30) {
            if (click & 0x10) Serial.print(" single click");
            if (click & 0x20) Serial.print(" double click");
        }
        Serial.println();
        worker.click = CLICK_IDLE;
        break;
    }
}
ladyada commented 2 years ago

looks great, maybe rename to readAndClearInterrupt() since it does both?

patricklaf commented 2 years ago

Edit code example to follow function rename.

ladyada commented 2 years ago

nice!