-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpp
190 lines (158 loc) · 5.24 KB
/
core.cpp
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
#include "common.h"
#include <avr/eeprom.h>
/* Defines */
#ifdef __AVR_ATtiny85__
#if F_CPU != 16000000UL || defined DISABLEMILLIS
#error Board Configuration is wrong...
#endif
#else
#error Sorry, Unsupported Board...
#endif
#define SimpleWire_SCL_PORT B
#define SimpleWire_SCL_POS 2
#define SimpleWire_SDA_PORT B
#define SimpleWire_SDA_POS 0
#include "SimpleWire.h"
#define SIMPLEWIRE SimpleWire<SimpleWire_1M>
#define HT16K33_ADDRESS 0x70
#define HT16K33_CMD_SETUP 0x21 // normal operation mode
#define HT16K33_CMD_DIMMING 0xe0 // brightness: 0xe0 ~ 0xef
#define HT16K33_CMD_DISPLAY 0x81 // display on & blinking off
#define HT16K33_DATA_START 0x00
#define HT16K33_DATA_LENGTH 0x10
#define MMA7455_ADDRESS 0x1D
#define MMA7455_REG_XOUTL 0x00
#define MMA7455_REG_XOUT8 0x06
#define MMA7455_REG_STATUS 0x09
#define MMA7455_REG_XOFFL 0x10
#define MMA7455_REG_MCTL 0x16
#define MMA7455_BIT_DRDY 0
#define MMA7455_BIT_MODE0 0
#define MMA7455_BIT_GLVL0 2
#define MMA7455_BIT_DRPD 6
#define FONT_W 3
#define FONT_H 4
/* Global Variables */
CONFIG_T config;
uint16_t screenBuffer[BOX_SIZE];
uint8_t counter;
/* Local Constants */
PROGMEM static const uint16_t font[] = {
00600, 02000, 01224, // -./
02552, 02223, 07243, 03427, 04754, 03437, 03571, 01247, // 01234567
07576, 04657, 02002, 01202, 04240, 06060, 02420, 02047, // 89:;<=>?
07777, 05752, 03537, 06116, 03553, 07137, 01317, 06516, // @ABCDEFG
05575, 02222, 02544, 05535, 07111, 05577, 05553, 03556, // HIJKLMNO
01353, 02756, 05353, 03636, 02227, 07555, 02555, 07755, // PQRSTUVW
05525, 02255, 07367, // XYZ
};
/* Local Variables */
static XYZ_T accel;
static uint8_t currentButton, lastButton;
/*---------------------------------------------------------------------------*/
void initCore(void)
{
SIMPLEWIRE::begin();
delay(200);
updateButtonState();
// Load configuration
eeprom_busy_wait();
eeprom_read_block(&config, (const void *)EEPROM_ADDR_CONFIG, sizeof(config));
if (config.wallType >= WALL_TYPE_MAX || config.amountType >= AMOUNT_TYPE_MAX) {
memset(&config, 0, sizeof(config));
}
// Setup matrix LED
for (uint8_t i = 0; i < BOX_SIZE / PAGE_HEIGHT; i++) {
uint8_t addr = HT16K33_ADDRESS + i;
SIMPLEWIRE::writeWithCommand(addr, HT16K33_CMD_SETUP);
SIMPLEWIRE::writeWithCommand(addr, HT16K33_CMD_DIMMING);
SIMPLEWIRE::writeWithCommand(addr, HT16K33_CMD_DISPLAY);
}
// Setup acceleration sensor
uint8_t data = bit(MMA7455_BIT_GLVL0) | bit(MMA7455_BIT_MODE0) | bit(MMA7455_BIT_DRPD);
SIMPLEWIRE::writeWithCommand(MMA7455_ADDRESS, MMA7455_REG_MCTL, &data, 1);
setAccelerationOffset(&config.offset);
clearXYZ(accel);
}
void updateButtonState(void)
{
lastButton = currentButton;
currentButton = ~PINB;
}
uint8_t isButtonPressed(uint8_t button)
{
return currentButton & button;
}
uint8_t isButtonDown(uint8_t button)
{
return currentButton & ~lastButton & button;
}
/*---------------------------------------------------------------------------*/
void clearScreen(void)
{
memset(screenBuffer, 0, sizeof(screenBuffer));
}
void drawChar(int8_t x, int8_t y, char c)
{
if (x <= -3 || x >= BOX_SIZE || y <= -4 || y >= BOX_SIZE || c < '-' || c > 'Z') return;
uint16_t ptn = pgm_read_word(&font[c - '-']);
for (uint8_t i = 0; i < 4 && y < BOX_SIZE; i++, y++) {
if (y >= 0) screenBuffer[y] |= (ptn >> (i * FONT_W) & ((1 << FONT_W) - 1)) << x;
}
}
void drawNumber(int8_t x, int8_t y, int16_t n)
{
int16_t a = abs(n);
do {
drawChar(x, y, '0' + a % 10);
a /= 10;
x -= FONT_W + 1;
} while (a > 0);
if (n < 0) drawChar(x, y, '-');
}
void drawString(int8_t y, const void *p)
{
int8_t x = 0;
char c;
while ((c = pgm_read_byte(p++)) && y <= BOX_SIZE) {
drawChar(x, y, c);
x += FONT_W + 1;
if (x >= BOX_SIZE) {
x = 0;
y += FONT_H + 1;
}
}
}
void refreshScreen(void)
{
for (uint8_t i = 0; i < BOX_SIZE / PAGE_HEIGHT; i++) {
uint8_t addr = HT16K33_ADDRESS + i;
SIMPLEWIRE::writeWithCommand(
addr, HT16K33_DATA_START, (const uint8_t *)&screenBuffer[i * PAGE_HEIGHT], HT16K33_DATA_LENGTH);
}
}
/*---------------------------------------------------------------------------*/
XYZ_T *getAcceleration(void)
{
#if 0
uint8_t ret;
do {
SIMPLEWIRE::readWithCommand(MMA7455_ADDRESS, MMA7455_REG_STATUS, &ret, 1);
} while (!bitRead(ret, MMA7455_BIT_DRDY));
#endif
if (SIMPLEWIRE::readWithCommand(MMA7455_ADDRESS, MMA7455_REG_XOUTL, (uint8_t *)&accel, sizeof(XYZ_T)) > 0) {
if (accel.reg.x_msb & 0x02) accel.reg.x_msb |= 0xfc;
if (accel.reg.y_msb & 0x02) accel.reg.y_msb |= 0xfc;
if (accel.reg.z_msb & 0x02) accel.reg.z_msb |= 0xfc;
}
return &accel;
}
void setAccelerationOffset(XYZ_T *pOffset)
{
SIMPLEWIRE::writeWithCommand(MMA7455_ADDRESS, MMA7455_REG_XOFFL, (const uint8_t *)pOffset, sizeof(XYZ_T));
}
void saveConfig(uint8_t addr, void *data, size_t len)
{
eeprom_busy_wait();
eeprom_write_block(data, (void *)addr, len);
}