avr/display/display.c

156 lines
4.2 KiB
C

#include <stdlib.h>
#include <display.h>
#include <util/delay.h>
#include <base.h>
/*
struct CHAR {
char c;
uint8_t bits;
};
struct CHAR CHARSET[96] = {
{ .c = ' ', .bits = 0x20 },
{ .c = '!', .bits = 0x21 },
{ .c = '"', .bits = 0x22 },
{ .c = '#', .bits = 0x23 },
{ .c = '$', .bits = 0x24 },
{ .c = '%', .bits = 0x25 },
{ .c = '&', .bits = 0x26 },
{ .c = ''', .bits = 0x27 },
{ .c = '(', .bits = 0x28 },
{ .c = ')', .bits = 0x29 },
{ .c = '*', .bits = 0x2a },
{ .c = '+', .bits = 0x2b },
{ .c = ',', .bits = 0x2c },
{ .c = '-', .bits = 0x2d },
{ .c = '.', .bits = 0x2e },
{ .c = '/', .bits = 0x2f },
{ .c = '0', .bits = 0x30 },
{ .c = '1', .bits = 0x31 },
{ .c = '2', .bits = 0x32 },
{ .c = '3', .bits = 0x33 },
{ .c = '4', .bits = 0x34 },
{ .c = '5', .bits = 0x35 },
{ .c = '6', .bits = 0x36 },
{ .c = '7', .bits = 0x37 },
{ .c = '8', .bits = 0x38 },
{ .c = '9', .bits = 0x39 },
{ .c = ':', .bits = 0x3a },
{ .c = ';', .bits = 0x3b },
{ .c = '<', .bits = 0x3c },
{ .c = '=', .bits = 0x3d },
{ .c = '>', .bits = 0x3e },
{ .c = '?', .bits = 0x3f },
{ .c = '@', .bits = 0x40 },
{ .c = 'A', .bits = 0x41 },
{ .c = 'B', .bits = 0x42 },
{ .c = 'C', .bits = 0x43 },
{ .c = 'D', .bits = 0x44 },
{ .c = 'E', .bits = 0x45 },
{ .c = 'F', .bits = 0x46 },
{ .c = 'G', .bits = 0x47 },
{ .c = 'H', .bits = 0x48 },
{ .c = 'I', .bits = 0x49 },
{ .c = 'J', .bits = 0x4a },
{ .c = 'K', .bits = 0x4b },
{ .c = 'L', .bits = 0x4c },
{ .c = 'M', .bits = 0x4d },
{ .c = 'N', .bits = 0x4e },
{ .c = 'O', .bits = 0x4f },
{ .c = 'P', .bits = 0x50 },
{ .c = 'Q', .bits = 0x51 },
{ .c = 'R', .bits = 0x52 },
{ .c = 'S', .bits = 0x53 },
{ .c = 'T', .bits = 0x54 },
{ .c = 'U', .bits = 0x55 },
{ .c = 'V', .bits = 0x56 },
{ .c = 'W', .bits = 0x57 },
{ .c = 'X', .bits = 0x58 },
{ .c = 'Y', .bits = 0x59 },
{ .c = 'Z', .bits = 0x5a },
{ .c = '[', .bits = 0x5b },
{ .c = ' ', .bits = 0x5c },
{ .c = ']', .bits = 0x5d },
{ .c = '^', .bits = 0x5e },
{ .c = '_', .bits = 0x5f },
{ .c = '`', .bits = 0x60 },
{ .c = 'a', .bits = 0x61 },
{ .c = 'b', .bits = 0x62 },
{ .c = 'c', .bits = 0x63 },
{ .c = 'd', .bits = 0x64 },
{ .c = 'e', .bits = 0x65 },
{ .c = 'f', .bits = 0x66 },
{ .c = 'g', .bits = 0x67 },
{ .c = 'h', .bits = 0x68 },
{ .c = 'i', .bits = 0x69 },
{ .c = 'j', .bits = 0x6a },
{ .c = 'k', .bits = 0x6b },
{ .c = 'l', .bits = 0x6c },
{ .c = 'm', .bits = 0x6d },
{ .c = 'n', .bits = 0x6e },
{ .c = 'o', .bits = 0x6f },
{ .c = 'p', .bits = 0 },
{ .c = 'q', .bits = 0 },
{ .c = 'r', .bits = 0 },
{ .c = 's', .bits = 0 },
{ .c = 't', .bits = 0 },
{ .c = 'u', .bits = 0 },
{ .c = 'v', .bits = 0 },
{ .c = 'w', .bits = 0 },
{ .c = 'x', .bits = 0 },
{ .c = 'y', .bits = 0 },
{ .c = 'z', .bits = 0 },
{ .c = ' ', .bits = 0 },
{ .c = ' ', .bits = 0 },
{ .c = ' ', .bits = 0 },
{ .c = ' ', .bits = 0 },
{ .c = ' ', .bits = 0 },
};
*/
void display_strobe_line(gpio_t *line) {
set_line(line);
_delay_us(100);
clear_line(line);
_delay_us(100);
}
void write_instruction(display_t *disp, uint8_t bitcode) {
clear_line(&disp->register_select);
sr_send_msb(&disp->reg, bitcode);
_delay_ms(1);
display_strobe_line(&disp->enable);
}
void write_char(display_t *disp, uint8_t bitcode) {
set_line(&disp->register_select);
sr_send_msb(&disp->reg, bitcode);
_delay_ms(1);
display_strobe_line(&disp->enable);
}
void display_init(display_t *disp) {
sr_initialize(&disp->reg);
set_line_direction(&disp->register_select, LINE_OUT);
set_line_direction(&disp->enable, LINE_OUT);
write_instruction(disp, 0b00111100);
}
void display_clear(display_t *disp) {
write_instruction(disp, 0x01);
write_instruction(disp, 0x02);
}
void display_enable(display_t *disp) {
write_instruction(disp, 0x0e);
}
void display_write_message(display_t *disp, const char *msg) {
for (size_t i = 0; msg[i] != 0; i++) {
write_char(disp, msg[i]);
}
}