# Understanding TotalCross for Linux ARM

## Introduction

See at this guide:

* Discover plugin for VS Code;
* Getting your Hello World App cooler;
* How to deploy;&#x20;
* After basics;

## Requirements

Complete the Getting Started:

{% content-ref url="../get-started" %}
[get-started](https://learn.totalcross.com/master/documentation/get-started)
{% endcontent-ref %}

The following electronic components are also required:

* Raspberry Pi 3;
* 7x jumpers male-female;
* Protoboard;
* LED RGB module (or common 4 pins LED RGB);
* Push-button module (or common push-button).

In order to execute Gpiod methods at your embedded device, you will also need to have the libgpiod-dev package installed in your board. You can do that by entering the following command at the device's terminal:

```java
$ sudo apt-get install libgpiod-dev
```

## **Guide**

### Discover VS Code plugin

A quick way to start using TotalCross is installing the [TotalCross extension for VS Code](https://marketplace.visualstudio.com/items?itemName=Italo.totalcross).&#x20;

**Step 1:** open VS Code console  (CTRL + Shift + P) and type TotalC… autocomplete should help!

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-Lun83v5b4ERDVyE7VT-%2F3%5B1%5D.gif?alt=media\&token=7d6dd1d4-7cef-4980-af61-7752fb0805c0)

**Step 2:** select *TotalCross: Create new Project;*&#x20;

**Step 3:** create a folder called *HelloWorld* and select it;&#x20;

**Step 4:** *GroupId* will be `com.totalcross`;

**Step 5:** *ArtifactId* will be `HelloWorld`*;*

**Step 6:** select the latest version of TotalCross SDK and `-linux-arm` platform;

![Click to expand](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-Lun8cZb1egyD-mqZBhj%2F4%5B1%5D.gif?alt=media\&token=a47a9232-5721-46c0-898e-3b494a8abb27)

**Step 7:** open`RunHelloWorldApplication.java` and click *Run* (IDE). The result should be:

![Click  to expand](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-Lun9-JBKRLqV_24--Q4%2F5%5B1%5D.gif?alt=media\&token=dd1ae1f0-1174-43f5-b39f-7ae41c883b17)

**Step 8:** watch the integrated simulator!

### Getting your Hello World App even more Cooler :cold\_face: :sweat\_smile:&#x20;

The following project deals with the control of an RGB LED with user interface buttons and a pin reset button!

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LzgpB3yjx44V1Oh-Uxv%2F-LzgpT8LCC3-7z58c_wO%2Fgpiotutorial.jpg?alt=media\&token=0c630b81-2f63-4952-abda-91000f10a1b6)

**Step 1:** follow the schematic:

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LzcfBCrkAb7vk8QOHul%2F-Lzcj5bEWsjwrkCoCK58%2Funderstanding_linux_arm.png?alt=media\&token=0f98176c-0c62-4d95-a2a8-682d7ef56d8c)

**Step 2:** to work with pin logic after `public class HelloWorld extends MainWindow {` add:

```java
// 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;
```

{% hint style="danger" %}
If you need to work with different pinouts check the manufacturer manual!
{% endhint %}

**Step 3:** at  *HelloWorld.java* in `initUI()` code add:

```java
// 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);
```

**Step 4:** then, board setup:

```java
// 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);
```

**Step 5:** pins setup:

```java
// 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");
```

**Step 6:** the red button:

```java
// 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
```

**Step 7:** and the other buttons:&#x20;

```java
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.
```

**Step 8:** finally we use a thread to check the state of the reset button:

```java
// A thread will be used to check every 20 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();
```

**Step 9:** run *RunHelloWorldApplication.java* again and watch the results!&#x20;

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-LunDOEt3v1aVNDLqxjG%2F6%5B1%5D.gif?alt=media\&token=2ede5627-276c-4eee-a215-05f53b400fac)

{% hint style="info" %}
View fully code [here](https://gist.github.com/acmlira/e6c18f0a82688f750c1648af4d101344)&#x20;
{% endhint %}

### How to deploy

**Step 1:** open VS Code console  (CTRL + Shift + P) and select *TotalCross: Deploy*

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-LunDi6Z-vuOMZiz2XLo%2F7%5B1%5D.gif?alt=media\&token=3280501b-34bd-487d-be97-cc8817ba7dc3)

**Step 2:** a second dialog box will appear and just fill in the board's information:

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-LunDoXYlF_oU9Y39NP7%2F8%5B1%5D.gif?alt=media\&token=358c321d-cba1-4d2b-8813-fb35f62ca043)

**Step 3:** see the results in screen or VNC

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-LunDsKM3Yzmhnvpb_Ub%2F9%5B1%5D.gif?alt=media\&token=a7ca4720-d28f-4098-99ae-af500791439f)

### After basics

This was the beginning of application development for TotalCross embedded systems, how about taking a look at [TCSample](https://github.com/TotalCross/TCSample) and seeing all that can be done? See dashboard made especially for Web Summit 2019:

![](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-LunFODqxapPPckKLGiW%2Fvideo.gif?alt=media\&token=6cd8e1db-a228-4348-ad91-2f3decec5f3c)

## See more

Are you interested in development with embedded systems? Contact us via [Telegram](https://t.me/comunidadetotalcross)!&#x20;

![Bruno Muniz, Lucas Galvanini e Pedro Lyra no WebSummit](https://1879575642-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_mPP3a_E_A7NbRMq7Q%2F-LumnOTtg2X_vYInc3Vo%2F-LunDx-hDEZniSPFP55o%2Fimg3%5B1%5D.jpeg?alt=media\&token=70616025-91c2-4db5-9724-665f992d0b7a)

## References

* [Fritzing ](https://fritzing.org/home/)
