#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
float shuntVoltage_mV = 0.2;
float loadVoltage_V = 12.2;
float busVoltage_V = 12.3;
float current_mA = 150;
float power_mW = 1234;
bool ina219_overflow = false;
int bat_level = 0;
int charge_amount = -100;
bool bat_charging=true;
// U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void setup(void)
{
u8g2.begin();
u8g2.setFontDirection(0);
u8g2.setFont(u8g2_font_likeminecraft_te);
u8g2.setFontRefHeightExtendedText();
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
delay(1000);
}
void drawBatFill()
{
u8g2.drawBox(1,15+bat_level,53,30-bat_level);
bat_level+=1;
if (bat_level > 25) bat_level=0;
}
void loop(void) {
u8g2.clearBuffer();
u8g2.setDrawColor(1);
u8g2.drawRFrame(0,10,55,40,8);
u8g2.drawRFrame(6,5,8,6,2);
u8g2.drawRFrame(40,5,8,6,2);
drawBatFill();
u8g2.setFont(u8g2_font_percent_circle_25_hn);
u8g2.setDrawColor(2);
u8g2.setFontMode(1);
//u8g2.drawGlyph(15,15,50);
//u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_profont29_tn);
u8g2.drawStr(8,12,"613");
u8g2.setFont(u8g2_font_profont15_tf);
u8g2.drawStr(36,30,"Ah");
//u8g2.setFont(u8g2_font_helvR08_tr);
u8g2.setFont(u8g2_font_spleen5x8_mr);
u8g2.setDrawColor(1);
u8g2.drawStr(3,52,"13.2V");
u8g2.setFont(u8g2_font_6x10_tf);
//u8g2.drawBox(3,7,25,15);
/*
u8g2.setCursor(0,0);
u8g2.print("Bus: ");
u8g2.print(busVoltage_V,1);
u8g2.print("V");
u8g2.setCursor(8*8,0);
u8g2.print(current_mA,1);
u8g2.print("mA");
u8g2.setCursor(0,12);
u8g2.print("Shn: ");
u8g2.print(shuntVoltage_mV,1);
u8g2.print("V");
u8g2.setCursor(8*8,12);
u8g2.print(power_mW,1);
u8g2.print("mW");
*/
int xcenter=100;
int ycenter=32;
int arc=20;
//u8g2.setDrawColor(1);
//u8g2.drawDisc(xcenter, ycenter, arc+6, U8G2_DRAW_UPPER_LEFT + U8G2_DRAW_UPPER_RIGHT);
//u8g2.setDrawColor(0);
//u8g2.drawDisc(xcenter, ycenter, arc+4, U8G2_DRAW_UPPER_LEFT + U8G2_DRAW_UPPER_RIGHT);
//u8g2.setDrawColor(1);
//u8g2.drawCircle(xcenter,ycenter,arc+6, U8G2_DRAW_UPPER_LEFT);
//u8g2.drawCircle(xcenter,ycenter,arc+4, U8G2_DRAW_UPPER_LEFT);
// draw the needle
//float x1=sin(2*3.14/360); // needle position
// float y1=cos(2*3.14/360);
// u8g2.drawLine(xcenter, ycenter, xcenter+arc*x1, ycenter-arc*y1);
// u8g2.drawDisc(xcenter, ycenter, 5, U8G2_DRAW_UPPER_LEFT);
// u8g2.drawDisc(xcenter, ycenter, 5, U8G2_DRAW_UPPER_RIGHT);
// show scale labels
charge_amount+=1;
if (charge_amount>100) charge_amount=-100;
if (charge_amount >0 )
{
ringMeter(charge_amount ,0,100, 64,5,30);
}
else
{
reverse_ringMeter(charge_amount ,0,100, 64,3,30);
}
u8g2.setDrawColor(1);
u8g2.setFont( u8g2_font_VCR_OSD_mu);
u8g2.setCursor(92,30);
u8g2.print(String(charge_amount));
u8g2.sendBuffer();
}
// #########################################################################
// Draw the meter on the screen, returns x coord of righthand side
// #########################################################################
int ringMeter(int value, int vmin, int vmax, int x, int y, int r)
{
// Minimum value of r is about 52 before value text intrudes on ring
// drawing the text first is an option
x += r; y += r; // Calculate coords of centre of ring
int w = r / 4; // Width of outer ring is 1/4 of radius
int angle = 150; // Half the sweep angle of meter (300 degrees)
int text_colour = 0; // To hold the text colour
int v = map(value, vmin, vmax, -angle, angle); // Map the value to an angle v
byte seg = 5; // Segments are 5 degrees wide = 60 segments for 300 degrees
byte inc = 5; // Draw segments every 5 degrees, increase to 10 for segmented ring
// Draw colour blocks every inc degrees
for (int i = -angle; i < angle; i += inc)
{
// Calculate pair of coordinates for segment start
float sx = cos((i - 90) * 0.0174532925);
float sy = sin((i - 90) * 0.0174532925);
uint16_t x0 = sx * (r - w) + x;
uint16_t y0 = sy * (r - w) + y;
uint16_t x1 = sx * r + x;
uint16_t y1 = sy * r + y;
// Calculate pair of coordinates for segment end
float sx2 = cos((i + seg - 90) * 0.0174532925);
float sy2 = sin((i + seg - 90) * 0.0174532925);
int x2 = sx2 * (r - w) + x;
int y2 = sy2 * (r - w) + y;
int x3 = sx2 * r + x;
int y3 = sy2 * r + y;
if (i < v) { // Fill in coloured segments with 2 triangles
u8g2.setDrawColor(1);
u8g2.drawTriangle(x0, y0, x1, y1, x2, y2);
u8g2.drawTriangle(x1, y1, x2, y2, x3, y3);
}
else // Fill in blank segments
{
u8g2.setDrawColor(0);
u8g2.drawTriangle(x0, y0, x1, y1, x2, y2);
u8g2.drawTriangle(x1, y1, x2, y2, x3, y3);
}
}
// Convert value to a string
// Calculate and return right hand side x coordinate
return x + r;
}
int reverse_ringMeter(int value, int vmin, int vmax, int x, int y, int r)
{
// Minimum value of r is about 52 before value text intrudes on ring
// drawing the text first is an option
value = abs(value);
x += r; y += r; // Calculate coords of centre of ring
int w = r / 4; // Width of outer ring is 1/4 of radius
int angle = 150; // Half the sweep angle of meter (300 degrees)
int text_colour = 0; // To hold the text colour
int v = map(value, vmin, vmax, -angle, angle); // Map the value to an angle v
byte seg = 5; // Segments are 5 degrees wide = 60 segments for 300 degrees
byte inc = 5; // Draw segments every 5 degrees, increase to 10 for segmented ring
// Draw colour blocks every inc degrees
for (int i = -angle; i < angle; i += inc)
{
// Calculate pair of coordinates for segment start
float sx = cos((i + 90) * 0.0174532925);
float sy = sin((i + 90) * 0.0174532925);
uint16_t x0 = sx * (r - w) + x;
uint16_t y0 = sy * (r - w) + y;
uint16_t x1 = sx * r + x;
uint16_t y1 = sy * r + y;
// Calculate pair of coordinates for segment end
float sx2 = cos((i - seg + 90) * 0.0174532925);
float sy2 = sin((i - seg + 90) * 0.0174532925);
int x2 = sx2 * (r - w) + x;
int y2 = sy2 * (r - w) + y;
int x3 = sx2 * r + x;
int y3 = sy2 * r + y;
if (i < v) { // Fill in coloured segments with 2 triangles
u8g2.setDrawColor(1);
u8g2.drawTriangle(x0, y0, x1, y1, x2, y2);
u8g2.drawTriangle(x1, y1, x2, y2, x3, y3);
}
else // Fill in blank segments
{
u8g2.setDrawColor(0);
u8g2.drawTriangle(x0, y0, x1, y1, x2, y2);
u8g2.drawTriangle(x1, y1, x2, y2, x3, y3);
}
}
// Convert value to a string
// Calculate and return right hand side x coordinate
return x + r;
}
// #########################################################################
// Return a value in range -1 to +1 for a given phase angle in degrees
// #########################################################################
float sineWave(int phase) {
return sin(phase * 0.0174532925);
}