Skip to content

Commit

Permalink
Merge pull request #22 from Unreal-Dan/daniel/small_cleanup
Browse files Browse the repository at this point in the history
Daniel/small cleanup
  • Loading branch information
Unreal-Dan authored Jan 18, 2024
2 parents af450db + ab36899 commit 2f86a44
Show file tree
Hide file tree
Showing 13 changed files with 2,375 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ bool Button::processPostInput()
void Button::doShortClick()
{
m_shortClick = true;
m_releaseCount++;
}

void Button::doLongClick()
{
m_longClick = true;
m_releaseCount++;
}

// this will actually press down the button, it's your responsibility to wait
Expand Down
7 changes: 3 additions & 4 deletions Helios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void Helios::handle_state_modes()
pat.play();
}
// check how long the button is held
uint16_t holdDur = (uint16_t)Button::holdDuration();
uint32_t holdDur = Button::holdDuration();
// calculate a magnitude which corresponds to how many times past the MENU_HOLD_TIME
// the user has held the button, so 0 means haven't held fully past one yet, etc
uint8_t magnitude = (uint8_t)(holdDur / MENU_HOLD_TIME);
Expand Down Expand Up @@ -506,9 +506,8 @@ bool Helios::handle_state_col_select_slot(ColorSelectOption &out_option)
} else {
Led::set(col);
}
uint16_t hold_dur = (uint16_t)Button::holdDuration();
bool deleting = ((hold_dur > DELETE_COLOR_TIME) &&
((hold_dur % (DELETE_COLOR_TIME * 2)) > DELETE_COLOR_TIME));
uint16_t mod_dur = (uint16_t)(Button::holdDuration() % (DELETE_COLOR_TIME * 2));
bool deleting = (mod_dur > DELETE_COLOR_TIME);
if (deleting) {
if (Button::isPressed()) {
// flash red
Expand Down
2 changes: 1 addition & 1 deletion HeliosCLI/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MAKE=make
RM=rm -rf
RANLIB=ranlib

CFLAGS=-O0 -g -Wall -std=c++11
CFLAGS=-O2 -g -Wall -std=c++11

# compiler defines
DEFINES=\
Expand Down
21 changes: 17 additions & 4 deletions HeliosCLI/cli_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <string>

#include "Helios.h"
#include "TimeControl.h"
#include "Storage.h"
#include "Button.h"
#include "Led.h"

Expand All @@ -26,6 +28,7 @@ OutputType output_type = OUTPUT_TYPE_COLOR;
bool in_place = false;
bool lockstep = false;
bool storage = false;
bool timestep = true;
bool eeprom = false;

// used to switch terminal to non-blocking and back
Expand All @@ -45,6 +48,10 @@ int main(int argc, char *argv[])
parse_options(argc, argv);
// set the terminal to instantly receive key presses
set_terminal_nonblocking();
// toggle timestep in the engine based on the cli input
Time::enableTimestep(timestep);
// toggle storage in the engine based on cli input
Storage::enableStorage(storage);
// run the arduino setup routine
Helios::init();
// just generate eeprom?
Expand Down Expand Up @@ -82,8 +89,9 @@ static void parse_options(int argc, char *argv[])
{"color", no_argument, nullptr, 'c'},
{"quiet", no_argument, nullptr, 'q'},
{"lockstep", no_argument, nullptr, 'l'},
{"no-timestep", no_argument, nullptr, 't'},
{"in-place", no_argument, nullptr, 'i'},
{"storage", optional_argument, nullptr, 's'},
{"no-storage", no_argument, nullptr, 's'},
{"eeprom", no_argument, nullptr, 'E'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}
Expand All @@ -105,13 +113,17 @@ static void parse_options(int argc, char *argv[])
// if the user wants to step in lockstep with the engine
lockstep = true;
break;
case 't':
// turn off timestep
timestep = false;
break;
case 'i':
// if the user wants to print in-place (on one line)
in_place = true;
break;
case 's':
// TODO: enable persistent storage to file?
storage = true;
// TODO: implement storage filename
storage = false;
break;
case 'E':
eeprom = true;
Expand Down Expand Up @@ -243,8 +255,9 @@ static void print_usage(const char* program_name)
fprintf(stderr, "\n");
fprintf(stderr, "Engine Control Flags (optional):\n");
fprintf(stderr, " -l, --lockstep Only step once each time an input is received\n");
fprintf(stderr, " -t, --no-timestep Run as fast as possible without managing timestep\n");
fprintf(stderr, " -i, --in-place Print the output in-place (interactive mode)\n");
fprintf(stderr, " -s, --storage [file] Persistent storage to file (default file: FlashStorage.flash)\n");
fprintf(stderr, " -s, --no-storage Disable persistent storage to file (FlashStorage.flash)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Other Options:\n");
fprintf(stderr, " -E, --eeprom Generate an eeprom file for flashing\n");
Expand Down
11 changes: 11 additions & 0 deletions Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
// the index of the last config byte (or first counting down)
#define CONFIG_START_INDEX 254

#ifdef HELIOS_CLI
// whether storage is enabled, default enabled
bool Storage::m_enableStorage = true;
#endif

bool Storage::init()
{
#ifdef HELIOS_CLI
Expand Down Expand Up @@ -127,6 +132,9 @@ void Storage::write_byte(uint8_t address, uint8_t data)
/* Start eeprom write by setting EEPE */
EECR |= (1<<EEPE);
#else // HELIOS_CLI
if (!m_enableStorage) {
return;
}
FILE *f = fopen(STORAGE_FILENAME, "r+b");
if (!f) {
if (errno != ENOENT) {
Expand Down Expand Up @@ -166,6 +174,9 @@ uint8_t Storage::read_byte(uint8_t address)
/* Return data from data register */
return EEDR;
#else
if (!m_enableStorage) {
return 0;
}
uint8_t val = 0;
if (!access(STORAGE_FILENAME, O_RDONLY)) {
return val;
Expand Down
9 changes: 9 additions & 0 deletions Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Storage
static uint8_t read_config(uint8_t index);
static void write_config(uint8_t index, uint8_t val);

#ifdef HELIOS_CLI
// toggle storage on/off
static void enableStorage(bool enabled) { m_enableStorage = enabled; }
#endif
private:
static uint8_t crc8(uint8_t pos, uint8_t size);
static uint8_t crc_pos(uint8_t pos);
Expand All @@ -28,6 +32,11 @@ class Storage
static void write_crc(uint8_t pos);
static void write_byte(uint8_t address, uint8_t data);
static uint8_t read_byte(uint8_t address);

#ifdef HELIOS_CLI
// whether storage is enabled
static bool m_enableStorage;
#endif
};

#endif
8 changes: 8 additions & 0 deletions TimeControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ uint32_t Time::m_curTick = 0;
// the last frame timestamp
uint32_t Time::m_prevTime = 0;

#ifdef HELIOS_CLI
// whether timestep is enabled, default enabled
bool Time::m_enableTimestep = true;
#endif

bool Time::init()
{
m_prevTime = microseconds();
Expand All @@ -45,6 +50,9 @@ void Time::tickClock()
m_curTick++;

#ifdef HELIOS_CLI
if (!m_enableTimestep) {
return;
}
// the rest of this only runs inside vortexlib because on the duo the tick runs in the
// tcb timer callback instead of in a busy loop constantly checking microseconds()
// perform timestep
Expand Down
10 changes: 10 additions & 0 deletions TimeControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,21 @@ class Time
static void delayMicroseconds(uint32_t us);
static void delayMilliseconds(uint32_t ms);

#ifdef HELIOS_CLI
// toggle timestep on/off
static void enableTimestep(bool enabled) { m_enableTimestep = enabled; }
#endif

private:
// global tick counter
static uint32_t m_curTick;
// the last frame timestamp
static uint32_t m_prevTime;

#ifdef HELIOS_CLI
// whether timestep is enabled
static bool m_enableTimestep;
#endif
};

#endif
Expand Down
Loading

0 comments on commit 2f86a44

Please sign in to comment.