Hallo fony
/*-------------------------------------------------------------------------------------
* glcd_demo
*
* Simple exmaple for working with the I2C graphic LCD
* Reads a value from ADC port 0 and shows that value as a horizonatl bar graph
* In addition, the display light is switch off after a period of inactivity
* and restored to the previous level, once activity is recogized
*
* Stephan Laage-Witt - 17-Dec-2017
* http://laagewitt.de/smart-i2c-glcd-am-arduino-ein-einfaches-beispiel/
*/
#include <Wire.h>
#include <glcd_functions.h>
#define ADC_CHANNEL A0
#define LIGHT_OFF_DELAY 300
#define ADC_OFFSET 10
// Global varibales -------------------------------------------------------------------
glcd my_gd(0x20); // instance of the graphic display at I2C address 0x20
uint16_t delay_cnt = LIGHT_OFF_DELAY; // counter measuring time of inactivity
struct bar_graph {
uint8_t x_pos; // coordinates of the upper left corner
uint8_t y_pos;
uint8_t len; // length and width of the bar
uint8_t width;
uint8_t new_display_len; // new length of the display bar, to be set
uint8_t current_display_len; // current length of the display bar
} my_bar_graph;
// setup() ----------------------------------------------------------------------------
void setup() {
uint8_t x_cur, y_cur, font_height;
// initialize bar graph
my_bar_graph.x_pos = 10;
my_bar_graph.y_pos = 40;
my_bar_graph.len = 170;
my_bar_graph.width = 6;
my_bar_graph.current_display_len = 0;
// start display, print title and draw bar frame for the bar graph
Wire.begin();
my_gd.clear_screen();
my_gd.set_light(10);
my_gd.set_font(4);
my_gd.draw_str("Smart I2C GLCD Demo with Arduino");
delay(40); // ensure buffer is empty prior to read request
my_gd.get_cursor(&x_cur, &y_cur);
font_height = my_gd.get_font_height();
my_gd.draw_line(0, font_height + 1, x_cur - 1, font_height + 1, 1);
draw_bar_graph_frame(&my_bar_graph); // plot frame for the bar
my_gd.set_font(0);
}
// loop() -----------------------------------------------------------------------------
void loop() {
uint16_t adc_value; // input via ADC channel 0 (0 ... 1024)
uint16_t conv_value; // converted ADC value (0 ... 1000)
// read and print ADC value
adc_value = analogRead(ADC_CHANNEL);
my_gd.set_cursor(my_bar_graph.x_pos - 5, my_bar_graph.y_pos - 20);
my_gd.draw_udec(adc_value, 4, 1);
// convert ADC value to usable range (0 ... 1000)
if (adc_value < ADC_OFFSET) conv_value = 0;
else if(adc_value > 1000 + ADC_OFFSET) conv_value = 1000;
else conv_value = adc_value - ADC_OFFSET;
my_bar_graph.new_display_len = (uint32_t) conv_value * my_bar_graph.len / 1000; // calculate corresponding display len
// show current value and manage back light
if (my_bar_graph.new_display_len != my_bar_graph.current_display_len) { // any changes ?
refresh_bar_graph(&my_bar_graph); // show new value
my_gd.dim_off(); // enable display backlight
delay_cnt = LIGHT_OFF_DELAY; // re-initilaize delay_counter
} else {
if (delay_cnt > 0) { // otherwise in case of no changes ...
--delay_cnt; // count down delay counter
if (delay_cnt == 0) // when reaching zero ...
my_gd.dim_on(); // switch off the light
};
}
// wait a moment
delay(100);
}
/* draw_bar_graph_frame(bar_graph *bg) --------------------------------------------------------------
* Plots the frame for a horizontal bar display
*/
void draw_bar_graph_frame(bar_graph *bg) {
uint16_t tickmark, tickmark_x;
char tickmark_str[4];
// draw empty box
my_gd.draw_rectangle(bg->x_pos -1 , bg->y_pos - 1, bg->x_pos + bg->len + 1, bg->y_pos + bg->width + 1, 1);
// draw tick marks
my_gd.set_font(1);
for (tickmark = 0; tickmark <= 1000; tickmark += 100) { // 10 tickmarks in total
tickmark_x = bg->x_pos + ((uint32_t) tickmark * bg->len / 1000);
my_gd.set_pixel(tickmark_x, bg->y_pos - 2, 1);
my_gd.set_pixel(tickmark_x, bg->y_pos - 3, 1);
my_gd.set_pixel(tickmark_x, bg->y_pos + bg->width + 2, 1);
my_gd.set_pixel(tickmark_x, bg->y_pos + bg->width + 3, 1);
my_gd.set_pixel(tickmark_x, bg->y_pos + bg->width + 2, 1);
my_gd.set_pixel(tickmark_x, bg->y_pos + bg->width + 3, 1);
if (tickmark % 200 == 0) {
my_gd.set_cursor(tickmark_x, bg->y_pos - 9); // every other tickmark with label
itoa(tickmark / 10, tickmark_str, 10);
my_gd.draw_center_str(tickmark_str);
};
};
}
/* refresh_bar_graph(bar_graph *bg) -------------------------------------------------------------------
* Plots a display bar
*/
void refresh_bar_graph(bar_graph *bg) {
if (bg->new_display_len > bg->current_display_len) { // in case the new bar is longer than the current ...
my_gd.draw_filled_rectangle(bg->x_pos + bg->current_display_len,
bg->y_pos, bg->x_pos + bg->new_display_len,
bg->y_pos + bg->width, 1);
} else if (bg->new_display_len < bg->current_display_len) { // in case the new bar is shorter than the current ...
my_gd.draw_filled_rectangle(bg->x_pos + bg->new_display_len + 1,
bg->y_pos, bg->x_pos + bg->current_display_len,
bg->y_pos + bg->width, 0);
};
bg->current_display_len = bg->new_display_len; // udpate current status of the display
}
Habe den Code jetzt mal als Beispiel genommen.
Es geht mir nur um das Einbinden der Ks0108.h Datei in den Code.