Skip to content

Commit 2e08d43

Browse files
author
Samuel Sieb
committed
unlimited buffer and continuous option
1 parent c1d639f commit 2e08d43

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

components/ht16k33_alpha/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ display:
3030
3131
`scroll:` defaults to false
3232

33+
`continuous:` is whether the start follows immediately after the end in a continuous loop, defaults to false
34+
3335
`scroll_speed:` is the time between each movement, default 250ms
3436

3537
`scroll_dwell:` is the time to wait at the end before going back to the start, default 2s

components/ht16k33_alpha/display.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
ht16k33_alpha_ns = cg.esphome_ns.namespace('ht16k33_alpha')
99
HT16K33AlphaDisplay = ht16k33_alpha_ns.class_('HT16K33AlphaDisplay', cg.PollingComponent, i2c.I2CDevice)
1010

11+
CONF_CONTINUOUS = "continuous"
1112
CONF_SCROLL = "scroll"
1213
CONF_SCROLL_SPEED = "scroll_speed"
1314
CONF_SCROLL_DWELL = "scroll_dwell"
@@ -20,6 +21,7 @@
2021

2122
CONFIG_SCHEMA = display.BASIC_DISPLAY_SCHEMA.extend({
2223
cv.GenerateID(): cv.declare_id(HT16K33AlphaDisplay),
24+
cv.Optional(CONF_CONTINUOUS, default=False): cv.boolean,
2325
cv.Optional(CONF_SCROLL, default=False): cv.boolean,
2426
cv.Optional(CONF_SCROLL_SPEED, default='250ms'): cv.positive_time_period_milliseconds,
2527
cv.Optional(CONF_SCROLL_DWELL, default='2s'): cv.positive_time_period_milliseconds,
@@ -39,6 +41,7 @@ async def to_code(config):
3941
cg.add(var.set_writer(lambda_))
4042
if config[CONF_SCROLL]:
4143
cg.add(var.set_scroll(True))
44+
cg.add(var.set_continuous(config[CONF_CONTINUOUS]))
4245
cg.add(var.set_scroll_speed(config[CONF_SCROLL_SPEED]))
4346
cg.add(var.set_scroll_dwell(config[CONF_SCROLL_DWELL]))
4447
cg.add(var.set_scroll_delay(int(config[CONF_SCROLL_DELAY] * config[CONF_SCROLL_SPEED].total_milliseconds)))

components/ht16k33_alpha/ht16k33_display.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,24 @@ void HT16K33AlphaDisplay::setup() {
2626
display->write_bytes(DISPLAY_COMMAND_DISPLAY_ON, nullptr, 0);
2727
}
2828
this->set_brightness(1);
29-
memset(this->buffer_, 0, 64);
3029
}
3130

3231
void HT16K33AlphaDisplay::loop() {
3332
unsigned long now = millis();
3433
int numc = this->displays_.size() * 8;
35-
// check if the buffer has shrunk past the current position since last update
36-
if (this->offset_ + numc > this->buffer_fill_) {
37-
this->offset_ = std::max(this->buffer_fill_ - numc, 0);
38-
this->display_();
39-
}
40-
if (!this->scroll_ || (this->buffer_fill_ <= numc))
34+
int len = this->buffer_.size();
35+
if (!this->scroll_ || (len <= numc))
4136
return;
4237
if ((this->offset_ == 0) && (now - this->last_scroll_ < this->scroll_delay_))
4338
return;
44-
if (this->offset_ + numc >= this->buffer_fill_) {
45-
if (now - this->last_scroll_ >= this->scroll_dwell_) {
39+
if ((!this->continuous_ && (this->offset_ + numc >= len)) ||
40+
(this->continuous_ && (this->offset_ > len - 2))) {
41+
if (this->continuous_ || (now - this->last_scroll_ >= this->scroll_dwell_)) {
4642
this->offset_ = 0;
4743
this->last_scroll_ = now;
4844
this->display_();
4945
}
50-
} else
51-
if (now - this->last_scroll_ >= this->scroll_speed_) {
46+
} else if (now - this->last_scroll_ >= this->scroll_speed_) {
5247
this->offset_ += 2;
5348
this->last_scroll_ = now;
5449
this->display_();
@@ -58,19 +53,33 @@ void HT16K33AlphaDisplay::loop() {
5853
float HT16K33AlphaDisplay::get_setup_priority() const { return setup_priority::PROCESSOR; }
5954

6055
void HT16K33AlphaDisplay::display_() {
61-
int offset = this->offset_;
56+
int numc = this->displays_.size() * 8;
57+
int len = this->buffer_.size();
58+
uint8_t data[numc];
59+
memset(data, 0, numc);
60+
int pos = this->offset_;
61+
for (int i = 0; i < numc; i++, pos++) {
62+
if (pos >= len) {
63+
if (!this->continuous_)
64+
break;
65+
pos %= len;
66+
}
67+
data[i] = this->buffer_[pos];
68+
}
69+
pos = 0;
6270
for (auto *display : this->displays_) {
63-
display->write_bytes(DISPLAY_COMMAND_SET_DDRAM_ADDR, this->buffer_ + offset, 8);
64-
offset += 8;
71+
display->write_bytes(DISPLAY_COMMAND_SET_DDRAM_ADDR, data + pos, 8);
72+
pos += 8;
6573
}
6674
}
6775

6876
void HT16K33AlphaDisplay::update() {
69-
memset(this->buffer_, 0, 64);
70-
int prev_fill = this->buffer_fill_;
71-
this->buffer_fill_ = 0;
77+
int prev_fill = this->buffer_.size();
78+
this->buffer_.clear();
7279
this->call_writer();
73-
if (this->scroll_ && (prev_fill != this->buffer_fill_)) {
80+
int numc = this->displays_.size() * 8;
81+
int len = this->buffer_.size();
82+
if ((this->scroll_ && (prev_fill != len) && !this->continuous_) || (len <= numc)) {
7483
this->last_scroll_ = millis();
7584
this->offset_ = 0;
7685
}
@@ -99,14 +108,8 @@ float HT16K33AlphaDisplay::get_brightness() {
99108
}
100109

101110
void HT16K33AlphaDisplay::print(const char *str) {
102-
uint8_t pos = this->buffer_fill_;
103111
uint16_t fontc = 0;
104112
while (*str != '\0') {
105-
if (pos >= 64) {
106-
ESP_LOGW(TAG, "output buffer full!");
107-
break;
108-
}
109-
110113
uint8_t c = *reinterpret_cast<const uint8_t *>(str++);
111114
if (c > 127)
112115
fontc = 0;
@@ -117,10 +120,9 @@ void HT16K33AlphaDisplay::print(const char *str) {
117120
fontc |= 0x4000;
118121
str++;
119122
}
120-
this->buffer_[pos++] = fontc & 0xff;
121-
this->buffer_[pos++] = fontc >> 8;
123+
this->buffer_.push_back(fontc & 0xff);
124+
this->buffer_.push_back(fontc >> 8);
122125
}
123-
this->buffer_fill_ = pos;
124126
}
125127

126128
void HT16K33AlphaDisplay::print(const std::string &str) { this->print(str.c_str()); }

components/ht16k33_alpha/ht16k33_display.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class HT16K33AlphaDisplay : public PollingComponent, public i2c::I2CDevice {
2020
float get_setup_priority() const override;
2121
void add_secondary_display(i2c::I2CDevice *display) { this->displays_.push_back(display); }
2222
void set_scroll(bool scroll) { this->scroll_ = scroll; }
23+
void set_continuous(bool continuous) { this->continuous_ = continuous; }
2324
void set_scroll_speed(unsigned long scroll_speed) { this->scroll_speed_ = scroll_speed; }
2425
void set_scroll_dwell(unsigned long scroll_dwell) { this->scroll_dwell_ = scroll_dwell; }
2526
void set_scroll_delay(unsigned long scroll_delay) { this->scroll_delay_ = scroll_delay; }
@@ -48,12 +49,12 @@ class HT16K33AlphaDisplay : public PollingComponent, public i2c::I2CDevice {
4849
std::vector<i2c::I2CDevice *> displays_ {this};
4950
std::function<void(HT16K33AlphaDisplay &)> writer_;
5051
bool scroll_ {false};
52+
bool continuous_ {false};
5153
unsigned long scroll_speed_ {250};
5254
unsigned long scroll_dwell_ {2000};
5355
unsigned long scroll_delay_ {750};
5456
unsigned long last_scroll_ {0};
55-
uint8_t buffer_[64];
56-
int buffer_fill_ {0};
57+
std::vector<uint8_t> buffer_;
5758
int offset_ {0};
5859
uint8_t brightness_ = 16;
5960
};

0 commit comments

Comments
 (0)