aboutsummaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/sign_logic/CMakeLists.txt4
-rw-r--r--components/sign_logic/include/sign_logic.h30
-rw-r--r--components/sign_logic/sign_logic.c25
-rw-r--r--components/sign_logic/test/CMakeLists.txt5
-rw-r--r--components/sign_logic/test/test_sign_logic.c38
5 files changed, 102 insertions, 0 deletions
diff --git a/components/sign_logic/CMakeLists.txt b/components/sign_logic/CMakeLists.txt
new file mode 100644
index 0000000..53e9a30
--- /dev/null
+++ b/components/sign_logic/CMakeLists.txt
@@ -0,0 +1,4 @@
+idf_component_register(
+ SRCS "sign_logic.c"
+ INCLUDE_DIRS "include"
+)
diff --git a/components/sign_logic/include/sign_logic.h b/components/sign_logic/include/sign_logic.h
new file mode 100644
index 0000000..ca296cb
--- /dev/null
+++ b/components/sign_logic/include/sign_logic.h
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define SIGN_BRIGHTNESS_MIN 0u
+#define SIGN_BRIGHTNESS_MAX 100u
+#define SIGN_BRIGHTNESS_DEFAULT 75u
+
+typedef struct {
+ bool is_on_air;
+ uint8_t brightness;
+} sign_state_t;
+
+typedef enum {
+ SIGN_OK = 0,
+ SIGN_ERR_NULL = 1,
+ SIGN_ERR_RANGE = 2,
+} sign_result_t;
+
+sign_result_t sign_set_on_air(sign_state_t *state, bool on_air);
+sign_result_t sign_set_brightness(sign_state_t *state, uint8_t brightness);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/components/sign_logic/sign_logic.c b/components/sign_logic/sign_logic.c
new file mode 100644
index 0000000..a8ceb0b
--- /dev/null
+++ b/components/sign_logic/sign_logic.c
@@ -0,0 +1,25 @@
+#include <stddef.h>
+
+#include "sign_logic.h"
+
+sign_result_t sign_set_on_air(sign_state_t *state, bool on_air) {
+ if (state == NULL) {
+ return SIGN_ERR_NULL;
+ }
+
+ state->is_on_air = on_air;
+ return SIGN_OK;
+}
+
+sign_result_t sign_set_brightness(sign_state_t *state, uint8_t brightness) {
+ if (state == NULL) {
+ return SIGN_ERR_NULL;
+ }
+
+ if (brightness > SIGN_BRIGHTNESS_MAX) {
+ return SIGN_ERR_RANGE;
+ }
+
+ state->brightness = brightness;
+ return SIGN_OK;
+}
diff --git a/components/sign_logic/test/CMakeLists.txt b/components/sign_logic/test/CMakeLists.txt
new file mode 100644
index 0000000..2f6f0f1
--- /dev/null
+++ b/components/sign_logic/test/CMakeLists.txt
@@ -0,0 +1,5 @@
+idf_component_register(
+ SRCS "test_sign_logic.c"
+ INCLUDE_DIRS "../include"
+ REQUIRES unity sign_logic
+)
diff --git a/components/sign_logic/test/test_sign_logic.c b/components/sign_logic/test/test_sign_logic.c
new file mode 100644
index 0000000..9985702
--- /dev/null
+++ b/components/sign_logic/test/test_sign_logic.c
@@ -0,0 +1,38 @@
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "sign_logic.h"
+
+static void test_sign_set_on_air_updates_state(void) {
+ sign_state_t state = {.is_on_air = false, .brightness = SIGN_BRIGHTNESS_DEFAULT};
+
+ assert(sign_set_on_air(&state, true) == SIGN_OK);
+ assert(state.is_on_air == true);
+}
+
+static void test_sign_set_brightness_rejects_out_of_range(void) {
+ sign_state_t state = {.is_on_air = false, .brightness = SIGN_BRIGHTNESS_DEFAULT};
+
+ assert(sign_set_brightness(&state, 101) == SIGN_ERR_RANGE);
+ assert(state.brightness == SIGN_BRIGHTNESS_DEFAULT);
+}
+
+static void test_sign_set_brightness_accepts_valid_range(void) {
+ sign_state_t state = {.is_on_air = false, .brightness = SIGN_BRIGHTNESS_DEFAULT};
+
+ assert(sign_set_brightness(&state, 0) == SIGN_OK);
+ assert(state.brightness == 0);
+
+ assert(sign_set_brightness(&state, 100) == SIGN_OK);
+ assert(state.brightness == 100);
+}
+
+int main(void) {
+ test_sign_set_on_air_updates_state();
+ test_sign_set_brightness_rejects_out_of_range();
+ test_sign_set_brightness_accepts_valid_range();
+
+ printf("sign_logic tests passed\n");
+ return 0;
+}