Skip to content

Commit

Permalink
Merge pull request #242: Issue 240: Some code restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
boerge1 authored Oct 13, 2024
2 parents 93e4881 + da512f8 commit 2901fb6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Die SD Karte (Ordner mp3 und advert) hat sich gegenüber der Version 3.1.11 geä
# Change Log

## Version 3.2.1 (12.10.2024)
- [Issue 240](https://github.com/tonuino/TonUINO-TNG/issues/240): Some code restructuring
- [Issue 239](https://github.com/tonuino/TonUINO-TNG/issues/239): Starting Quiz or Memory game the "pling" is missing
- [Issue 236](https://github.com/tonuino/TonUINO-TNG/issues/236): Improve hardware diagnostic on startup

Expand Down
6 changes: 3 additions & 3 deletions src/rotary_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

volatile int8_t RotaryEncoder::pos = 0;

#ifdef ROTARY_ENCODER_USES_TIMER1
#ifdef ROTARY_ENCODER_USES_TIMER
volatile uint8_t RotaryEncoder::clk = 0;

void RotaryEncoder::timer_loop() {
Expand Down Expand Up @@ -39,9 +39,9 @@ RotaryEncoder::RotaryEncoder(const Settings& settings)
pinMode(rotaryEncoderClkPin, INPUT_PULLUP);
pinMode(rotaryEncoderDtPin , INPUT_PULLUP);

#ifndef ROTARY_ENCODER_USES_TIMER1
#ifndef ROTARY_ENCODER_USES_TIMER
attachInterrupt(digitalPinToInterrupt(rotaryEncoderClkPin), RotaryEncoder::changed, FALLING);
#endif // ROTARY_ENCODER_USES_TIMER1
#endif // ROTARY_ENCODER_USES_TIMER
}

commandRaw RotaryEncoder::getCommandRaw() {
Expand Down
8 changes: 4 additions & 4 deletions src/rotary_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "timer.hpp"

#if not defined(ALLinONE_Plus) and not defined(TonUINO_Every) and not defined(TonUINO_Every_4808)
#define USE_TIMER1
#define ROTARY_ENCODER_USES_TIMER1
#define USE_TIMER
#define ROTARY_ENCODER_USES_TIMER
#endif

class RotaryEncoder: public CommandSource {
Expand All @@ -22,7 +22,7 @@ class RotaryEncoder: public CommandSource {
commandRaw getCommandRaw() override;

static void changed();
#ifdef USE_TIMER1
#ifdef USE_TIMER
static void timer_loop();
#endif
private:
Expand All @@ -32,7 +32,7 @@ class RotaryEncoder: public CommandSource {
const Settings& settings;

volatile static int8_t pos;
#ifdef USE_TIMER1
#ifdef USE_TIMER
volatile static uint8_t clk;
#endif

Expand Down
90 changes: 65 additions & 25 deletions src/tonuino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,36 @@ const __FlashStringHelper* str_bis () { return F(" bis "); }

} // anonymous namespace

#ifdef USE_TIMER1
#ifdef USE_TIMER
#if defined(ALLinONE_Plus) or defined(TonUINO_Every) or defined(TonUINO_Every_4808)
ISR(TCA0_OVF_vect) {
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm;
#else // classic with nano, AiO
ISR(TIMER1_COMPA_vect){
TCNT1 = 0;
#ifdef ROTARY_ENCODER_USES_TIMER1
#endif

#ifdef ROTARY_ENCODER_USES_TIMER
RotaryEncoder::timer_loop();
#endif
}
#endif

void Tonuino::setup() {
#ifdef USE_TIMER1
cli();//stop interrupts

TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0;
OCR1A = 10000U; // set timer count for frequency = (16*10^6) / (F*8) - 1, F=200Hz
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS11); // Set CS11 bit for 8 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt

sei();//allow interrupts
#ifdef USE_TIMER
setup_timer();
#endif

#if defined(BUTTONS3X3) or defined(BAT_VOLTAGE_MEASUREMENT)
#if defined(ALLinONE_Plus) or defined(TonUINO_Every) or defined(TonUINO_Every_4808)
analogReference(INTERNAL2V5);
#endif
#ifdef ALLinONE
analogReference(INTERNAL2V048);
analogReadResolution(12);
#endif
setup_adc();
#endif

pinMode(shutdownPin , OUTPUT);
digitalWrite(shutdownPin, getLevel(shutdownPinType, level::inactive));

randomSeed(generateRamdomSeed());

// speaker switch off
#if defined SPKONOFF
pinMode(ampEnablePin, OUTPUT);
digitalWrite(ampEnablePin, getLevel(ampEnablePinType, level::inactive));
Expand Down Expand Up @@ -88,23 +79,25 @@ void Tonuino::setup() {
LOG(init_log, s_debug, F("get last, folder: "), myFolder.folder, F(", mode: "), static_cast<uint8_t>(myFolder.mode));
#endif

// NFC Leser initialisieren
// init NFC reader
chip_card.initCard();

// RESET --- ALLE DREI KNÖPFE BEIM STARTEN GEDRÜCKT HALTEN -> alle EINSTELLUNGEN werden gelöscht
// RESET flash if all buttons pressed
if (buttons.isReset()) {
settings.clearEEPROM();
settings.loadSettingsFromFlash();
}

// DFPlayer Mini initialisieren
// init DFPlayer Mini
mp3.init();

// init state machine
SM_tonuino::start();

// ignore commands, if buttons already pressed during startup
commands.getCommandRaw();

// speaker switch on
#if defined SPKONOFF
digitalWrite(ampEnablePin, getLevel(ampEnablePinType, level::active));
#endif
Expand All @@ -126,9 +119,51 @@ void Tonuino::setup() {

#endif // SPECIAL_START_SHORTCUT

// play start track
SM_tonuino::dispatch(command_e(commandRaw::start));
}

#ifdef USE_TIMER
void Tonuino::setup_timer() {
// enable timer with 200 Hz (5 ms)
#if defined(ALLinONE_Plus) or defined(TonUINO_Every) or defined(TonUINO_Every_4808)
//ATMega4809 runs at 16MHz without Main Clock Prescaller and default is TCA_SINGLE_CLKSEL_DIV64_gc
//4us per tick

TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm; //enable overflow interrupt
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc;//timer mode may break pwm

//5ms = 1250 x .000004 set H and L period registers
TCA0.SINGLE.PERL = lowByte(1250);
TCA0.SINGLE.PERH = highByte(1250);

TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm;
#else // classic with nano, AiO
cli();//stop interrupts

TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0;
OCR1A = 10000U; // set timer count for frequency = (16*10^6) / (F*8) - 1, F=200Hz
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS11); // Set CS11 bit for 8 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt

sei();//allow interrupts
#endif
}
#endif // USE_TIMER

void Tonuino::setup_adc() {
#if defined(ALLinONE_Plus) or defined(TonUINO_Every) or defined(TonUINO_Every_4808)
analogReference(INTERNAL2V5);
#endif
#ifdef ALLinONE
analogReference(INTERNAL2V048);
analogReadResolution(12);
#endif
}

void Tonuino::loop() {

unsigned long start_cycle = millis();
Expand Down Expand Up @@ -274,6 +309,11 @@ void Tonuino::playFolder() {
}
break;

case pmode_t::quiz_game:
case pmode_t::memory_game:
// Nothing to enqueue here
break;

default:
break;
}
Expand Down Expand Up @@ -317,7 +357,7 @@ void Tonuino::previousTrack(uint8_t tracks) {
}
}

// Funktionen für den Standby Timer (z.B. über Pololu-Switch oder Mosfet)
// functions for the standby timer (e.g.. with Pololu-Switch or Mosfet)
void Tonuino::setStandbyTimer() {
LOG(standby_log, s_debug, F("setStandbyTimer"));
if (settings.standbyTimer != 0 && not standbyTimer.isActive()) {
Expand Down
3 changes: 3 additions & 0 deletions src/tonuino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class Tonuino {

private:

void setup_timer();
void setup_adc();

void checkStandby();

bool specialCard(const folderSettings &nfcTag);
Expand Down

0 comments on commit 2901fb6

Please sign in to comment.