From 4a119100e7693f51eee333f0f90639d0b5ae674c Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 00:07:55 -0700 Subject: [PATCH] updated with more enums and start of the update code --- src/gy521_driver/gy521_driver.c | 55 ++++++++++++++++++++++++++++++--- src/gy521_driver/gy521_driver.h | 4 +-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/gy521_driver/gy521_driver.c b/src/gy521_driver/gy521_driver.c index e705c25..8018cf2 100644 --- a/src/gy521_driver/gy521_driver.c +++ b/src/gy521_driver/gy521_driver.c @@ -18,6 +18,8 @@ extern void _test_free(void* const ptr, const char* file, const int line); #define free(ptr) _test_free(ptr, __FILE__, __LINE__) #endif // UNIT_TESTING +#define NUM_ACCEL_REGS 6 + /* * ############################ * REGISTER MAP @@ -25,6 +27,20 @@ extern void _test_free(void* const ptr, const char* file, const int line); */ enum gy521_map { + accel_xouth = 0x3B, + accel_xoutl, + accel_youth, + accel_youtl, + accel_zouth, + accel_zoutl, + temp_outh, + temp_outl, + gyro_xouth, + gyro_xoutl, + gyro_youth, + gyro_youtl, + gyro_zouth, + gyro_zoutl, fifo_couth = 0x72, fifo_contl, fifo_r_w, @@ -55,6 +71,23 @@ struct gy521_module{ accel_values_struct accel; }; +/* + * ############################ + * Helper/Private Functions + * ############################ + */ + +uint8_t read_register(enum gy521_map reg) +{ + return 0; +} + +uint8_t write_register(enum gy521_map reg) +{ + return 0; +} + + /* * ############################ * Function Definitions @@ -75,6 +108,7 @@ void gy521_free(gy521_module *m) free(m); } + _Bool gy521_init(gy521_module *m, uint8_t slave_address) { if((slave_address != TWI_GY521_ADDR1) && (slave_address != TWI_GY521_ADDR2)) { m->slave_address = 0x0; @@ -97,12 +131,25 @@ _Bool gy521_init(gy521_module *m, uint8_t slave_address) { } -_Bool gy521_update_gyro(gy521_module *m) +void gy521_update_gyro(gy521_module *m) { - return 0; + } -_Bool gy521_update_accel(gy521_module *m) +void gy521_update_accel(gy521_module *m) { - return 0; + uint8_t read_regs[NUM_ACCEL_REGS] = {accel_xouth, accel_xoutl, + accel_youth, accel_youth, + accel_zouth, accel_zoutl}; + + /*update individually the structure by communicating with the device.*/ + gy521_twi_rx(m->slave_address, read_regs, NUM_ACCEL_REGS); + + /*bitshift to reassembly the high/low regs into a single 16bit*/ + m->accel.x = (read_regs[0] << 8) | read_regs[1]; + m->accel.y = (read_regs[2] << 8) | read_regs[3]; + m->accel.z = (read_regs[4] << 8) | read_regs[5]; + } + + diff --git a/src/gy521_driver/gy521_driver.h b/src/gy521_driver/gy521_driver.h index 64b0d1c..b27bf84 100644 --- a/src/gy521_driver/gy521_driver.h +++ b/src/gy521_driver/gy521_driver.h @@ -42,8 +42,8 @@ extern void (*gy521_twi_rx)(uint8_t, uint8_t*, uint8_t); struct gy521_module* gy521_new(void); _Bool gy521_init(struct gy521_module *m, uint8_t slave_address); -_Bool gy521_update_gyro(struct gy521_module* m); -_Bool gy521_update_accel(struct gy521_module* m); +void gy521_update_gyro(struct gy521_module* m); +void gy521_update_accel(struct gy521_module* m); void gy521_free(struct gy521_module *m);