Skip to content

Instantly share code, notes, and snippets.

@acmlira acmlira/HelloWorld.java
Last active Jan 27, 2020

Embed
What would you like to do?
package com.totalcross;
import totalcross.ui.MainWindow;
import totalcross.ui.event.ControlEvent;
import totalcross.ui.event.PressListener;
import totalcross.ui.gfx.Color;
import totalcross.ui.Button;
import totalcross.ui.Label;
import totalcross.io.device.gpiod.GpiodChip;
import totalcross.io.device.gpiod.GpiodLine;
import totalcross.sys.Settings;
import totalcross.sys.Vm;
public class HelloWorld extends MainWindow {
// Integers to store pin numbers
private int R = 4, G = 17, B = 27, pushButton = 18;
// Integers to store state of each LED pin, 0 (LOW) and 1 (HIGH)
private int sttR, sttG, sttB;
// Buttons to control colors
private Button btnR, btnG, btnB;
public HelloWorld() {
setUIStyle(Settings.MATERIAL_UI);
}
@Override
public void initUI() {
// Label helloWorld made on project creation
Label helloWorld = new Label("Hello World!");
// Change the position of label on the Y axis, with TOP (beginning of Y) + a fill of 20
add(helloWorld, CENTER, TOP + 20);
// Label to indicate LED control
Label labelRGB = new Label("RGB:");
// Add label to screen after previous with a fill of 20
add(labelRGB, CENTER, AFTER + 20);
// Board Setup
GpiodChip gpioChip = GpiodChip.open(0);
GpiodLine pinR = gpioChip.line(R);
GpiodLine pinG = gpioChip.line(G);
GpiodLine pinB = gpioChip.line(B);
GpiodLine pinPushButton = gpioChip.line(pushButton);
// Set LED pins as outputs and default value sttX
pinR.requestOutput("CONSUMER",sttR);
pinG.requestOutput("CONSUMER",sttG);
pinB.requestOutput("CONSUMER",sttB);
// Set Reset pin as input
pinPushButton.requestInput("CONSUMER");
// The TotalCross button:
btnR = new Button("R"); // Button instantiation
// without text
btnR.setBackColor(Color.RED); // Set background color (red)
btnR.addPressListener(new PressListener() { // Press event listener
@Override
public void controlPressed(ControlEvent controlEvent) {
sttR = 1 - sttR; // Invert pin state
pinR.setValue(sttR); // Set value (HIGH or LOW)
}
});
add(btnR, CENTER - 70, AFTER + 40); // To make horizontally aligned
// buttons in the 'RGB' sequence,
// take the center reference and
// decrease 70 to place the
// leftmost R. In the Y axis just
// take the reference of the
// previous component and add 40
btnG = new Button("G");
btnG.setBackColor(Color.GREEN);
btnG.addPressListener(new PressListener() {
@Override
public void controlPressed(ControlEvent controlEvent) {
sttG = 1 - sttG; // Pay attention to change pin!!!
pinG.setValue(sttG);
}
});
add(btnG, CENTER, SAME); // The green button will be
// placed at the center and in
// the same line of previous
// button
btnB = new Button("B");
btnB.setBackColor(Color.BLUE);
btnB.addPressListener(new PressListener() {
@Override
public void controlPressed(ControlEvent controlEvent) {
sttB = 1 - sttB; // Pay attention to change pin!!!
pinB.setValue(sttB);
}
});
add(btnB, CENTER + 70, SAME); // The last button will be placed
// to the right of the center.
// A thread will be used to check every 100 ms, if the reset button has been pressed: if yes then
// the pin state goes to LOW
new Thread() {
@Override
public void run() {
while(true){
if(pinPushButton.getValue() == 1) {
sttG = 1 - sttG;
sttR = 1 - sttR;
sttB = 1 - sttB;
pinR.setValue(sttR);
pinG.setValue(sttG);
pinB.setValue(sttB);
}
Vm.sleep(100);
}
}
}.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.