-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhw.c
150 lines (129 loc) · 3.49 KB
/
hw.c
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
/*
* TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "hw.h"
#include "cpu.h"
#include "hal.h"
/* SEG -> LCD mapping */
#if defined(E0C6S48_SUPPORT)
/* 51 segments */
static u8_t seg_pos[MEM_DISPLAY1_SIZE/2] = {0, 1, 2, 3, 4, 5, 6, 7, 32, 8, 9, 10, 11, 12 ,13 ,14, 15, 33, 34, 35, 31, 30, 29, 28, 27, 26, 25, 24, 36, 23, 22, 21, 20, 19, 18, 17, 16, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
#elif defined(E0C6S46_SUPPORT)
/* 40 segments */
static u8_t seg_pos[MEM_DISPLAY1_SIZE/2] = {0, 1, 2, 3, 4, 5, 6, 7, 32, 8, 9, 10, 11, 12 ,13 ,14, 15, 33, 34, 35, 31, 30, 29, 28, 27, 26, 25, 24, 36, 23, 22, 21, 20, 19, 18, 17, 16, 37, 38, 39};
#endif
bool_t hw_init(void)
{
/* Buttons/Tap sensor are active LOW */
cpu_set_input_pin(PIN_K00, PIN_STATE_HIGH);
cpu_set_input_pin(PIN_K01, PIN_STATE_HIGH);
cpu_set_input_pin(PIN_K02, PIN_STATE_HIGH);
cpu_set_input_pin(PIN_K03, PIN_STATE_HIGH);
return 0;
}
void hw_release(void)
{
}
void hw_set_lcd_pin(u8_t seg, u8_t com, u8_t val)
{
//printf(" hw_set_lcd_pin: seg = %u, com = %u, val = %u\n", seg, com, val);
if (seg_pos[seg] < LCD_WIDTH) {
g_hal->set_lcd_matrix(seg_pos[seg], com, val);
} else {
/*
* IC n -> seg-com|...
* IC 0 -> 8-0 |18-3 |19-2
* IC 1 -> 8-1 |17-0 |19-3
* IC 2 -> 8-2 |17-1 |37-12|38-13|39-14
* IC 3 -> 8-3 |17-2 |18-1 |19-0
* IC 4 -> 28-12|37-13|38-14|39-15
* IC 5 -> 28-13|37-14|38-15
* IC 6 -> 28-14|37-15|39-12
* IC 7 -> 28-15|38-12|39-13
*/
if (seg == 8 && com < 4) {
g_hal->set_lcd_icon(com, val);
} else if (seg == 28 && com >= 12) {
g_hal->set_lcd_icon(com - 8, val);
}
}
}
void hw_set_button(button_t btn, btn_state_t state)
{
pin_state_t pin_state = (state == BTN_STATE_PRESSED) ? PIN_STATE_LOW : PIN_STATE_HIGH;
switch (btn) {
case BTN_TAP:
cpu_set_input_pin(PIN_K03, pin_state);
break;
case BTN_LEFT:
cpu_set_input_pin(PIN_K02, pin_state);
break;
case BTN_MIDDLE:
cpu_set_input_pin(PIN_K01, pin_state);
break;
case BTN_RIGHT:
cpu_set_input_pin(PIN_K00, pin_state);
break;
}
}
void hw_set_buzzer_freq(u4_t freq)
{
u32_t snd_freq = 0;
switch (freq) {
case 0:
/* 4096.0 Hz */
snd_freq = 40960;
break;
case 1:
/* 3276.8 Hz */
snd_freq = 32768;
break;
case 2:
/* 2730.7 Hz */
snd_freq = 27307;
break;
case 3:
/* 2340.6 Hz */
snd_freq = 23406;
break;
case 4:
/* 2048.0 Hz */
snd_freq = 20480;
break;
case 5:
/* 1638.4 Hz */
snd_freq = 16384;
break;
case 6:
/* 1365.3 Hz */
snd_freq = 13653;
break;
case 7:
/* 1170.3 Hz */
snd_freq = 11703;
break;
}
if (snd_freq != 0) {
g_hal->set_frequency(snd_freq);
}
}
void hw_enable_buzzer(bool_t en)
{
g_hal->play_frequency(en);
}