Skip to content

Instantly share code, notes, and snippets.

@stolk
Created March 27, 2023 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stolk/e43647255651c62e962b431bab4fd80d to your computer and use it in GitHub Desktop.
Save stolk/e43647255651c62e962b431bab4fd80d to your computer and use it in GitHub Desktop.
#define HT16K33_CMD_BLINK 0x80
#define HT16K33_CMD_BRIGHTNESS 0xE0
enum blink_values
{
HT16K33_BLINK_NONE=0,
HT16K33_BLINK_500MS,
HT16K33_BLINK_1000MS,
HT16K33_BLINK_2000MS
};
uint8_t ledbp_i2c_port = 0;
uint8_t ledbp_i2c_addr = 0x70;
esp_err_t ledbp_write_reg(uint8_t command)
{
const esp_err_t ret = i2c_master_write_to_device
(
ledbp_i2c_port,
ledbp_i2c_addr,
&command,
1,
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS
);
return ret;
}
esp_err_t ledbp_write_buf(const uint8_t* buffer, uint8_t sz)
{
const esp_err_t ret = i2c_master_write_to_device
(
ledbp_i2c_port,
ledbp_i2c_addr,
buffer,
sz,
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS
);
return ret;
}
esp_err_t ledbp_write_screen(const uint8_t pixbuf[8])
{
uint8_t buffer[2*8+1];
uint8_t* writer = buffer;
*writer++ = 0x00; // start at address $00
for (uint8_t i = 0; i < 8; i++)
{
*writer++ = pixbuf[i]; // eight monochrome pixels.
*writer++ = 0x00; // high bytes are unused.
}
return ledbp_write_buf(buffer, sizeof(buffer));
}
void ledbp_set_brightness(uint8_t level)
{
level = level > 15 ? 15 : level;
uint8_t command = HT16K33_CMD_BRIGHTNESS | level;
ledbp_write_reg(command);
}
void ledbp_set_blink_rate(uint8_t rate)
{
rate = rate > 3 ? 3 : rate;
uint8_t command = HT16K33_CMD_BLINK | 1 | (rate << 1);
ledbp_write_reg(command);
}
void ledbp_init(uint8_t portnr, uint8_t addr)
{
// Save the port and addr settings.
ledbp_i2c_port = portnr;
ledbp_i2c_addr = addr;
// turn on oscillator.
ledbp_write_reg(0x21);
// not too bright.
ledbp_set_brightness(10);
// let's blink.
ledbp_set_blink_rate(HT16K33_BLINK_500MS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment