2 #include <twr_module_lcd.h>
3 #include <twr_tca9534a.h>
4 #include <twr_scheduler.h>
5 #include <twr_ls013b7dh03.h>
9 _TWR_MODULE_LCD_BACKLIGHT_PIN = TWR_TCA9534A_PIN_P0,
10 _TWR_MODULE_LCD_BUTTON1_PIN = TWR_TCA9534A_PIN_P1,
11 _TWR_MODULE_LCD_DISP_ON_PIN = TWR_TCA9534A_PIN_P2,
12 _TWR_MODULE_LCD_BUTTON2_PIN = TWR_TCA9534A_PIN_P3,
13 _TWR_MODULE_LCD_LED_GREEN_PIN = TWR_TCA9534A_PIN_P4,
14 _TWR_MODULE_LCD_LED_RED_PIN = TWR_TCA9534A_PIN_P5,
15 _TWR_MODULE_LCD_LED_BLUE_PIN = TWR_TCA9534A_PIN_P6,
16 _TWR_MODULE_LCD_LED_DISP_CS_PIN = TWR_TCA9534A_PIN_P7
19 #define _TWR_MODULE_LCD_INITIALIZED ((1 << _TWR_MODULE_LCD_BACKLIGHT_PIN) | (1 << _TWR_MODULE_LCD_LED_DISP_CS_PIN) | (1 << _TWR_MODULE_LCD_DISP_ON_PIN) | (1 << _TWR_MODULE_LCD_LED_GREEN_PIN) | (1 << _TWR_MODULE_LCD_LED_RED_PIN) | (1 << _TWR_MODULE_LCD_LED_BLUE_PIN))
25 bool is_tca9534a_initialized;
50 static bool _twr_module_lcd_tca9534a_init(
void);
52 static bool _twr_module_lcd_cs_pin_set(
bool state);
54 static void _twr_module_lcd_led_init(
twr_led_t *
self);
56 static void _twr_module_lcd_led_on(
twr_led_t *
self);
58 static void _twr_module_lcd_led_off(
twr_led_t *
self);
60 static void _twr_module_lcd_button_init(
twr_button_t *
self);
62 static int _twr_module_lcd_button_get_input(
twr_button_t *
self);
66 _twr_module_lcd_tca9534a_init();
77 return &_twr_module_lcd.gfx;
92 return _twr_module_lcd_tca9534a_init();
121 void twr_module_lcd_draw(
const uint8_t *frame, uint8_t width, uint8_t height)
128 void twr_module_lcd_printf(uint8_t line,
const uint8_t *
string)
153 uint8_t bytes_per_row = img->width / 8;
155 if(img->width % 8 != 0)
162 for (row = 0; row < img->height; row++) {
163 for (line = 0; line < img->width; line++) {
164 uint32_t byte_offset = line / 8 + row * bytes_per_row;
165 uint32_t bit = line % 8;
166 twr_gfx_draw_pixel(&_twr_module_lcd.gfx, line + left, row + top, (img->data[byte_offset]) & (1 << bit));
188 if (
self == &_twr_module_lcd.button_left)
192 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_LEFT_PRESS, _twr_module_lcd.event_param);
196 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_LEFT_RELEASE, _twr_module_lcd.event_param);
200 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_LEFT_CLICK, _twr_module_lcd.event_param);
204 if (_twr_module_lcd.button_right._state)
206 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_BOTH_HOLD, _twr_module_lcd.event_param);
208 _twr_module_lcd.button_right._hold_signalized =
true;
212 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_LEFT_HOLD, _twr_module_lcd.event_param);
217 if (
self == &_twr_module_lcd.button_right)
221 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_RIGHT_PRESS, _twr_module_lcd.event_param);
225 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_RIGHT_RELEASE, _twr_module_lcd.event_param);
229 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_RIGHT_CLICK, _twr_module_lcd.event_param);
233 if (_twr_module_lcd.button_left._state)
235 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_BOTH_HOLD, _twr_module_lcd.event_param);
237 _twr_module_lcd.button_left._hold_signalized =
true;
241 _twr_module_lcd.event_handler(TWR_MODULE_LCD_EVENT_RIGHT_HOLD, _twr_module_lcd.event_param);
249 _twr_module_lcd.event_handler = event_handler;
250 _twr_module_lcd.event_param = event_param;
299 .
init = _twr_module_lcd_led_init,
300 .on = _twr_module_lcd_led_on,
301 .off = _twr_module_lcd_led_off,
304 return &twr_module_lcd_led_driver;
311 .
init = _twr_module_lcd_button_init,
312 .get_input = _twr_module_lcd_button_get_input,
315 return &twr_module_lcd_button_driver;
318 static bool _twr_module_lcd_tca9534a_init(
void)
320 if (!_twr_module_lcd.is_tca9534a_initialized)
337 _twr_module_lcd.is_tca9534a_initialized =
true;
343 static bool _twr_module_lcd_cs_pin_set(
bool state)
345 if (!_twr_module_lcd_tca9534a_init())
352 _twr_module_lcd.is_tca9534a_initialized =
false;
360 static void _twr_module_lcd_led_init(
twr_led_t *
self)
364 _twr_module_lcd_tca9534a_init();
367 static void _twr_module_lcd_led_on(
twr_led_t *
self)
369 if (!
twr_tca9534a_write_pin(&_twr_module_lcd.tca9534a, _twr_module_lcd_led_pin_lut[self->_channel.virtual], self->_idle_state ? 0 : 1))
371 _twr_module_lcd.is_tca9534a_initialized =
false;
375 static void _twr_module_lcd_led_off(
twr_led_t *
self)
377 if (!
twr_tca9534a_write_pin(&_twr_module_lcd.tca9534a, _twr_module_lcd_led_pin_lut[self->_channel.virtual], self->_idle_state ? 1 : 0))
379 _twr_module_lcd.is_tca9534a_initialized =
false;
383 static void _twr_module_lcd_button_init(
twr_button_t *
self)
387 _twr_module_lcd_tca9534a_init();
393 static int _twr_module_lcd_button_get_input(
twr_button_t *
self)
402 if (!
twr_tca9534a_read_pin(&_twr_module_lcd.tca9534a, _twr_module_lcd_button_pin_lut[self->_channel.virtual], &state))
404 _twr_module_lcd.is_tca9534a_initialized =
false;
void twr_gfx_draw_rectangle(twr_gfx_t *self, int x0, int y0, int x1, int y1, uint32_t color)
Display draw rectangle.
void twr_gfx_draw_line(twr_gfx_t *self, int x0, int y0, int x1, int y1, uint32_t color)
Display draw line.
void twr_gfx_set_font(twr_gfx_t *self, const twr_font_t *font)
Set font.
void twr_gfx_set_rotation(twr_gfx_t *self, twr_gfx_rotation_t rotation)
Set rotation.
int twr_gfx_draw_char(twr_gfx_t *self, int left, int top, uint8_t ch, uint32_t color)
Display draw char.
void twr_gfx_init(twr_gfx_t *self, void *display, const twr_gfx_driver_t *driver)
Initialize button.
twr_gfx_rotation_t twr_gfx_get_rotation(twr_gfx_t *self)
Get rotation.
bool twr_gfx_display_is_ready(twr_gfx_t *self)
Check if display driver is ready for commands.
void twr_gfx_draw_circle(twr_gfx_t *self, int x0, int y0, int radius, uint32_t color)
Lcd draw circle, using Midpoint circle algorithm.
twr_gfx_rotation_t
Rotation.
void twr_gfx_draw_pixel(twr_gfx_t *self, int x, int y, uint32_t color)
Draw pixel.
void twr_gfx_clear(twr_gfx_t *self)
Clear.
int twr_gfx_draw_string(twr_gfx_t *self, int left, int top, char *str, uint32_t color)
Display draw string.
bool twr_gfx_update(twr_gfx_t *self)
Display update, send data.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
@ TWR_GPIO_MODE_INPUT
GPIO channel operates as input.
@ TWR_GPIO_BUTTON
GPIO channel BUTTON.
@ TWR_I2C_I2C0
I2C channel I2C0.
struct twr_led_t twr_led_t
LED instance.
const twr_gfx_driver_t * twr_ls013b7dh03_get_driver(void)
Get Lcd driver.
void twr_ls013b7dh03_init(twr_ls013b7dh03_t *self, bool(*pin_cs_set)(bool state))
Initialize lcd driver.
void twr_module_lcd_clear(void)
Lcd clear.
bool twr_module_lcd_update(void)
Lcd update, send data.
bool twr_module_lcd_on(void)
Lcd on.
void twr_module_lcd_draw_rectangle(int x0, int y0, int x1, int y1, bool color)
Lcd draw rectangle.
void twr_module_lcd_draw_line(int x0, int y0, int x1, int y1, bool color)
Lcd draw line.
int twr_module_lcd_draw_char(int left, int top, uint8_t ch, bool color)
Lcd draw char.
void twr_module_lcd_draw_image(int left, int top, const twr_image_t *img)
Lcd draw image.
void twr_module_lcd_draw_circle(int x0, int y0, int radius, bool color)
Lcd draw circle.
void twr_module_lcd_set_button_click_timeout(twr_tick_t click_timeout)
Set click timeout (maximum interval within which button has to be released to recognize click event)
twr_module_lcd_event_t
Callback events.
void twr_module_lcd_set_button_debounce_time(twr_tick_t debounce_time)
Set debounce time (minimum sampling interval during which input cannot change to toggle its state)
twr_module_lcd_rotation_t
Rotation.
const twr_led_driver_t * twr_module_lcd_get_led_driver(void)
Lcd get led driver.
int twr_module_lcd_draw_string(int left, int top, char *str, bool color)
Lcd draw string.
twr_gfx_t * twr_module_lcd_get_gfx()
Get gfx instance.
void twr_module_lcd_set_event_handler(void(*event_handler)(twr_module_lcd_event_t, void *), void *event_param)
Lcd set event handler for buttons.
twr_module_lcd_rotation_t twr_module_lcd_get_rotation(void)
Lcd get rotation.
bool twr_module_lcd_is_ready(void)
Check if lcd is ready for commands.
bool twr_module_lcd_is_present(void)
Get LCD Module is pressent, can use without twr_module_lcd_init.
void twr_module_lcd_set_button_hold_time(twr_tick_t hold_time)
Set hold time (interval after which hold event is recognized when button is steadily pressed)
void twr_module_lcd_set_font(const twr_font_t *font)
Lcd set font.
void twr_module_lcd_set_rotation(twr_module_lcd_rotation_t rotation)
Lcd set rotation.
bool twr_module_lcd_off(void)
Lcd off.
void twr_module_lcd_set_button_scan_interval(twr_tick_t scan_interval)
Set scan interval (period of button input sampling)
const twr_button_driver_t * twr_module_lcd_get_button_driver(void)
Lcd get button driver.
void twr_module_lcd_draw_pixel(int x, int y, bool value)
Lcd draw pixel.
void twr_module_lcd_init()
Initialize lcd.
@ TWR_MODULE_LCD_LED_RED
LCD red LED channel.
@ TWR_MODULE_LCD_LED_BLUE
LCD blue LED channel.
@ TWR_MODULE_LCD_LED_GREEN
LCD green LED channel.
@ TWR_MODULE_LCD_BUTTON_RIGHT
LCD right button channel.
@ TWR_MODULE_LCD_BUTTON_LEFT
LCD left button channel.
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t state)
Write state to all pins.
bool twr_tca9534a_read_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int *state)
Read pin state.
twr_tca9534a_pin_t
Individual pin names.
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
bool twr_tca9534a_write_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int state)
Write pin state.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.
void(* init)(twr_led_t *self)
Callback for initialization.