commit ef0bb65fdfd94ae4b6cf06ef7487ed7cd8a36aac Author: Fluffy-Bean Date: Sun Jun 22 13:13:01 2025 +0100 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/lib_sharp.c b/lib_sharp.c new file mode 100644 index 0000000..2b3633f --- /dev/null +++ b/lib_sharp.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include "hardware/gpio.h" +#include "hardware/spi.h" +#include "lib_sharp.h" + +sharp_display_t sharp_display_new(uint16_t width, uint16_t height, spi_inst_t * spi, uint8_t cs) +{ + gpio_put(cs, false); + + sharp_display_t display = { + .width = width, + .height = height, + .cs = cs, + .vcom = CMD_VCOM, + .framebuffer = (uint8_t *)malloc((width * height) / 8), + .spi = (spi) + }; + + return display; +} + +bool sharp_display_error(sharp_display_t * display) +{ + return (display == NULL || display->framebuffer == NULL); +} + +void sharp_display_refresh_screen(sharp_display_t * display) +{ + gpio_put(display->cs, true); + sharp_display_toggle_vcom(display); + + // These are sizes without the current line and end of line data + uint16_t line_size = display->width / 8; + uint16_t frame_size = (display->width * display->height) / 8; + + uint16_t buff_size = 1 + display->height * (1 + (display->width / 8) + 1) + 1; + uint8_t buff[buff_size]; + + uint16_t buff_offset = 0; + + buff[buff_offset] = display->vcom | CMD_WRITE; + buff_offset += 1; + + for (int i = 0; i < frame_size; i += line_size) + { + // Current line + buff[buff_offset] = ((i + 1) / (display->width / 8)) + 1; + buff_offset += 1; + + // Line data + memcpy(buff + buff_offset, display->framebuffer + i, line_size); + buff_offset += line_size; + + // End of line + buff[buff_offset] = 0x00; + buff_offset += 1; + } + + // End of transition + buff[buff_offset] = 0x00; + buff_offset += 1; + + if (buff_size != buff_offset) + { + printf("Sussy buffer size offset\n"); + } + + spi_write_blocking(display->spi, buff, buff_offset); + gpio_put(display->cs, false); +} + +void sharp_display_clear_screen(sharp_display_t * display) +{ + sharp_display_clear_buffer(display); + gpio_put(display->cs, true); + + uint8_t buff[2] = {display->vcom | CMD_CLEAR, 0x00}; + + spi_write_blocking(display->spi, buff, 2); + + sharp_display_toggle_vcom(display); + gpio_put(display->cs, false); +} + +void sharp_display_clear_buffer(sharp_display_t * display) +{ + memset(display->framebuffer, 0, display->width * display->height / 8); +} + +void sharp_display_toggle_vcom(sharp_display_t * display) +{ + display->vcom = display->vcom ? 0x00 : CMD_VCOM; +} diff --git a/lib_sharp.h b/lib_sharp.h new file mode 100644 index 0000000..6c45c2f --- /dev/null +++ b/lib_sharp.h @@ -0,0 +1,30 @@ +#ifndef LIB_SHARP_H +#define LIB_SHARP_H + +#include "hardware/spi.h" + +#define CMD_WRITE 0x80 +#define CMD_CLEAR 0x20 +#define CMD_VCOM 0x40 + +#define SWAP(a, b) { a ^= b; b ^= a; a ^= b; } + +typedef struct sharp_display { + uint16_t width, height; + uint8_t cs; + uint8_t vcom; + uint8_t * framebuffer; + spi_inst_t * spi; +} sharp_display_t; + +sharp_display_t sharp_display_new(uint16_t width, uint16_t height, spi_inst_t * spi, uint8_t cs); +bool sharp_display_error(sharp_display_t * display); +void sharp_display_refresh_screen(sharp_display_t * display); +void sharp_display_clear_screen(sharp_display_t * display); +void sharp_display_clear_buffer(sharp_display_t * display); +void sharp_display_toggle_vcom(sharp_display_t * display); + +void sharp_display_draw_pixel(sharp_display_t *display, int x, int y, bool color); +void sharp_display_draw_circle(sharp_display_t *display, int x0, int y0, int radius, bool color); + +#endif //LIB_SHARP_H