aboutsummaryrefslogtreecommitdiffstats
path: root/src/lcd.c
blob: a2a8ca79cc15a728347b990605fe5ad84a20eec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <lcd.h>
#include <clock.h>
#include <gpio.h>
#include <string.h>

//#define USE_DELAY

#define LCD_D0 GPIO_PORT(A, 0)
#define LCD_D1 GPIO_PORT(A, 1)
#define LCD_D2 GPIO_PORT(A, 4)
#define LCD_D3 GPIO_PORT(B, 0)
#define LCD_D4 GPIO_PORT(C, 1)
#define LCD_D5 GPIO_PORT(C, 0)
#define LCD_D6 GPIO_PORT(C, 2)
#define LCD_D7 GPIO_PORT(C, 3)
#define LCD_E  GPIO_PORT(C, 12)
#define LCD_RS GPIO_PORT(C, 10)

#define lcd_data() gpio_dout(LCD_RS, 1)

void lcd_pulse(void)
{
	gpio_dout(LCD_E, 1);
#ifdef USE_DELAY
	delay(1);
#else
	for (uint16_t i = 0; i < 10000; i++)
		asm("");
#endif // USE_DELAY
	gpio_dout(LCD_E, 0);
}

void lcd_byte(uint8_t byte)
{
	gpio_dout(LCD_D0, byte & 0x01);
	gpio_dout(LCD_D1, byte & 0x02);
	gpio_dout(LCD_D2, byte & 0x04);
	gpio_dout(LCD_D3, byte & 0x08);
	gpio_dout(LCD_D4, byte & 0x10);
	gpio_dout(LCD_D5, byte & 0x20);
	gpio_dout(LCD_D6, byte & 0x40);
	gpio_dout(LCD_D7, byte & 0x80);
}

void lcd_cmd(uint8_t cmd)
{
	gpio_dout(LCD_RS, 0);
	lcd_byte(cmd);
	lcd_pulse();
}

void lcd_putchar(int c)
{
	lcd_data();
	lcd_byte((uint8_t)c);
	lcd_pulse();
}

static int lcd_index = 0;
void lcd_puts(const char *s)
{
	lcd_cmd(0x06);
	while (*s) {
		lcd_putchar(*s++);
		if (++lcd_index == 0x10) {
			lcd_cmd(0x80 | 0x40);
		} else if (lcd_index == 0x20) {
			lcd_cmd(0x80);
			lcd_index = 0;
		}
	}
}

extern char *itoa(int n, int base);
void lcd_puti(int i)
{
	lcd_puts(itoa(i, 10));
}

void lcd_puth(int h)
{
	lcd_puts(itoa(h, 16));
}

void lcd_putb(uint8_t b)
{
	lcd_puts(itoa(b, 2));
}

void lcd_clear(void)
{
	lcd_cmd(0x01);
	delay(2);
	lcd_index = 0;
}

void lcd_init(void)
{
	gpio_mode(LCD_D0, OUTPUT);
	gpio_mode(LCD_D1, OUTPUT);
	gpio_mode(LCD_D2, OUTPUT);
	gpio_mode(LCD_D3, OUTPUT);
	gpio_mode(LCD_D4, OUTPUT);
	gpio_mode(LCD_D5, OUTPUT);
	gpio_mode(LCD_D6, OUTPUT);
	gpio_mode(LCD_D7, OUTPUT);
	gpio_mode(LCD_E,  OUTPUT);
	gpio_mode(LCD_RS, OUTPUT);
	gpio_dout(LCD_D0, 0);
	gpio_dout(LCD_D1, 0);
	gpio_dout(LCD_D2, 0);
	gpio_dout(LCD_D3, 0);
	gpio_dout(LCD_D4, 0);
	gpio_dout(LCD_D5, 0);
	gpio_dout(LCD_D6, 0);
	gpio_dout(LCD_D7, 0);
	gpio_dout(LCD_E,  0);
	gpio_dout(LCD_RS, 0);

	lcd_cmd(0x38);
	lcd_cmd(0x10);
	lcd_cmd(0x0D);
	delay(5);
	lcd_clear();
}

/**
 * Task code
 */

volatile int bufpos = 0;
volatile char buf[32];
volatile uint8_t using = 0;

void lcd_clearbuf(void)
{
	bufpos = 0;
	for (int i = 0; i < 32; i++)
		buf[i] = 0;
}

void lcd_put(const char *s)
{
	int len = strlen(s);
	int i;

	using = 1;
	for (i = 0; i < len; bufpos++, i++) {
		if (bufpos > 31)
			bufpos = 0;
		buf[bufpos] = s[i];
	}
	using = 0;
}

void lcd_handler(void)
{
	lcd_init();
	lcd_clearbuf();

	while (1) {
		if (!using && buf[0] != '\0') {
			lcd_puts(buf);
			lcd_clearbuf();
		}
		delay(100);
	}
}