#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "pico/stdlib.h"
//#include "pico/cyw43_arch.h"
#include "hardware/spi.h"
#include "hardware/i2c.h"
#define SERIAL_CON 0
#if SERIAL_CON
#include "hardware/uart.h"
#define UART_ID uart0
#define BAUD_RATE 9600
// Use pins 4 and 5 for UART1
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define UART_TX_PIN 16
#define UART_RX_PIN 17
#endif
// Display Drivers: https://github.com/tvlad1234/pico-displayDrivs
// HX711 : https://github.com/endail/hx711-pico-c/tree/main
#include "ili9341.h"
#include "gfx.h"
#include "common.h"
hx711_config_t hxcfg;
hx711_t hx;
unsigned int pressure = 5001;
// SPI Defines
// We are going to use SPI 0, and allocate it to the following GPIO pins
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define SPI_PORT spi0
#define SPI_MISO_PIN 4
#define SPI_CS_PIN 5
#define SPI_SCK_PIN 2
#define SPI_MOSI_PIN 3
#define RST_PIN 8
#define DC_PIN 7
#define LED_PIN 6
// I2C defines
// This example will use I2C0 on GPIO8 (SDA) and GPIO9 (SCL) running at 400KHz.
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define I2C_PORT i2c0
#define I2C_SDA_PIN 0
#define I2C_SCK_PIN 1
unsigned short lastBottleCaps;
uint8_t lastBottleCapDigits;
uint8_t check_color(uint16_t newColor);
int main() {
stdio_init_all();
LCD_setPins(DC_PIN, SPI_CS_PIN, RST_PIN, SPI_SCK_PIN, SPI_MOSI_PIN);
LCD_setSPIperiph(spi0);
LCD_initDisplay();
LCD_setRotation(0);
GFX_createFramebuf();
int c = 0;
uint16_t color[] = {ILI9341_RED, ILI9341_GREEN, ILI9341_BLUE,};
#if SERIAL_CON
uart_init(UART_ID, BAUD_RATE);
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
#endif
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, 1);
GFX_clearScreen();
hx711_get_default_config(&hxcfg);
hxcfg.clock_pin = I2C_SCK_PIN; //GPIO pin connected to HX711 clock pin
hxcfg.data_pin = I2C_SDA_PIN; //GPIO pin connected to HX711 data pin
hx711_init(&hx, &hxcfg);
hx711_power_up(&hx, hx711_gain_128);
while (true) {
//hx711_wait_settle(0);
gpio_put(I2C_SCK_PIN, 0);
long newPressure = hx711_get_value(&hx);
gpio_put(I2C_SCK_PIN, 1);
#if SERIAL_CON
char uartMessage[30];
snprintf(uartMessage, sizeof(uartMessage), "Hello, Wokwi!\n %c", c);
uart_puts(UART_ID, uartMessage );
#endif
if(pressure != newPressure)
{
pressure = newPressure;
unsigned short bottleCaps = (unsigned short)(pressure / 0.42 / 2.22);
unsigned short bottleCapDigits = (unsigned short)(log10(bottleCaps));
char capString[5];
sprintf(capString,"%ld", bottleCaps);
GFX_drawCircle(120, 180, 100, ILI9341_RED);
GFX_fillCircle(120, 180, 100, ILI9341_RED);
GFX_setCursor((int16_t)(110 - 11.5 * bottleCapDigits), 167);
GFX_printf(capString);
}
GFX_setCursor(0, 0);
GFX_printf("Hello GFX!\n%d\n%x ", c, newPressure);
c = c + 1;
GFX_flush();
sleep_ms(1000);
}
}