#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
//---------------------------------------
//------------ IMPORTANT !!! ------------
//---------------------------------------
// Function draw_level_bars compatible with U8glib
// Can be copied into any U8glib file
// The function is called at line 93 and the rest of the code is for demo only
void draw_level_bars(uint8_t x_pivot, uint8_t y_pivot,
uint8_t bar_width, uint8_t bar_gap,
uint8_t smaller_bar_height, uint8_t bigger_bar_height,
uint8_t bar_count, uint8_t filled_levels)
{
uint8_t bar_step_size = (bigger_bar_height - smaller_bar_height) / (bar_count - 1);
for (uint8_t i = 0; i < bar_count; i++)
{
if ((filled_levels - 1) < i)
{
u8g.drawFrame(x_pivot + i * (bar_width + bar_gap),
y_pivot + (bigger_bar_height - (i + 1) * (bar_step_size)),
bar_width, smaller_bar_height + (i * bar_step_size));
}
else
{
u8g.drawBox(x_pivot + i * (bar_width + bar_gap),
y_pivot + (bigger_bar_height - (i + 1) * (bar_step_size)),
bar_width, smaller_bar_height + (i * bar_step_size));
}
}
}
void setup() {
u8g.setColorIndex(1);
}
// X and Y coordinates of the upper left corner in pixels
uint8_t ul_corner[2] = {10, 10};
// Width of a single bar in pixels
uint8_t width = 10;
// Gap between two bars
uint8_t gap = 2;
// Height of the smaller bar and the bigger bar in pixels
uint8_t height[2] = {10, 30};
// Number of bars
uint8_t n_bars = 3;
// How many bares are filled or active
uint8_t level = 0;
// Auxiliar variables for demo
int8_t direction_level = 1;
int8_t direction_x = 1;
int8_t direction_y = 1;
uint32_t time_ctrl_level = 0;
uint16_t level_refresh = 1000;
uint32_t time_ctrl_xy = 0;
uint16_t xy_refresh = 10;
void loop() {
if (millis() > (time_ctrl_level + level_refresh))
{
level += direction_level;
if (level == 3) direction_level = -1;
else if (level == 0) direction_level = 1;
time_ctrl_level = millis();
}
if (millis() > (time_ctrl_xy + xy_refresh))
{
ul_corner[0] += direction_x;
ul_corner[1] += direction_y;
if ((ul_corner[0] + (width * n_bars) + (gap * (n_bars - 1)) + direction_x) > 128) direction_x = -1;
else if ((ul_corner[0] + direction_x) < 0) direction_x = 1;
if ((ul_corner[1] + height[1] + direction_y) > 64) direction_y = -1;
else if ((ul_corner[1] + direction_y) < 0) direction_y = 1;
u8g.firstPage();
do {
draw_level_bars(ul_corner[0], ul_corner[1], width, gap, height[0], height[1], n_bars, level);
} while (u8g.nextPage());
}
delay(10);
}
Loading
ssd1306
ssd1306