Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom #1

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 105 additions & 1 deletion esphome/components/max7219digit/max7219digit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "esphome/core/helpers.h"
#include "esphome/core/hal.h"
#include "max7219font.h"
#include "max7219font5.h"

namespace esphome {
namespace max7219digit {
Expand Down Expand Up @@ -106,7 +107,7 @@
}
}

void MAX7219Component::display() {
void MAX7219Component::display_old() {
uint8_t pixels[8];
// Run this loop for every MAX CHIP (GRID OF 64 leds)
// Run this routine for the rows of every chip 8x row 0 top to 7 bottom
Expand Down Expand Up @@ -327,6 +328,109 @@
return 0;
}

void MAX7219Component::printf5(int x, const char *format, ...) {
va_list arg;
va_start(arg, format);
char buffer[64];
auto written_count = vsnprintf(buffer, sizeof(buffer), format, arg);
va_end(arg);
if (written_count > 0) {
return this->print5(x, buffer);
}
}

void MAX7219Component::print5(int x, const char *s) {
uint8_t font_width = 5;
uint8_t screen_width = this->num_chips_ * 8;
uint8_t symbols_per_screen = ceil((float) screen_width / font_width);

uint8_t text_length = strlen(s);
uint8_t text_width = text_length * font_width;
uint8_t x2 = x + text_width;

if (x < screen_width && x2 > 0) {
uint8_t c1 = 0;

if (x < 0) {
c1 = floor(-x / font_width);
x = x + c1 * font_width;
}

uint8_t c2 = _min(text_length, c1 + symbols_per_screen);

for (int i = c1; i < c2; i++) {
draw_symbol(x, 0, s[i]);
x = x + font_width;
}
}
}

void MAX7219Component::draw_symbol(int x, int y, uint8_t symbol) {
uint8_t chip = floor(x / 8);
int shift = x % 8;

for (int dataRow = 0; dataRow < 8; dataRow++) {

Check failure on line 372 in esphome/components/max7219digit/max7219digit.cpp

View workflow job for this annotation

GitHub Actions / Run script/clang-tidy for ESP32 Arduino 2/4

invalid case style for local variable 'dataRow'
auto row = y + dataRow;
if (row >= 0 && row < 8) {
uint8_t data = progmem_read_byte(&MAX7219_DOT_MATRIX_FONT_5[symbol][dataRow]);
draw_byte(chip, row, shift >= 0 ? data >> shift : data << -shift);

if (shift > 0) {
draw_byte(chip + 1, row, data << (8 - shift));
}
}
}
}

void MAX7219Component::draw_byte(uint8_t chip, uint8_t row, uint8_t data) {
if (chip >= 0 && chip < this->num_chips_) {
this->max_displaybuffer_[0][chip * 8 + row] |= data;
}
}

void MAX7219Component::display() {
for (int row = 0; row < 8; row++) {
this->enable();

int address = this->reverse_ ? 8 - row : row + 1;
uint8_t data;

for (auto chip = 0; chip < this->num_chips_; chip++) {
if (this->reverse_) {
data = mirror_byte(max_displaybuffer_[0][(this->num_chips_ - chip - 1) * 8 + row]);
} else {
data = max_displaybuffer_[0][chip * 8 + row];
}
this->send_byte_(address, data);
}

this->disable();
}
}

uint8_t MAX7219Component::mirror_byte(uint8_t value) {
uint8_t result = 0;

if (value & 0x01)
result |= 0x80;
if (value & 0x02)
result |= 0x40;
if (value & 0x04)
result |= 0x20;
if (value & 0x08)
result |= 0x10;
if (value & 0x10)
result |= 0x08;
if (value & 0x20)
result |= 0x04;
if (value & 0x40)
result |= 0x02;
if (value & 0x80)
result |= 0x01;

return result;
}

uint8_t MAX7219Component::strftimedigit(uint8_t pos, const char *format, ESPTime time) {
char buffer[64];
size_t ret = time.strftime(buffer, sizeof(buffer), format);
Expand Down
9 changes: 8 additions & 1 deletion esphome/components/max7219digit/max7219digit.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MAX7219Component : public display::DisplayBuffer,

float get_setup_priority() const override;

void display();
void display_old();

void invert_on_off(bool on_off);
void invert_on_off();
Expand Down Expand Up @@ -84,6 +84,13 @@ class MAX7219Component : public display::DisplayBuffer,
/// Print `str` at position 0.
uint8_t printdigit(const char *str);

void printf5(int x, const char *format, ...) __attribute__((format(printf, 3, 4)));
void print5(int x, const char *str);
void draw_symbol(int x, int y, uint8_t symbol);
void draw_byte(uint8_t chip, uint8_t row, uint8_t data);
void display();
uint8_t mirror_byte(uint8_t value);

/// Evaluate the strftime-format and print the result at the given position.
uint8_t strftimedigit(uint8_t pos, const char *format, ESPTime time) __attribute__((format(strftime, 3, 0)));

Expand Down
Loading
Loading