From 27b81048bb08c3920bdcfdf53c13939e3fcf545b Mon Sep 17 00:00:00 2001 From: ajk1e20 <ajk1e20@soton.ac.uk> Date: Thu, 12 May 2022 20:17:06 +0000 Subject: [PATCH] Upload New File --- compass.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 compass.c diff --git a/compass.c b/compass.c new file mode 100644 index 0000000..38ac0e5 --- /dev/null +++ b/compass.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include "pico/stdlib.h" +#include "lib/fonts.h" +#include "lib/st7735.h" +#include "lib/ICM20948.h" + +// Based off the helpful guide from plaaosert (ct2g20) + +void SetupIMU() { + // Apparently this is important ¯\_(ツ)_/¯ + i2c_init(i2c0, 400 * 1000); + gpio_set_function(4, GPIO_FUNC_I2C); + gpio_set_function(5, GPIO_FUNC_I2C); + gpio_pull_up(4); + gpio_pull_up(5); + sleep_ms(1000); + + IMU_EN_SENSOR_TYPE enMotionSensorType; + + imuInit(&enMotionSensorType); + if (IMU_EN_SENSOR_TYPE_ICM20948 != enMotionSensorType) { + printf("Failed to initialise IMU..."); + } + + printf("IMU initialised!"); +} + +int main() { + stdio_init_all(); // Initialise serial in/output + setvbuf ( stdout , NULL , _IONBF , 0 ); // Disable line and block buffering on stdout (for serial comm.) + sleep_ms(1000); + + ST7735_Init(); + ST7735_FillScreen(ST7735_BLACK); + + SetupIMU(); + + // TODO: initialize variables + float tmp_x = 0.0f; + float tmp_y = 0.0f; + float tmp_z = 0.0f; + char xString[5]; + char yString[5]; + char zString[5]; + + while (true) { + // TODO: read from IMU + icm20948MagRead(&tmp_x, &tmp_y, &tmp_z); + + sprintf(xString, "%.1f", tmp_x ); + sprintf(yString, "%.1f", tmp_y ); + sprintf(zString, "%.1f", tmp_z ); + + // TODO: draw something to the LCD + ST7735_WriteString(5,20, xString, Font_16x26, ST7735_WHITE, ST7735_BLACK); + ST7735_WriteString(5,45, yString, Font_16x26, ST7735_WHITE, ST7735_BLACK); + ST7735_WriteString(5,65, zString, Font_16x26, ST7735_WHITE, ST7735_BLACK); + sleep_ms(100); + } + + +} \ No newline at end of file -- GitLab