SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
portable/led.cpp
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#include <stdint.h>
11#include <stdio.h>
12#include <assert.h>
13#include <time.h>
14#include "../led.h"
15
16using namespace bsp;
17
18static bool g_LedState[LED_MAX] = {};
19
20static const char *Led_GetPin(Led::Id led)
21{
22 switch (led)
23 {
24 case LED_RED: return "RED";
25 case LED_ORANGE: return "ORANGE";
26 case LED_GREEN: return "GREEN";
27 case LED_BLUE: return "BLUE";
28 default:
29 return "UNK";
30 }
31}
32
33static void PrintMsg(const char *label, Led::Id led, bool state)
34{
35 static const time_t g_SecNow = time(NULL);
36 time_t now = time(NULL);
37
38 printf("%ds [%s]: %s - %s\n", (int)(now - g_SecNow), label, Led_GetPin(led), (state ? "ON" : "OFF"));
39}
40
41void Led::Init(Id led, bool init_state)
42{
43 // required to show log in Eclipse IDE Console for 64-bit binary
44#if defined(_WIN32) && !defined(_MSC_VER)
45 static bool s_init = false;
46 if (!s_init)
47 {
48 setbuf(stdout, NULL);
49 s_init = true;
50 }
51#endif
52
53 if (static_cast<uint32_t>(led) < LED_MAX)
54 g_LedState[led] = init_state;
55
56 PrintMsg("LED_INIT", led, init_state);
57}
58
59void Led::Set(Id led, bool state)
60{
61 if (static_cast<uint32_t>(led) < LED_MAX)
62 {
63 if (g_LedState[led] == state)
64 return;
65
66 g_LedState[led] = state;
67 }
68
69 PrintMsg("LED_SET_STATE", led, state);
70}
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_MAX
Definition led.h:22
@ LED_BLUE
Definition led.h:20
@ LED_RED
Definition led.h:19
time_t time(time_t *tloc)
Definition cpu.h:17
LedId Id
Definition led.h:32
static void Init(Id led, bool init_state)
static void Set(Id led, bool state)
static const char * Led_GetPin(Led::Id led)
static bool g_LedState[LED_MAX]
static void PrintMsg(const char *label, Led::Id led, bool state)