Skip to content

Commit

Permalink
Add display dimming on idle
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Mihálek <[email protected]>
  • Loading branch information
aenniw committed Nov 17, 2018
1 parent 47d32c4 commit faa06d6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/Grove 4-Digit Display_ID5064/TM1637.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void TM1637::set(uint8_t brightness, uint8_t SetData, uint8_t SetAddr)
{
Cmd_SetData = SetData;
Cmd_SetAddr = SetAddr;
Cmd_DispCtrl = 0x88 + brightness; //Set the brightness and it takes effect the next time it displays.
Cmd_DispCtrl = 0x87 + brightness; //Set the brightness and it takes effect the next time it displays.
}

//Whether to light the clock point ":".
Expand Down
6 changes: 3 additions & 3 deletions lib/Grove 4-Digit Display_ID5064/TM1637.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
#define POINT_ON 1
#define POINT_OFF 0
/**************definitions for brightness***********************/
#define BRIGHT_LOW 0
#define BRIGHT_MEDIUM 2
#define BRIGHT_HIGH 7
#define BRIGHT_LOW 1
#define BRIGHT_MEDIUM 3
#define BRIGHT_HIGH 8

class TM1637
{
Expand Down
4 changes: 3 additions & 1 deletion skarsta/lib/buttons/Buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ ToggleButton::ToggleButton(uint8_t button, void (*on)(), void (*off)())
registerIsr(button, this);
}

TimedButton::TimedButton(uint8_t button, unsigned int delay, void (*short_press)(), void (*long_press)())
TimedButton::TimedButton(uint8_t button, unsigned int delay, void (*short_press)(), void (*long_press)(), void (*on)())
{
this->button = button;
this->delay = delay;
this->long_press = long_press;
this->short_press = short_press;
this->on = on;
registerIsr(button, this);
}

Expand Down Expand Up @@ -78,6 +79,7 @@ void TimedButton::isr()
button_state = digitalRead(button) == LOW;
if (button_state)
{
if (this->on) this->on();
msg_time = millis();
last_state = true;
return;
Expand Down
7 changes: 6 additions & 1 deletion skarsta/lib/buttons/Buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ class TimedButton : Button
unsigned int delay;
uint8_t button;

void (*on)();

void (*short_press)();

void (*long_press)();

public:
TimedButton(uint8_t button, unsigned int delay, void (*short_press)(), void (*long_press)());
TimedButton(uint8_t button, unsigned int delay, void (*short_press)(), void (*long_press)())
: TimedButton(button, delay, short_press, long_press, nullptr) {}

TimedButton(uint8_t button, unsigned int delay, void (*short_press)(), void (*long_press)(), void (*on)());

bool get_state();

Expand Down
39 changes: 32 additions & 7 deletions skarsta/lib/display/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static int8_t get_code_c(char c)
Display::Display(uint8_t _pin1, uint8_t _pin2)
{
display = new TM1637(_pin1, _pin2);
display->set();
display->set(brightness);
}

void Display::set_blink(bool state)
Expand All @@ -51,7 +51,7 @@ void Display::set_blink(bool state)
blink = state;
}

void Display::display_print(unsigned int position)
void Display::print(unsigned int position)
{
int8_t buffer[4] = {
get_code_n(position / 1000),
Expand All @@ -73,7 +73,7 @@ void Display::display_print(unsigned int position)
#endif
}

void Display::display_print(const char *text)
void Display::print(const char *text)
{
const uint8_t len = strlen(text);
int8_t buffer[4] = {
Expand All @@ -96,24 +96,40 @@ void Display::display_print(const char *text)
#endif
}

void Display::set_brightness(uint8_t b)
{
if (brightness == b)
return;
brightness = b;
#ifdef __DEBUG__
Serial.print("brightness: ");
Serial.println(b);
#endif
display->set(b);
}

void Display::light_up() {
if (!this->blink)
this->dirty = true;
}

void Display::cycle()
{
static unsigned long last_tick = millis();
unsigned long now = millis(), diff = get_period(last_tick, now);

if (dirty)
{
this->set_brightness(BRIGHT_HIGH);
display->display(disp_buffer, true);
dirty = false;

last_tick = now;
#ifdef __DEBUG__
Serial.println("redraw");
#endif
}
else if (blink)
{
unsigned long now = millis(),
diff = get_period(last_tick, now);

if (diff >= 500)
{
#ifdef __DEBUG__
Expand All @@ -131,10 +147,19 @@ void Display::cycle()
}
else
{
this->set_brightness(BRIGHT_HIGH);
display->display(disp_buffer, true);
}
clear = !clear;
last_tick = now;
}
}
else if (brightness != 0 && diff >= FADE_TIMEOUT)
{
this->set_brightness((uint8_t)(8 - ((diff - FADE_TIMEOUT) / 10000)));
if (brightness != 0)
display->display(disp_buffer, true);
else
display->clearDisplay();
}
}
12 changes: 10 additions & 2 deletions skarsta/lib/display/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@
#include <Service.h>
#include <TM1637.h>

#define FADE_TIMEOUT 60000

class Display : Service
{
private:
TM1637 *display = nullptr;
uint8_t brightness = BRIGHT_HIGH;
int8_t disp_buffer[4] = {0x00, 0x00, 0x00, 0x00};
bool dirty = false, blink = false, clear = false;

protected:
void set_brightness(uint8_t b);

public:
Display(uint8_t _pin1, uint8_t _pin2);

void set_blink(bool state);

void display_print(unsigned int position);
void light_up();

void print(unsigned int position);

void display_print(const char *text);
void print(const char *text);

void cycle() override;

Expand Down
26 changes: 15 additions & 11 deletions skarsta/lib/keypad/Keypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ static Motor *motor = nullptr;
static Display *display = nullptr;
unsigned int preset_values[3] = {0u};

#define PRESET_BUTTON(pin, p) new TimedButton(pin, SAVE_BUTTON_TIMEOUT, []() { if (!keypad->stop_motor()) keypad->goto_preset(p); }, []() { keypad->set_preset(p); })
#define SET_PRESET_BUTTON(pin, p) preset_buttons[p] = PRESET_BUTTON(pin, p)
#define PRESET_BUTTON(pin, p, on) new TimedButton(pin, SAVE_BUTTON_TIMEOUT, []() { if (!keypad->stop_motor()) keypad->goto_preset(p); }, []() { keypad->set_preset(p); },on)
#define SET_PRESET_BUTTON(pin, p, on) preset_buttons[p] = PRESET_BUTTON(pin, p,on)

Keypad::Keypad(Motor *_motor, Display *_display)
{
Expand All @@ -31,10 +31,12 @@ Keypad::Keypad(Motor *_motor, Display *_display)
// up/down
down = new ToggleButton(BUTTON_DOWN, []() { if (!keypad->stop_motor()) motor->dir_ccw(); }, []() { motor->off(); });
up = new ToggleButton(BUTTON_UP, []() { if (!keypad->stop_motor()) motor->dir_cw(); }, []() { motor->off(); });
// display light-up callback
auto light_up = []() { display->light_up(); };
// presets
SET_PRESET_BUTTON(BUTTON_P0, 0);
SET_PRESET_BUTTON(BUTTON_P1, 1);
SET_PRESET_BUTTON(BUTTON_P2, 2);
SET_PRESET_BUTTON(BUTTON_P0, 0, light_up);
SET_PRESET_BUTTON(BUTTON_P1, 1, light_up);
SET_PRESET_BUTTON(BUTTON_P2, 2, light_up);
// calibration
rst = new TimedButton(BUTTON_RST, RST_BUTTON_TIMEOUT, []() { keypad->stop_motor(); }, []() {
switch (motor->get_mode()) {
Expand All @@ -59,7 +61,8 @@ Keypad::Keypad(Motor *_motor, Display *_display)
display->set_blink(true);
motor->reset_position();
break;
} });
}
}, light_up);
}

void Keypad::cycle()
Expand All @@ -69,15 +72,15 @@ void Keypad::cycle()
if (rst->get_state() && !rst->is_short())
{
display->set_blink(false);
display->display_print("-rst");
display->print("-rst");
return;
}
for (auto &preset_button : preset_buttons)
{
if (preset_button->get_state() && !preset_button->is_short())
{
display->set_blink(false);
display->display_print("-set");
display->print("-set");
return;
}
}
Expand All @@ -91,11 +94,11 @@ void Keypad::cycle()
display->set_blink(true);

if (motor->get_mode() != UNCALIBRATED)
display->display_print(motor->get_position());
display->print(motor->get_position());
else if (motor->get_mode() == UNCALIBRATED)
{
display->set_blink(true);
display->display_print(DISPLAY_NONE);
display->print(DISPLAY_NONE);
}
}

Expand Down Expand Up @@ -139,7 +142,8 @@ Keypad::~Keypad()
delete preset_button;
}

bool Keypad::stop_motor() {
bool Keypad::stop_motor()
{
if (motor->get_state() == OFF)
return false;
motor->off();
Expand Down

0 comments on commit faa06d6

Please sign in to comment.