summaryrefslogtreecommitdiffstats
path: root/Smol Watch Project/My Project/watch-library/watch.c
blob: 73d67c356f852dc04a67afd529ec9c882c119298 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * watch.c
 *
 * Created: 4/25/2021 10:22:10 AM
 *  Author: joeycastillo
 */ 

#include "watch.h"
#include "driver_init.h"
#include <stdlib.h>
#include <string.h>

void watch_init(Watch *watch) {
	memset(watch, 0, sizeof(*watch));
}

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 segmap[] = {
		0x4e4f0e8e8f8d4d0d, // Position 8
		0xc8c4c4c8b4b4b0b,  // Position 9
		0xc049c00a49890949, // Position 6
		0xc048088886874707, // Position 7
		0xc053921252139352, // Position 0
		0xc054511415559594, // Position 1
		0xc057965616179716, // Position 2
		0xc041804000018a81, // Position 3
		0xc043420203048382, // Position 4
		0xc045440506468584, // Position 5
	};
	watch->num_chars = 10;
	watch->segment_map = &segmap[0];

	SEGMENT_LCD_0_init();
	slcd_sync_enable(&SEGMENT_LCD_0);
	watch->display_enabled = true;
}

void watch_display_pixel(Watch *watch, uint8_t com, uint8_t seg) {
	slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
}

void watch_clear_pixel(Watch *watch, uint8_t com, uint8_t seg) {
	slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
}

void watch_display_character(Watch *watch, uint8_t character, uint8_t position) {
	uint64_t segmap = watch->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, char *string, uint8_t position) {
	size_t i = 0;
	while(string[i] != 0) {
		watch_display_character(watch, string[i], position + i);
		i++;
		if (i >= watch->num_chars) break;
	}
}

void watch_enable_interrupts(Watch *watch) {
	EXTERNAL_IRQ_0_init();
}

void watch_register_interrupt_callback(Watch *watch, const uint32_t pin, ext_irq_cb_t callback) {
	ext_irq_register(pin, callback);
}

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(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) {
	TC3->COUNT16.CC[0].reg = red;
	TC3->COUNT16.CC[1].reg = green;
}

void watch_set_led_red() {
	watch_set_led_color(65535, 0);
}

void watch_set_led_green() {
	watch_set_led_color(0, 65535);
}

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) {
	calendar_set_date(&CALENDAR_0, &date_time.date);
	calendar_set_time(&CALENDAR_0, &date_time.time);
}

void watch_get_date_time(struct calendar_date_time *date_time) {
	calendar_get_date_time(&CALENDAR_0, date_time);
}

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);
			break;
		case A1:
			gpio_set_pin_function(A1, PINMUX_PB01B_ADC_AIN9);
			break;
		case A2:
			gpio_set_pin_function(A2, PINMUX_PB02B_ADC_AIN10);
			break;
		default:
			return;
	}
	gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF);
}

void watch_enable_digital_input(const uint8_t pin) {
	gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
	gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
}

void watch_enable_digital_output(const uint8_t pin) {
	gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
	gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
}

struct io_descriptor *I2C_0_io;

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);
}

void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length) {
	i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN);
	io_write(I2C_0_io, buf, length);
}

void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length) {
	i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN);
	io_read(I2C_0_io, buf, length);
}