Здравствуйте!
В этой статье вы найдете код программы, который использовался в восьмой серии обучающего сериала «Не спали свою Ардуинку», а так же схему подключения TFT дисплея 1.8 160*128 пикселей, к Arduino Mega 2560.
Суть восьмой серии заключается в том, чтобы показать пример, как создать вот такие индикаторы для TFT дисплея.
Вот сам код программы. Он содержит две функции, которые позволяют выводить на TFT дисплей, индикатор наличия питания в сети, а индикатор состояния зарядки батареи.
//Начало скетча
#include "TFT.h"
#include "SPI.h"
TFT TFTscreen = TFT(53, 49, 50);//CS, DC, RESET
int buf_charg = 3;
void charging(int x_pixel, int y_pixel, int state){
if(buf_charg != state){
buf_charg = state;
if(state == 0){
TFTscreen.stroke(255, 255, 255);
TFTscreen.line(x_pixel+2, y_pixel+6, x_pixel+2, y_pixel+8);
TFTscreen.line(x_pixel+3, y_pixel+4, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+2, x_pixel+4, y_pixel+8);
TFTscreen.line(x_pixel+5, y_pixel+0, x_pixel+5, y_pixel+4);
TFTscreen.line(x_pixel+4, y_pixel+11, x_pixel+4, y_pixel+15);
TFTscreen.line(x_pixel+5, y_pixel+7, x_pixel+5, y_pixel+13);
TFTscreen.line(x_pixel+6, y_pixel+7, x_pixel+6, y_pixel+11);
TFTscreen.line(x_pixel+7, y_pixel+7, x_pixel+7, y_pixel+9);
TFTscreen.stroke(255, 0, 0);
TFTscreen.line(x_pixel, y_pixel+12, x_pixel+9, y_pixel+3);
TFTscreen.line(x_pixel, y_pixel+11, x_pixel+9, y_pixel+2);
}
if(state == 1){
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel, y_pixel+12, x_pixel+9, y_pixel+3);
TFTscreen.line(x_pixel, y_pixel+11, x_pixel+9, y_pixel+2);
TFTscreen.stroke(0, 255, 0);
TFTscreen.line(x_pixel+2, y_pixel+6, x_pixel+2, y_pixel+8);
TFTscreen.line(x_pixel+3, y_pixel+4, x_pixel+3, y_pixel+8);
TFTscreen.line(x_pixel+4, y_pixel+2, x_pixel+4, y_pixel+8);
TFTscreen.line(x_pixel+5, y_pixel+0, x_pixel+5, y_pixel+4);
TFTscreen.line(x_pixel+4, y_pixel+11, x_pixel+4, y_pixel+15);
TFTscreen.line(x_pixel+5, y_pixel+7, x_pixel+5, y_pixel+13);
TFTscreen.line(x_pixel+6, y_pixel+7, x_pixel+6, y_pixel+11);
TFTscreen.line(x_pixel+7, y_pixel+7, x_pixel+7, y_pixel+9);
}
}
}
int buf_volt = 3;
void voltage(int x_pixel, int y_pixel, int state){
if(buf_volt != state){
buf_volt = state;
if(state == 0){
TFTscreen.stroke(255, 255, 255);
TFTscreen.line(x_pixel, y_pixel+4, x_pixel, y_pixel+6);
TFTscreen.line(x_pixel+1, y_pixel, x_pixel+1, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+4, x_pixel+2, y_pixel+10);
TFTscreen.line(x_pixel+3, y_pixel+4, x_pixel+3, y_pixel+10);
TFTscreen.line(x_pixel+4, y_pixel+4, x_pixel+4, y_pixel+10);
TFTscreen.line(x_pixel+5, y_pixel+4, x_pixel+5, y_pixel+10);
TFTscreen.line(x_pixel+6, y_pixel+0, x_pixel+6, y_pixel+8);
TFTscreen.line(x_pixel+7, y_pixel+4, x_pixel+7, y_pixel+6);
TFTscreen.stroke(255, 0, 0);
TFTscreen.line(x_pixel, y_pixel+8, x_pixel+8, y_pixel);
TFTscreen.line(x_pixel, y_pixel+9, x_pixel+8, y_pixel+1);
}
if(state == 1){
TFTscreen.stroke(0, 0, 0);
TFTscreen.line(x_pixel, y_pixel+8, x_pixel+8, y_pixel);
TFTscreen.line(x_pixel, y_pixel+9, x_pixel+8, y_pixel+1);
TFTscreen.stroke(0, 255, 0);
TFTscreen.line(x_pixel, y_pixel+4, x_pixel, y_pixel+6);
TFTscreen.line(x_pixel+1, y_pixel, x_pixel+1, y_pixel+8);
TFTscreen.line(x_pixel+2, y_pixel+4, x_pixel+2, y_pixel+10);
TFTscreen.line(x_pixel+3, y_pixel+4, x_pixel+3, y_pixel+10);
TFTscreen.line(x_pixel+4, y_pixel+4, x_pixel+4, y_pixel+10);
TFTscreen.line(x_pixel+5, y_pixel+4, x_pixel+5, y_pixel+10);
TFTscreen.line(x_pixel+6, y_pixel+0, x_pixel+6, y_pixel+8);
TFTscreen.line(x_pixel+7, y_pixel+4, x_pixel+7, y_pixel+6);
}
}
}
void setup()
{
TFTscreen.begin();
TFTscreen.background(0,0,0);
}
void loop() {
charging(0, 0, digitalRead(2));
voltage(15, 2, digitalRead(3));
}
//Конец скетча
А это схема подключения TFT дисплея 1.8 160*128 пикселей, к Arduino Mega 2560. В схеме имеются две кнопки, которыми можно влиять на состояние обеих индикаторов.