#include "SmartResponseXEBoard.h" #include "SmartResponseXE.h" #include #include #include #include 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(); } }