Yestin ParaguyaPaul Rodolf P. Castor
Published © GPL3+

GPIO User Interface with RT-Thread on RT-Spark Board

GPIO-driven UI on RT-Spark with 5-way switch controls 3 LEDs and an LCD in real time using RT-Thread Studio.

IntermediateFull instructions provided4 hours9
GPIO User Interface with RT-Thread on RT-Spark Board

Things used in this project

Hardware components

RT Thread Spark 1 STM32F407ZGT6
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
LED (generic)
LED (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Tactile Switch, SPST-NO
Tactile Switch, SPST-NO
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

RT-Thread IoT OS
RT-Thread IoT OS
Github

Story

Read more

Schematics

GPIO-User-Interface-with-RT-Thread-on-RT-Spark-Board

RT-Spark Architecture

Code

3.3 Complete Source Code

C/C++
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "drv_lcd.h"

// ========================================================
// DIAGNOSTIC TOGGLE
// ========================================================
#define ENABLE_LCD_CODE 1

// 1. Define Pins
#define P_SW_UP     GET_PIN(A, 1)
#define P_SW_DN     GET_PIN(A, 2)
#define P_SW_LT     GET_PIN(A, 3)
#define P_SW_RT     GET_PIN(A, 4)
#define P_SW_CR     GET_PIN(A, 5)
#define P_BACKLIGHT GET_PIN(F, 9)
#define P_LED_1     GET_PIN(E, 2)
#define P_LED_2     GET_PIN(E, 3)
#define P_LED_3     GET_PIN(E, 4)

// 2. Initialize Switches
void switches_init(void) {
    rt_pin_mode(P_SW_UP, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(P_SW_DN, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(P_SW_LT, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(P_SW_RT, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(P_SW_CR, PIN_MODE_INPUT_PULLUP);
}

// 3. Read Switch (active LOW — invert reading)
int switch_get(rt_base_t pin) {
    return !rt_pin_read(pin);
}

// 4. Initialize LEDs (active HIGH — LOW = off at startup)
void leds_init(void) {
    rt_pin_mode(P_LED_1, PIN_MODE_OUTPUT);
    rt_pin_mode(P_LED_2, PIN_MODE_OUTPUT);
    rt_pin_mode(P_LED_3, PIN_MODE_OUTPUT);
    rt_pin_write(P_LED_1, PIN_LOW);
    rt_pin_write(P_LED_2, PIN_LOW);
    rt_pin_write(P_LED_3, PIN_LOW);
}

// 5. Control LEDs
void leds_set(int l1, int l2, int l3) {
    rt_pin_write(P_LED_1, l1 ? PIN_HIGH : PIN_LOW);
    rt_pin_write(P_LED_2, l2 ? PIN_HIGH : PIN_LOW);
    rt_pin_write(P_LED_3, l3 ? PIN_HIGH : PIN_LOW);
}

void light_leds(void) {
    leds_set(
        switch_get(P_SW_UP),
        switch_get(P_SW_CR),
        switch_get(P_SW_DN)
    );
}

// 6. LCD Display Logic (font size = 32px, fixed position x=10, y=10)
void print_switches(void) {
#if ENABLE_LCD_CODE
    if (switch_get(P_SW_UP)) {
        lcd_show_string(10, 10, 32, "UP PRESSED     ");
    } else if (switch_get(P_SW_CR)) {
        lcd_show_string(10, 10, 32, "CENTER PRESSED ");
    } else if (switch_get(P_SW_DN)) {
        lcd_show_string(10, 10, 32, "DOWN PRESSED   ");
    } else if (switch_get(P_SW_LT)) {
        lcd_show_string(10, 10, 32, "LEFT PRESSED   ");
    } else if (switch_get(P_SW_RT)) {
        lcd_show_string(10, 10, 32, "RIGHT PRESSED  ");
    } else {
        lcd_show_string(10, 10, 32, "HELLO WORLD!    ");
    }
#endif
}

// 7. Main Thread
int main(void) {
    switches_init();
    leds_init();

#if ENABLE_LCD_CODE
    rt_pin_mode(P_BACKLIGHT, PIN_MODE_OUTPUT);
    rt_pin_write(P_BACKLIGHT, PIN_HIGH);  // Enable LCD backlight
    rt_thread_mdelay(50);                 // Settle delay
    lcd_clear(WHITE);                     // Clear screen
#endif

    while (1) {
        print_switches();
        light_leds();
        rt_thread_mdelay(50);
    }
    return 0;
}

GPIO-User-Interface-with-RT-Thread-on-RT-Spark-Board

Credits

Yestin Paraguya
2 projects • 0 followers
Paul Rodolf P. Castor
19 projects • 9 followers

Comments