examples: possibly helpful comments

This commit is contained in:
William Clark 2024-01-02 15:35:35 +00:00
parent b86e97662a
commit b0e8428519
10 changed files with 92 additions and 40 deletions

View File

@ -17,11 +17,11 @@ This device supports two different interrupt "output pins," `INT1` and `INT2`. T
### file: single-click.c ### file: single-click.c
Set up single-click detection Set up single-click detection (no latching interrupt possible)
### file: double-click.c ### file: double-click.c
Set up double-click detection Set up double-click detection (no latching interrupt possible)
### file: adc.c ### file: adc.c

View File

@ -22,7 +22,7 @@ int main() {
/* error handling */ /* error handling */
} }
/* reset device because it sometimes corrupts itself */ /* reset device just in case */
if (lis3dh_reset(&lis)) { if (lis3dh_reset(&lis)) {
/* error handling */ /* error handling */
} }

View File

@ -37,7 +37,7 @@ int main() {
/* error handling */ /* error handling */
} }
/* reset device because it sometimes corrupts itself */ /* reset device just in case */
if (lis3dh_reset(&lis)) { if (lis3dh_reset(&lis)) {
/* error handling */ /* error handling */
} }
@ -51,7 +51,8 @@ int main() {
lis.cfg.mode = LIS3DH_MODE_HR; lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G; lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_400_HZ; /* minimum recommended ODR */ lis.cfg.rate = LIS3DH_ODR_400_HZ; /* minimum recommended ODR */
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL;
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL_REF;
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8; lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.filter.click = 1; /* enable filtering for CLICK function */ lis.cfg.filter.click = 1; /* enable filtering for CLICK function */
@ -59,15 +60,14 @@ int main() {
lis.cfg.click.yd = 1; /* enable Y axis double click */ lis.cfg.click.yd = 1; /* enable Y axis double click */
lis.cfg.click.zd = 1; /* enable Z axis double click */ lis.cfg.click.zd = 1; /* enable Z axis double click */
lis.cfg.pin1.click = 1; /* enable click int src through pin1 */ lis.cfg.pin1.click = 1; /* enable CLICK INT through pin1 */
lis.cfg.int1.latch = 1; /* latch interrupt until INT1_SRC is read */
/* 1 LSb = 16 mg @ FS_2G /* 1 LSb = 16 mg @ FS_2G
* so a 0.3g 'shock' is 300/16 = 18.75 * so a 0.3g 'shock' is 300/16 = 18.75
* However, the device can have up to +- 40mg read error, so add 40mg * However, the device can have up to +- 40mg read error, so add 40mg
* 0.34g => 340/16 ~= 21 * 0.34g => 340/16 ~= 21
*/ */
lis.cfg.click_ths = 21; /* pretty sensitive */ lis.cfg.click_ths = 21;
/* Duration time is measured in N/ODR where: /* Duration time is measured in N/ODR where:
* --- N = The content of the intX_dur integer * --- N = The content of the intX_dur integer
@ -90,6 +90,11 @@ int main() {
/* error handling */ /* error handling */
} }
/* read REFERENCE to set filter to current accel field */
if (lis3dh_reference(&lis)) {
/* error handling */
}
/* read CLICK_SRC to clear previous interrupts, if any */ /* read CLICK_SRC to clear previous interrupts, if any */
if (lis3dh_read_click(&lis)) { if (lis3dh_read_click(&lis)) {
/* error handling */ /* error handling */

View File

@ -16,23 +16,35 @@ int main() {
lis.dev.sleep = usleep; lis.dev.sleep = usleep;
lis.dev.deinit = i2c_deinit; lis.dev.deinit = i2c_deinit;
/* initialise LIS3DH struct */
if (lis3dh_init(&lis)) { if (lis3dh_init(&lis)) {
/* error handling */ /* error handling */
} }
/* reset device just in case */
if (lis3dh_reset(&lis)) {
/* error handling */
}
lis.cfg.mode = LIS3DH_MODE_HR; lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_4G; lis.cfg.range = LIS3DH_FS_4G;
lis.cfg.rate = LIS3DH_ODR_100_HZ; lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_NORMAL; lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_NORMAL;
/* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
/* error handling */ /* error handling */
} }
/* poll fifo register until it reports that the watermark level has
been reached, or that it has overwritten old data, whichever
happens first. */
if (lis3dh_poll_fifo(&lis)) { if (lis3dh_poll_fifo(&lis)) {
/* error handling */ /* error handling */
} }
/* read as many [x y z] sets as specified by watermark level (fth) */
/* copy them to the fifo data struct given below as `fifo' */
if (lis3dh_read_fifo(&lis, &data)) { if (lis3dh_read_fifo(&lis, &data)) {
/* error handling */ /* error handling */
} }
@ -42,6 +54,7 @@ int main() {
printf("x: %f, y: %f, z: %f\n", data.x[i], data.y[i], data.z[i]); printf("x: %f, y: %f, z: %f\n", data.x[i], data.y[i], data.z[i]);
} }
/* deinitialise struct */
if (lis3dh_deinit(&lis)) { if (lis3dh_deinit(&lis)) {
/* error handling */ /* error handling */
} }

View File

@ -25,7 +25,7 @@ int main() {
/* error handling */ /* error handling */
} }
/* reset device because it sometimes corrupts itself */ /* reset device just in case */
if (lis3dh_reset(&lis)) { if (lis3dh_reset(&lis)) {
/* error handling */ /* error handling */
} }

View File

@ -22,11 +22,12 @@ int main() {
lis.dev.sleep = usleep; lis.dev.sleep = usleep;
lis.dev.deinit = i2c_deinit; lis.dev.deinit = i2c_deinit;
/* initalise LIS3DH struct */
if (lis3dh_init(&lis)) { if (lis3dh_init(&lis)) {
/* error handling */ /* error handling */
} }
/* device sometimes corrupts itself, so reset .. */ /* reset device just in case */
if (lis3dh_reset(&lis)) { if (lis3dh_reset(&lis)) {
/* error handling */ /* error handling */
} }
@ -40,25 +41,35 @@ int main() {
lis.cfg.range = LIS3DH_FS_2G; lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_100_HZ; lis.cfg.rate = LIS3DH_ODR_100_HZ;
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_STREAM; lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_STREAM;
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1; /* trigger into INT1 */ lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1; /* trigger interrupt into int pin1 */
lis.cfg.pin1.wtm = 1; /* trigger upon watermark level reached */ lis.cfg.pin1.wtm = 1; /* trigger upon FIFO watermark level reached */
/* set up HP filter to remove DC component */ /* set up HP filter to remove DC component */
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL; lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL_REF;
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_4; lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_4;
/* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
/* error handling */ /* error handling */
} }
/* read REFERENCE to set filter to current accel field */
if (lis3dh_reference(&lis)) {
/* error handling */
}
/* wait for interrupt from LIS3DH */ /* wait for interrupt from LIS3DH */
if (int_poll(GPIO_INTERRUPT_PIN)) { if (int_poll(GPIO_INTERRUPT_PIN)) {
/* error handling */ /* error handling */
} }
/* read as many [x y z] sets as specified by watermark level (fth) */
/* copy them to the fifo data struct given below as `fifo' */
if (lis3dh_read_fifo(&lis, &fifo)) { if (lis3dh_read_fifo(&lis, &fifo)) {
/* error handling */ /* error handling */
} }
/* above function also writes out the qty of [x y z] sets stored in `fifo' */
for(k=0; k<fifo.size; k++) { for(k=0; k<fifo.size; k++) {
printf("x: %04.04f, y: %04.04f z: %04.04f\n", fifo.x[k], fifo.y[k], fifo.z[k]); printf("x: %04.04f, y: %04.04f z: %04.04f\n", fifo.x[k], fifo.y[k], fifo.z[k]);
} }
@ -68,6 +79,7 @@ int main() {
/* error handling */ /* error handling */
} }
/* deinitalise struct */
if (lis3dh_deinit(&lis)) { if (lis3dh_deinit(&lis)) {
/* error handling */ /* error handling */
} }

View File

@ -14,11 +14,12 @@ int main() {
lis.dev.sleep = usleep; lis.dev.sleep = usleep;
lis.dev.deinit = i2c_deinit; lis.dev.deinit = i2c_deinit;
/* initialise LIS3DH struct */
if (lis3dh_init(&lis)) { if (lis3dh_init(&lis)) {
/* error handling */ /* error handling */
} }
/* device sometimes corrupts itself, so reset .. */ /* reset device just in case */
if (lis3dh_reset(&lis)) { if (lis3dh_reset(&lis)) {
/* error handling */ /* error handling */
} }
@ -27,20 +28,24 @@ int main() {
lis.cfg.range = LIS3DH_FS_4G; lis.cfg.range = LIS3DH_FS_4G;
lis.cfg.rate = LIS3DH_ODR_100_HZ; lis.cfg.rate = LIS3DH_ODR_100_HZ;
/* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
/* error handling */ /* error handling */
} }
/* poll STATUS_REG for new [x y z] data ready */
if (lis3dh_poll(&lis)) { if (lis3dh_poll(&lis)) {
/* error handling */ /* error handling */
} }
/* read latest [x y z] data, store in the `lis' struct's `acc' field */
if (lis3dh_read(&lis)) { if (lis3dh_read(&lis)) {
/* error handling */ /* error handling */
} }
printf("x: %f, y: %f, z: %f\n", lis.acc.x, lis.acc.y, lis.acc.z); printf("x: %f, y: %f, z: %f\n", lis.acc.x, lis.acc.y, lis.acc.z);
/* deinitalise struct */
if (lis3dh_deinit(&lis)) { if (lis3dh_deinit(&lis)) {
/* error handling */ /* error handling */
} }

View File

@ -25,7 +25,7 @@ int main() {
/* error handling */ /* error handling */
} }
/* reset device because it sometimes corrupts itself */ /* reset device just in case */
if (lis3dh_reset(&lis)) { if (lis3dh_reset(&lis)) {
/* error handling */ /* error handling */
} }
@ -39,23 +39,24 @@ int main() {
lis.cfg.mode = LIS3DH_MODE_HR; lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G; lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_400_HZ; /* minimum recommended ODR */ lis.cfg.rate = LIS3DH_ODR_400_HZ; /* minimum recommended ODR */
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL; lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL_REF;
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8; lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.filter.click = 1; /* enable filtering for CLICK function */ lis.cfg.filter.click = 1; /* enable filtering for CLICK function */
lis.cfg.click.xs = 1; /* enable X axis single click */ lis.cfg.click.xs = 1; /* enable X axis single click */
lis.cfg.click.ys = 1; /* enable Y axis single click */ lis.cfg.click.ys = 1; /* enable Y axis single click */
lis.cfg.click.zs = 1; /* enable Z axis single click */ lis.cfg.click.zs = 1; /* enable Z axis single click */
lis.cfg.pin1.click = 1; /* enable click int src through pin1 */
lis.cfg.int1.latch = 1; /* latch interrupt until INT1_SRC is read */ lis.cfg.pin1.click = 1; /* enable CLICK INT through pin1 */
/* 1 LSb = 16 mg @ FS_2G /* 1 LSb = 16 mg @ FS_2G
* so a 0.072g 'shock' is 72/16 = 4.5 * so a 0.3g 'shock' is 300/16 = 18.75
* However, the device can have up to +- 40mg read error * However, the device can have up to +- 40mg read error, add 40 mg
* 0.112g => 112/16 = 7 * 0.34g => 340/16 ~= 21
* (Note: 0.34g and not 1.34g because of HP filter)
*/ */
lis.cfg.click_ths = 7; /* pretty sensitive */ lis.cfg.click_ths = 21;
/* the 'shock' must be gone after 10 ms let's say ..*/
/* /*
* Duration time is measured in N/ODR where: * Duration time is measured in N/ODR where:
* --- N = The content of the intX_dur integer * --- N = The content of the intX_dur integer
@ -63,15 +64,26 @@ int main() {
* [ODR] [1 LSb in milliseconds] * [ODR] [1 LSb in milliseconds]
* 400 2.5 * 400 2.5
* *
* At 400 ODR, 10ms/2.5ms = 4 * At 400 ODR,
* 20 ms = 20/2.5 = 8
*/ */
lis.cfg.time_limit = 4; lis.cfg.time_limit = 8;
/* write device config */ /* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
/* error handling */ /* error handling */
} }
/* read REFERENCE to set filter to current accel field */
if (lis3dh_reference(&lis)) {
/* error handling */
}
/* read CLICK_SRC to clear previous interrupts, if any */
if (lis3dh_read_click(&lis)) {
/* error handling */
}
for(;;) { for(;;) {
/* poll interrupt on INT1 pin */ /* poll interrupt on INT1 pin */
@ -84,8 +96,10 @@ int main() {
/* error handling */ /* error handling */
} }
/* only print if SCLICK=1 */
if (LIS3DH_CLICK_SCLICK(lis.src.click)) {
/* print data gathered from CLICK_SRC */ /* print data gathered from CLICK_SRC */
printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_en=%d, D_en=%d\n", printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_CLICK=%d, D_CLICK=%d\n",
LIS3DH_CLICK_SRC_X(lis.src.click), LIS3DH_CLICK_SRC_X(lis.src.click),
LIS3DH_CLICK_SRC_Y(lis.src.click), LIS3DH_CLICK_SRC_Y(lis.src.click),
LIS3DH_CLICK_SRC_Z(lis.src.click), LIS3DH_CLICK_SRC_Z(lis.src.click),
@ -93,6 +107,7 @@ int main() {
LIS3DH_CLICK_SCLICK(lis.src.click), LIS3DH_CLICK_SCLICK(lis.src.click),
LIS3DH_CLICK_DCLICK(lis.src.click)); LIS3DH_CLICK_DCLICK(lis.src.click));
} }
}
/* unregister interrupt */ /* unregister interrupt */
if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) { if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) {

View File

@ -22,7 +22,7 @@ int main() {
/* error handling */ /* error handling */
} }
/* reset device because it sometimes corrupts itself */ /* reset device just in case */
if (lis3dh_reset(&lis)) { if (lis3dh_reset(&lis)) {
/* error handling */ /* error handling */
} }
@ -34,12 +34,12 @@ int main() {
lis.cfg.en_adc = 1; /* enable ADC */ lis.cfg.en_adc = 1; /* enable ADC */
lis.cfg.en_temp = 1; /* enable temp sensing */ lis.cfg.en_temp = 1; /* enable temp sensing */
/* write device config */ /* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
/* error handling */ /* error handling */
} }
/* Read all 3 ADCs */
if (lis3dh_read_adc(&lis)) { if (lis3dh_read_adc(&lis)) {
/* error handling */ /* error handling */
} }

View File

@ -214,6 +214,7 @@ int lis3dh_configure(lis3dh_t *lis3dh) {
return err; return err;
} }
/* should always return something with valid start configuration */
int lis3dh_poll(lis3dh_t *lis3dh) { int lis3dh_poll(lis3dh_t *lis3dh) {
uint8_t status; uint8_t status;
int err = 0; int err = 0;
@ -226,6 +227,7 @@ int lis3dh_poll(lis3dh_t *lis3dh) {
return err; return err;
} }
/* assume fifo configured */
int lis3dh_poll_fifo(lis3dh_t *lis3dh) { int lis3dh_poll_fifo(lis3dh_t *lis3dh) {
uint8_t src; uint8_t src;
int err = 0; int err = 0;