Lego Interactive Brick Program
Jump to navigation
Jump to search
//Demonstration Program for Interactive LCD Touch Screen Lego 70455 Interface
#include <tinyFAT.h> #include <UTFT.h> #include <UTFT_tinyFAT.h> #include <UTouch.h> #include <LegoTechnic.h> // Declare which fonts we will be using extern uint8_t SmallFont[]; UTFT LEGO_Display(ITDB32S, 38, 39, 40, 41); // Remember to change the model parameter to suit your display module! UTFT_tinyFAT LEGO_Files(&LEGO_Display); UTouch LEGO_Touch( 6, 5, 4, 3, 2); LegoTechnic brick(8, 9, 10, 11, 12, 13, 0, 1); int picsize_x, picsize_y; word resource; // Display locations int x_HotSpot=85; int y_HotSpot=163; int HotSpot_width=202; int HotSpot_height=72; int x_slider = 86; int y_slider = 173; int slider_space = 19; int slider_width = 10; int slider_height = 10; int x_indicator = 259; int y_indicator = 59; int indicator_space = 34; int indicator_width = 5; int indicator_height = 5; int x_button = 277; int y_button = 73; int button_width = 19; int button_height = 40; int x_output = 86; int y_output = 140; int output_space = 107; int output_width = 200; int output_height = 10;
void setup()
{
// Initialize LEGO brick
brick.Port_Initialize();
// Prepare Display
LEGO_Display.InitLCD();
LEGO_Display.clrScr();
LEGO_Display.setColor(255,255,255);
LEGO_Display.setFont(SmallFont);
picsize_x=LEGO_Display.getDisplayXSize();
picsize_y=LEGO_Display.getDisplayYSize();
// Prepare Touch Screen
LEGO_Touch.InitTouch();
LEGO_Touch.setPrecision (PREC_MEDIUM);
LEGO_Display.setBackCol or(0, 0, 255);
// LEGO Library has Serial Monitor Functions
Serial.begin(9600);
// Load Interface Image
file.initFAT();
resource=LEGO_Files.loadBitmap(0, 0, picsize_x, picsize_y, "LEGOPIC.RAW");
}
void loop()
{
int hold_value[6]={0,0,0,0,0,0};
int x,y;
int Previous_I0, Present_I0;
int Previous_I1, Present_I1;
int button_value;
// Draw Screen Elements
Sliders();
Outputs();
brick.Verbose(LOW);
UpdateDisplay();
Previous_I0 = -1;
Previous_I1 = -1;
while (true)
{
// Check for change to input values
Present_I0 = brick.Input_Port(6);
Present_I1 = brick.Input_Port(7);
if (Present_I0 != Previous_I0)
{
Previous_I0 = Present_I0;
DrawIndicatorValue(6,Present_I0);
DrawOutputValue(0,Present_I0);
}
if (Present_I1 != Previous_I1)
{
Previous_I1 = Present_I1;
DrawIndicatorValue(7,Present_I1);
DrawOutputValue(1,Present_I1);
}
// Test for touch screen input
if (LEGO_Touch.dataAvailable())
{
LEGO_Touch.read();
x=LEGO_Touch.getX();
y=LEGO_Touch.getY();
EvaluateTouch(x,y);
delay(100);
while(LEGO_Touch.dataAvailable())
{
LEGO_Touch.read();
}
}
}
}
// Determine if touch screen input is within an active zone
void EvaluateTouch(int x, int y)
{
if ((x >= x_HotSpot) && (x <= x_HotSpot + HotSpot_width))
if ((y >= y_HotSpot) && (y <= y_HotSpot + HotSpot_height))
EvaluateSliders(x,y);
if ((x >= x_button) && (x <= x_button + button_width))
if ((y >= y_button) && (y <= y_button + button_height))
ExecuteButton(x,y);
}
// Reset Button for Outputs
void ExecuteButton(int x,int y)
{
for(int x=0; x< 6; ++x)
{
EraseSliderValue(x, int (brick.Port_PWM_Value(x)/25));
brick.PWM_Port(x, 0);
}
UpdateDisplay();
}
// Determine which slider was "touched"
void EvaluateSliders(int x,int y)
{
int line_number = (5 - (y-y_HotSpot)/(slider_height+1));
int line_value = (x-x_HotSpot)/(slider_space);
EraseSliderValue(line_number, int (brick.Port_PWM_Value(line_number)/25));
brick.PWM_Port(line_number, int (line_value * 25));
DrawSliderValue(line_number, line_value);
DrawIndicatorValue(line_number,brick.Port_PWM_Value(line_number));
}
// Update Screen Elements
void UpdateDisplay()
{
for(int x=0;x<8;++x)
{
if (x < 6)
{
DrawSliderValue(x,brick.Port_PWM_Value(x));
DrawIndicatorValue(x,brick.Port_PWM_Value(x));
}
else
DrawIndicatorValue(x,brick.Input_Port(x));
}
}
// Draw All Sliders
void Sliders()
{
for(int x=0; x < 6; ++x)
{
y_slider = 224-(x*(slider_height+2));
DrawSlider(x);
DrawSliderValue(x,0);
}
}
// Draw each Slider
void DrawSlider(int line)
{
String label=" Line ";
label.concat(char(48+line));
LEGO_Display.setColor(255,255,255);
LEGO_Display.setBackColor(0,0,0);
LEGO_Display.print(label, LEFT,224-(line*(slider_height+2)));
LEGO_Display.print(" OFF", 53, 224-(line*(slider_height+2)));
LEGO_Display.print(" MAX", RIGHT, 224-(line*(slider_height+2)));
LEGO_Display.setColor(0,0,0);
LEGO_Display.drawLine(x_slider,224-(line*(slider_height+2))+(slider_height/2),x_slider+slider_width+(10*slider_space),224-(line*(slider_height+2))+(slider_height/2));
for(int x=0;x<11;++x)
{
LEGO_Display.drawLine(x_slider+(slider_width/2)+(x*slider_space),224-(line*(slider_height+2)),x_slider+(slider_width/2)+(x*slider_space),224-(line*(slider_height+2))+(slider_height));
}
}
// Display Slider Value on Screen
void DrawSliderValue(int line, int value)
{
if (line < 6)
LEGO_Display.setColor(255, 0, 0);
else
LEGO_Display.setColor(0, 255, 0);
if (value == 0)
LEGO_Display.setColor(0,0,0);
LEGO_Display.fillRoundRect(x_slider+(value*slider_space), 224-(line*(slider_height+2)), x_slider+slider_width+(value*slider_space), 224-(line*(slider_height+2)) + slider_height);
LEGO_Display.setColor(0, 0, 0);
LEGO_Display.drawRoundRect (x_slider+(value*slider_space), 224-(line*(slider_height+2)), x_slider+slider_width+(value*slider_space), 224-(line*(slider_height+2)) + slider_height);
if (value == 0)
{
LEGO_Display.setColor(255,255,255);
LEGO_Display.drawRoundRect (x_slider+(value*slider_space)+1, 224-(line*(slider_height+2))+1, x_slider+slider_width+(value*slider_space)-1, 224-(line*(slider_height+2)) + slider_height-1);
}
}
// Remove Slider value from Screen
void EraseSliderValue(int line, int value)
{
LEGO_Display.setColor(0, 255, 255);
LEGO_Display.fillRoundRect(x_slider+(value*slider_space), 224-(line*(slider_height+2)), x_slider+slider_width+(value*slider_space), 224-(line*(slider_height+2)) + slider_height);
LEGO_Display.setColor(0,0,0);
LEGO_Display.drawLine (x_slider+(value*slider_space), 224-(line*(slider_height+2))+(slider_height/2), x_slider+slider_width+(value*slider_space), 224-(line*(slider_height+2))+(slider_height/2));
LEGO_Display.drawLine(x_slider+(slider_width/2)+(value*slider_space),224-(line*(slider_height+2)),x_slider+(slider_width/2)+(value*slider_space),224-(line*(slider_height+2))+(slider_height));
}
// Draw Indicator Dots on Screen
void Indicators()
{
for(int x=0; x < 8; ++x)
{
DrawIndicatorValue(x,0);
}
}
// Display Input and Output Status on Screen
void DrawIndicatorValue(int line, int value)
{
LEGO_Display.setColor(50,50,50);
if (line < 6)
{
if (value != 0)
LEGO_Display.setColor(255,0,0);
}
else
{
if (value > 511)
LEGO_Display.setColor(0,255,0);
Serial.println(value);
}
LEGO_Display.fillRoundRect(x_indicator-(line*indicator_space), y_indicator, x_indicator-indicator_width-(line*indicator_space), y_indicator + indicator_height);
LEGO_Display.setColor(0,0,0);
LEGO_Display.drawRoundRect(x_indicator-(line*indicator_space), y_indicator, x_indicator-indicator_width-(line*indicator_space), y_indicator + indicator_height);
}
// Draw All Input Display on Screen
void Outputs()
{
for(int x=0;x<2;++x)
{
DrawOutput(x);
DrawOutputValue(x,LOW);
}
}
// Draw each Input Display on Screen
void DrawOutput(int line)
{
String label=" Output ";
label.concat(char(48-line+7));
label.concat(" ");
LEGO_Display.setColor(255,255,255);
LEGO_Display.setBackColor(0,0,0);
LEGO_Display.print(label, LEFT,y_output+(line*(output_height+2)));
LEGO_Display.print(" ", RIGHT, y_output+(line*(output_height+2)));
LEGO_Display.setColor(0,0,0);
LEGO_Display.drawRoundRect(x_output, y_output+(line*(output_height+2)), x_output+output_width, y_output+(line*(output_height+2)) + output_height);
}
// Draw Input Display based on Value to Screen
void DrawOutputValue(int line, int value)
{
if (line == 0)
line = 1;
else
line = 0;
if (value > 511)
{
LEGO_Display.setColor(0,255,0);
LEGO_Display.fillRoundRect(x_output, y_output+(line*(output_height+2)), x_output+output_width, y_output+(line*(output_height+2)) + output_height);
}
else
{
LEGO_Display.setColor(50,50,50);
LEGO_Display.fillRoundRect(x_output, y_output+(line*(output_height+2)), x_output+output_width, y_output+(line*(output_height+2)) + output_height);
}
LEGO_Display.setColor(0,0,0);
LEGO_Display.drawRoundRect(x_output, y_output+(line*(output_height+2)), x_output+output_width, y_output+(line*(output_height+2)) + output_height);
}