initial work on segment LCD

This commit is contained in:
Joey Castillo 2021-04-27 23:06:48 -04:00
parent 46083a416e
commit 6833b44f50
4 changed files with 297 additions and 86 deletions

View file

@ -69,6 +69,8 @@ void PWM_1_PORT_init(void);
void PWM_1_CLOCK_init(void);
void PWM_1_init(void);
void SEGMENT_LCD_0_init(void);
/**
* \brief Perform system initialization, initialize pins and clocks for
* peripherals

View file

@ -15,13 +15,18 @@ int main(void)
date_time.time.sec = 0;
watch_init(&watch);
watch_enable_led();
watch_enable_date_time();
watch_enable_led(&watch);
watch_enable_date_time(&watch);
watch_set_date_time(date_time);
watch_enable_digital_output(A0);
gpio_set_pin_level(A0, true);
watch_enable_i2c();
watch_enable_display(&watch);
/*
watch_enable_i2c(&watch);
uint8_t chipID = 0;
uint8_t ChipIdRegister = 0xD0;
watch_i2c_send(0x77, &ChipIdRegister, 1);
@ -29,7 +34,7 @@ int main(void)
if (chipID == 0x60) {
watch_set_led_green();
}
*/
uint8_t last = date_time.time.sec;
while (1) {
@ -37,9 +42,11 @@ int main(void)
if (date_time.time.sec != last) {
last = date_time.time.sec;
if (last % 2 == 0) {
watch_set_led_red();
watch_set_led_color(50, 0);
watch_display_string(&watch, watch.main_display, " Hello");
} else {
watch_set_led_green();
watch_set_led_color(0, 50);
watch_display_string(&watch, watch.main_display, " there");
}
}
}

View file

@ -7,21 +7,204 @@
#include "watch.h"
#include "driver_init.h"
#include <stdlib.h>
#include <string.h>
void watch_init(Watch *watch) {
memset(watch, 0, sizeof(*watch));
}
void watch_enable_led() {
const uint8_t Character_Set[] =
{
0b00000000, //
0b00000000, // !
0b00100010, // "
0b00000000, // #
0b00000000, // $
0b00000000, // %
0b01000100, // &
0b00100000, // '
0b00000000, // (
0b00000000, // )
0b00000000, // *
0b11000000, // +
0b00010000, // ,
0b01000000, // -
0b00000100, // .
0b00010010, // /
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b00000000, // :
0b00000000, // ;
0b01011000, // <
0b01001000, // =
0b01001100, // >
0b01010011, // ?
0b11111111, // @
0b01110111, // A
0b01111111, // B
0b00111001, // C
0b00111111, // D
0b01111001, // E
0b01110001, // F
0b00111101, // G
0b01110110, // H
0b10001001, // I
0b00001110, // J
0b11101010, // K
0b00111000, // L
0b10110111, // M
0b00110111, // N
0b00111111, // O
0b01110011, // P
0b01100111, // Q
0b11110111, // R
0b01101101, // S
0b10000001, // T
0b00111110, // U
0b00111110, // V
0b10111110, // W
0b01111110, // X
0b01101110, // Y
0b00011011, // Z
0b00111001, // [
0b00100100, // backslash
0b00001111, // ]
0b00100110, // ^
0b00001000, // _
0b00000010, // `
0b01011111, // a
0b01111100, // b
0b01011000, // c
0b01011110, // d
0b01111011, // e
0b01110001, // f
0b01101111, // g
0b01110100, // h
0b00010000, // i
0b01000010, // j
0b11101010, // k
0b00110000, // l
0b10110111, // m
0b01010100, // n
0b01011100, // o
0b01110011, // p
0b01100111, // q
0b01010000, // r
0b01101101, // s
0b01111000, // t
0b01100010, // u
0b01100010, // v
0b10111110, // w
0b01111110, // x
0b01101110, // y
0b00011011, // z
0b00111001, // {
0b00110000, // |
0b00001111, // }
0b00000001, // ~
};
void watch_enable_display(Watch *watch) {
if (watch->display_enabled) return;
static const uint64_t main_segmap[] = {
0xc053921252139352, // Position 0
0xc054511415559594, // Position 1
0xc057965616179716, // Position 2
0xc041804000018a81, // Position 3
0xc043420203048382, // Position 4
0xc045440506468584, // Position 5
};
watch->main_display.num_chars = 6;
watch->main_display.chars = malloc(6);
watch->main_display.segment_map = &main_segmap[0];
static const uint64_t day_segmap[] = {
0xc049c00a49890949, // Position 6
0xc048088886874707, // Position 7
};
watch->day_display.num_chars = 2;
watch->day_display.chars = malloc(2);
watch->day_display.segment_map = &day_segmap[0];
static const uint64_t date_segmap[] = {
0x4e4f0e8e8f8d4d0d, // Position 8
0xc8c4c4c8b4b4b0b, // Position 9
};
watch->date_display.num_chars = 2;
watch->date_display.chars = malloc(2);
watch->date_display.segment_map = &date_segmap[0];
SEGMENT_LCD_0_init();
slcd_sync_enable(&SEGMENT_LCD_0);
watch->display_enabled = true;
}
void watch_display_pixel(Watch *watch, WatchDisplay display, uint8_t com, uint8_t seg) {
slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
}
void watch_clear_pixel(Watch *watch, WatchDisplay display, uint8_t com, uint8_t seg) {
slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
}
void watch_display_character(Watch *watch, WatchDisplay display, uint8_t character, uint8_t position) {
uint64_t segmap = display.segment_map[position];
uint64_t segdata = Character_Set[character - 0x20];
for (int i = 0; i < 8; i++) {
uint8_t com = (segmap & 0xFF) >> 6;
if (com > 2) {
// COM3 means no segment exists; skip it.
segmap = segmap >> 8;
segdata = segdata >> 1;
continue;
}
uint8_t seg = segmap & 0x3F;
slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
if (segdata & 1) slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
segmap = segmap >> 8;
segdata = segdata >> 1;
}
}
void watch_display_string(Watch *watch, WatchDisplay display, char *string) {
size_t i = 0;
while(string[i] != 0) {
watch_display_character(watch, display, string[i], i);
i++;
if (i >= display.num_chars) break;
}
}
void watch_enable_led(Watch *watch) {
if (watch->led_enabled) return;
PWM_0_init();
pwm_set_parameters(&PWM_0, 10000, 0);
pwm_enable(&PWM_0);
watch->led_enabled = true;
}
void watch_disable_led() {
void watch_disable_led(Watch *watch) {
if (!watch->led_enabled) return;
gpio_set_pin_function(RED, GPIO_PIN_FUNCTION_OFF);
gpio_set_pin_function(GREEN, GPIO_PIN_FUNCTION_OFF);
pwm_disable(&PWM_0);
watch->led_enabled = false;
}
void watch_set_led_color(uint16_t red, uint16_t green) {
@ -37,9 +220,12 @@ void watch_set_led_green() {
watch_set_led_color(0, 65535);
}
void watch_enable_date_time() {
void watch_enable_date_time(Watch *watch) {
if (watch->calendar_enabled) return;
CALENDAR_0_init();
calendar_enable(&CALENDAR_0);
watch->calendar_enabled = true;
}
void watch_set_date_time(struct calendar_date_time date_time) {
@ -51,8 +237,9 @@ void watch_get_date_time(struct calendar_date_time *date_time) {
calendar_get_date_time(&CALENDAR_0, date_time);
}
void watch_enable_analog(const uint8_t pin) {
ADC_0_init(); // todo: only call this once
void watch_enable_analog(Watch *watch, const uint8_t pin) {
if (!watch->adc_enabled) ADC_0_init();
switch (pin) {
case A0:
gpio_set_pin_function(A0, PINMUX_PB04B_ADC_AIN12);
@ -81,7 +268,8 @@ void watch_enable_digital_output(const uint8_t pin) {
struct io_descriptor *I2C_0_io;
void watch_enable_i2c() {
void watch_enable_i2c(Watch *watch) {
if (watch->i2c_enabled) return;
I2C_0_init();
i2c_m_sync_get_io_descriptor(&I2C_0, &I2C_0_io);
i2c_m_sync_enable(&I2C_0);

View file

@ -11,37 +11,51 @@
#include <stdint.h>
#include "hpl_calendar.h"
#define WATCH_NUM_DISPLAYS (3)
struct WatchDisplay {
typedef struct WatchDisplay {
uint8_t num_chars;
uint8_t* chars;
};
const uint64_t* segment_map;
} WatchDisplay;
typedef struct Watch {
struct WatchDisplay displays[WATCH_NUM_DISPLAYS];
struct WatchDisplay main_display; // 6 chars, main line.
struct WatchDisplay day_display; // 2 chars, alphanumeric-ish. top center.
struct WatchDisplay date_display; // 2 chars, only supports numbers 0-39. top right.
bool display_enabled;
bool led_enabled;
bool buzzer_enabled;
bool calendar_enabled;
bool adc_enabled;
bool i2c_enabled;
bool spi_enabled;
bool eic_enabled;
} Watch;
void watch_init(Watch *watch);
void watch_enable_led();
void watch_disable_led();
void watch_enable_display(Watch *watch);
void watch_display_pixel(Watch *watch, WatchDisplay display, uint8_t com, uint8_t seg);
void watch_display_string(Watch *watch, WatchDisplay display, char *string);
void watch_enable_led(Watch *watch);
void watch_disable_led(Watch *watch);
void watch_set_led_color(uint16_t red, uint16_t green);
void watch_set_led_red();
void watch_set_led_green();
void watch_enable_date_time();
void watch_enable_date_time(Watch *watch);
void watch_set_date_time(struct calendar_date_time date_time);
void watch_get_date_time(struct calendar_date_time *date_time);
void watch_enable_analog(const uint8_t pin);
void watch_enable_analog(Watch *watch, const uint8_t pin);
void watch_enable_digital_input(const uint8_t pin);
void watch_enable_digital_output(const uint8_t pin);
struct io_descriptor *I2C_0_io;
void watch_enable_i2c();
void watch_enable_i2c(Watch *watch);
void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length);
void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length);