aboutsummaryrefslogtreecommitdiff
path: root/variants/smartresponse_xe/SmartResponseXEBoard.cpp
blob: 6f0d92eb3867d3988630d410529e45b2079099b6 (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
#include "SmartResponseXEBoard.h"

#include "SmartResponseXE.h"

#include <stdio.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

void SPI_Init(void);

#ifndef SRXE_LCD_CS
#define SRXE_LCD_CS 0xE7
#endif

#ifndef SRXE_LCD_DC
#define SRXE_LCD_DC 0xD6
#endif

#ifndef SRXE_LCD_RST
#define SRXE_LCD_RST 0xA2
#endif

namespace {
const uint8_t DISPLAY_LINE_HEIGHT = 8;
// Stop at x=348 so line updates do not erase the right-hand softkey strip.
const uint8_t DISPLAY_LINE_MAX_CHARS = 58;
const uint8_t POWER_BUTTON_PIN = 0xD2;

void enableAdc() {
#if defined(PRR0) && defined(PRADC)
  PRR0 &= ~_BV(PRADC);
#elif defined(PRR) && defined(PRADC)
  PRR &= ~_BV(PRADC);
#endif

#if defined(ADCSRA) && defined(ADEN)
  ADCSRA = _BV(ADEN);
  #if defined(ADPS2)
    ADCSRA |= _BV(ADPS2);
  #endif
  #if defined(ADPS1)
    ADCSRA |= _BV(ADPS1);
  #endif
  #if defined(ADPS0)
    ADCSRA |= _BV(ADPS0);
  #endif
#endif
}

void disableAdc() {
#if defined(ADCSRA) && defined(ADEN)
  ADCSRA &= ~_BV(ADEN);
#endif

#if defined(PRR0) && defined(PRADC)
  PRR0 |= _BV(PRADC);
#elif defined(PRR) && defined(PRADC)
  PRR |= _BV(PRADC);
#endif
}

void configurePowerReduction() {
  disableAdc();

#if defined(ACSR) && defined(ACD)
  ACSR |= _BV(ACD);
#endif

#if defined(PRR0)
  uint8_t prr0_mask = 0;
  #if defined(PRTWI)
    prr0_mask |= _BV(PRTWI);
  #endif
  #if defined(PRTIM2)
    prr0_mask |= _BV(PRTIM2);
  #endif
  #if defined(PRTIM1)
    prr0_mask |= _BV(PRTIM1);
  #endif
  #if defined(PRUSART0)
    prr0_mask |= _BV(PRUSART0);
  #endif
  PRR0 |= prr0_mask;
  #if defined(PRTIM0)
    PRR0 &= ~_BV(PRTIM0);
  #endif
  #if defined(PRSPI)
    PRR0 &= ~_BV(PRSPI);
  #endif
#elif defined(PRR)
  uint8_t prr_mask = 0;
  #if defined(PRTWI)
    prr_mask |= _BV(PRTWI);
  #endif
  #if defined(PRTIM2)
    prr_mask |= _BV(PRTIM2);
  #endif
  #if defined(PRTIM1)
    prr_mask |= _BV(PRTIM1);
  #endif
  #if defined(PRUSART0)
    prr_mask |= _BV(PRUSART0);
  #endif
  PRR |= prr_mask;
  #if defined(PRTIM0)
    PRR &= ~_BV(PRTIM0);
  #endif
  #if defined(PRSPI)
    PRR &= ~_BV(PRSPI);
  #endif
#endif

#if defined(PRR1)
  uint8_t prr1_mask = 0;
  #if defined(PRUSART1)
    prr1_mask |= _BV(PRUSART1);
  #endif
  #if defined(PRUSART2)
    prr1_mask |= _BV(PRUSART2);
  #endif
  #if defined(PRUSART3)
    prr1_mask |= _BV(PRUSART3);
  #endif
  #if defined(PRTIM3)
    prr1_mask |= _BV(PRTIM3);
  #endif
  #if defined(PRTIM4)
    prr1_mask |= _BV(PRTIM4);
  #endif
  #if defined(PRTIM5)
    prr1_mask |= _BV(PRTIM5);
  #endif
  #if defined(PRTRX24)
    prr1_mask |= _BV(PRTRX24);
  #endif
  PRR1 |= prr1_mask;
#endif
}
}

void SmartResponseXEBoard::begin() {
  startup_reason = BD_STARTUP_NORMAL;
  configurePowerReduction();
  mypinMode(POWER_BUTTON_PIN, INPUT_PULLUP);
#ifdef LED_BUILTIN
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
#endif
}

bool SmartResponseXEBoard::ensureDisplayInit() const {
  if (display_initialized) {
    return display_available;
  }

  display_initialized = true;
  display_available = (SRXEInit(SRXE_LCD_CS, SRXE_LCD_DC, SRXE_LCD_RST) == 0);
  return display_available;
}

uint16_t SmartResponseXEBoard::getBattMilliVolts() {
#ifdef PIN_VBAT_READ
  enableAdc();
  // The first conversion after powering the ADC can still reflect the previous
  // multiplexer/sample-and-hold state.
  (void)analogRead(PIN_VBAT_READ);
  long total = 0;
  for (uint8_t i = 0; i < 4; ++i) {
    total += analogRead(PIN_VBAT_READ);
  }
  disableAdc();
  long raw = total / 4;
#ifdef ADC_MULTIPLIER_MV
  return (uint16_t)((raw * ADC_MULTIPLIER_MV) / 1023L);
#else
  return (uint16_t)((raw * 3300L) / 1023L);
#endif
#else
  return 0;
#endif
}

void SmartResponseXEBoard::reboot() {
  wdt_enable(WDTO_15MS);
  while (1) {
  }
}

int SmartResponseXEBoard::readKey() const {
  int key = SRXEGetKey();
  if (key != 0) {
    return key;
  }
  return 0;
}

bool SmartResponseXEBoard::isPowerButtonPressed() const {
  return mydigitalRead(POWER_BUTTON_PIN) == LOW;
}

bool SmartResponseXEBoard::beginDisplay() const {
  return ensureDisplayInit();
}

void SmartResponseXEBoard::clearDisplay() const {
  if (!ensureDisplayInit()) {
    return;
  }
  SPI_Init();
  SRXEFill(0);
}

void SmartResponseXEBoard::writeDisplayLine(uint8_t row, const char* text) const {
  if (!ensureDisplayInit()) {
    return;
  }

  char line[DISPLAY_LINE_MAX_CHARS + 1];
  uint8_t i = 0;
  while (text && text[i] && i < DISPLAY_LINE_MAX_CHARS) {
    line[i] = text[i];
    ++i;
  }
  while (i < DISPLAY_LINE_MAX_CHARS) {
    line[i++] = ' ';
  }
  line[DISPLAY_LINE_MAX_CHARS] = 0;

  SPI_Init();
  SRXEWriteString(0, row * DISPLAY_LINE_HEIGHT, line, FONT_SMALL, 3, 0);
}

void SmartResponseXEBoard::writeDisplayText(uint16_t x, uint8_t y, const char* text) const {
  if (!ensureDisplayInit()) {
    return;
  }

  char line[DISPLAY_LINE_MAX_CHARS + 1];
  uint8_t i = 0;
  while (text && text[i] && i < DISPLAY_LINE_MAX_CHARS) {
    line[i] = text[i];
    ++i;
  }
  line[i] = 0;

  SPI_Init();
  SRXEWriteString((int)x, y, line, FONT_SMALL, 3, 0);
}

void SmartResponseXEBoard::renderDisplay() const {
}

void SmartResponseXEBoard::idleSleep() const {
  if ((SREG & _BV(SREG_I)) == 0) {
    return;
  }
  set_sleep_mode(SLEEP_MODE_IDLE);
  sleep_enable();
  sleep_cpu();
  sleep_disable();
}

void SmartResponseXEBoard::sleep(uint32_t secs) {
  (void)secs;
  if (display_initialized && display_available) {
    SRXESleep();
  } else {
    ensureDisplayInit();
    SRXESleep();
  }
}