summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2016-01-14 15:42:55 +0100
committerDaniel Friesel <derf@finalrewind.org>2016-01-14 15:42:55 +0100
commit4399a137a900a7d4bcbc862d85c87c92dd27d89b (patch)
tree77fcf9caaac0c740402fd9525d4191de79c10bd1
parent3ee9280284b1d9de2a2c675aa2fb251e02b98c78 (diff)
C++ test (TODO: distinguish between C/C++ in Makefile, use one file per class)
-rw-r--r--Makefile10
-rw-r--r--main.cc (renamed from main.c)139
2 files changed, 90 insertions, 59 deletions
diff --git a/Makefile b/Makefile
index bb94e73..2395797 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
MCU ?= attiny88
AVRDUDE_PROGRAMMER ?= usbasp
-AVRCC ?= avr-gcc
+AVRCC ?= avr-g++
AVRFLASH ?= avrdude
AVRNM ?= avr-nm
AVROBJCOPY ?= avr-objcopy
@@ -9,9 +9,9 @@ AVROBJDUMP ?= avr-objdump
CFLAGS += -mmcu=attiny88 -DF_CPU=8000000UL
# CFLAGS += -gdwarf-2
-CFLAGS += -I. -std=gnu99 -Os -Wall -Wextra -pedantic
+CFLAGS += -std=c++11 -I. -Os -Wall -Wextra -pedantic
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
-CFLAGS += -fwhole-program -flto -mstrict-X
+CFLAGS += -fwhole-program -flto -fno-rtti -fno-exceptions -mstrict-X
AVRFLAGS += -U lfuse:w:0xee:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m
AVRFLAGS += -U flash:w:main.hex
@@ -24,8 +24,8 @@ AVRFLAGS += -U flash:w:main.hex
${AVROBJCOPY} -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex $< $@
-main.elf: main.c
- ${AVRCC} ${CFLAGS} -o $@ ${@:.elf=.c} -Wl,-Map=main.map,--cref
+main.elf: main.cc
+ ${AVRCC} ${CFLAGS} -o $@ ${@:.elf=.cc} -Wl,-Map=main.map,--cref
@echo
@avr-size --format=avr --mcu=${MCU} $@
diff --git a/main.c b/main.cc
index ab9df5f..ee80936 100644
--- a/main.c
+++ b/main.cc
@@ -10,10 +10,89 @@
volatile uint8_t disp[8];
-int main (void)
+class System {
+ private:
+ uint16_t want_shutdown;
+ public:
+ System() { want_shutdown = 0; };
+ void loop(void);
+ void shutdown(void);
+};
+
+class Display {
+ public:
+ Display() {};
+ void turn_on(void);
+ void turn_off(void);
+};
+
+System system;
+Display display;
+
+void Display::turn_off()
+{
+ TIMSK0 &= ~_BV(TOIE0);
+ PORTB = 0;
+ PORTD = 0;
+}
+
+void Display::turn_on()
{
- uint16_t want_shutdown = 0;
+ TIMSK0 |= _BV(TOIE0);
+}
+
+void System::loop()
+{
+ // both buttons are pressed
+ if ((PINC & (_BV(PC3) | _BV(PC7))) == 0) {
+ // naptime!
+ // But not before both buttons have been pressed for
+ // SHUTDOWN_THRESHOLD * 0.256 ms. And then, not before both have
+ // been released, because otherwise we'd go te sleep when
+ // they're pressed and wake up when they're released, which
+ // isn't really the point here.
+
+ if (want_shutdown < SHUTDOWN_THRESHOLD) {
+ want_shutdown++;
+ }
+ else {
+
+ // turn off display to indicate we're about to shut down
+ display.turn_off();
+
+ // wait until both buttons are released
+ while (!((PINC & _BV(PC3)) && (PINC & _BV(PC7)))) ;
+
+ // and some more to debounce the buttons
+ _delay_ms(10);
+
+ // actual naptime
+
+ // enable PCINT on PC3 (PCINT11) and PC7 (PCINT15) for wakeup
+ PCMSK1 |= _BV(PCINT15) | _BV(PCINT11);
+ PCICR |= _BV(PCIE1);
+
+ // go to power-down mode
+ SMCR = _BV(SM1) | _BV(SE);
+ asm("sleep");
+
+ // execution will resume here - disable PCINT again.
+ // Don't disable PCICR, something else might need it.
+ PCMSK1 &= ~(_BV(PCINT15) | _BV(PCINT11));
+
+ // turn on display
+ display.turn_on();
+
+ want_shutdown = 0;
+ }
+ }
+ else {
+ want_shutdown = 0;
+ }
+}
+int main (void)
+{
// disable ADC to save power
PRR |= _BV(PRADC);
@@ -85,55 +164,7 @@ int main (void)
// nothing to do here, go to idle to save power
SMCR = _BV(SE);
asm("sleep");
-
- // both buttons are pressed
- if ((PINC & (_BV(PC3) | _BV(PC7))) == 0) {
- // naptime!
- // But not before both buttons have been pressed for
- // SHUTDOWN_THRESHOLD * 0.256 ms. And then, not before both have
- // been released, because otherwise we'd go te sleep when
- // they're pressed and wake up when they're released, which
- // isn't really the point here.
-
- if (want_shutdown < SHUTDOWN_THRESHOLD) {
- want_shutdown++;
- }
- else {
-
- // turn off display to indicate we're about to shut down
- TIMSK0 &= ~_BV(TOIE0);
- PORTB = 0;
- PORTD = 0;
-
- // wait until both buttons are released
- while (!((PINC & _BV(PC3)) && (PINC & _BV(PC7)))) ;
-
- // and some more to debounce the buttons
- _delay_ms(10);
-
- // actual naptime
-
- // enable PCINT on PC3 (PCINT11) and PC7 (PCINT15) for wakeup
- PCMSK1 |= _BV(PCINT15) | _BV(PCINT11);
- PCICR |= _BV(PCIE1);
-
- // go to power-down mode
- SMCR = _BV(SM1) | _BV(SE);
- asm("sleep");
-
- // execution will resume here - disable PCINT again.
- // Don't disable PCICR, something else might need it.
- PCMSK1 &= ~(_BV(PCINT15) | _BV(PCINT11));
-
- // turn on display
- TIMSK0 |= _BV(TOIE0);
-
- want_shutdown = 0;
- }
- }
- else {
- want_shutdown = 0;
- }
+ system.loop();
}
return 0;
@@ -153,7 +184,7 @@ ISR(TIMER0_OVF_vect)
static uint16_t scroll = 0;
static uint8_t disp_offset = 0;
- static uint8_t display[8];
+ static uint8_t disp_buf[8];
uint8_t i;
@@ -164,7 +195,7 @@ ISR(TIMER0_OVF_vect)
}
for (i = 0; i < 8; i++) {
- display[i] = ~disp[(disp_offset + i) % sizeof(disp)];
+ disp_buf[i] = ~disp[(disp_offset + i) % sizeof(disp)];
}
}
@@ -173,7 +204,7 @@ ISR(TIMER0_OVF_vect)
* calculations) between the following three lines.
*/
PORTB = 0;
- PORTD = display[active_col];
+ PORTD = disp_buf[active_col];
PORTB = _BV(active_col);
if (++active_col == 8)