6
6
7
7
#include "fsemu-util.h"
8
8
9
+ // FIXME: DEBUG -> INFO;
10
+ int fsemu_led_log_level = FSEMU_LOG_LEVEL_DEBUG ;
11
+
9
12
struct fsemu_led_t {
10
13
char * id ;
11
14
char * label ;
12
15
fsemu_led_state_t state ;
16
+ int brightness ;
17
+ bool changed ;
13
18
};
14
19
15
20
void fsemu_led_init (fsemu_led_t * led )
16
21
{
17
- fsemu_led_set_id (led , "" );
18
- fsemu_led_set_label (led , "" );
22
+ // Memory might not be initialized to zeros, all fields must be
23
+ // initialized here.
24
+ led -> id = strdup ("" );
25
+ led -> label = strdup ("" );
26
+ led -> state = 0 ;
27
+ led -> brightness = 100 ;
28
+ // Set changed to one initially, useful for check in renderer.
29
+ led -> changed = 1 ;
19
30
}
20
31
21
32
fsemu_led_t * fsemu_led_create (void )
@@ -33,10 +44,14 @@ const char *fsemu_led_id(fsemu_led_t *led)
33
44
void fsemu_led_set_id (fsemu_led_t * led , const char * id )
34
45
{
35
46
fsemu_assert (id != NULL );
36
- if (led -> id ) {
37
- free ( led -> id ) ;
47
+ if (strcmp ( id , led -> id ) == 0 ) {
48
+ return ;
38
49
}
50
+ // if (led->id) {
51
+ free (led -> id );
52
+ // }
39
53
led -> id = strdup (id );
54
+ led -> changed = true;
40
55
}
41
56
42
57
const char * fsemu_led_label (fsemu_led_t * led )
@@ -47,10 +62,14 @@ const char *fsemu_led_label(fsemu_led_t *led)
47
62
void fsemu_led_set_label (fsemu_led_t * led , const char * label )
48
63
{
49
64
fsemu_assert (label != NULL );
50
- if (led -> label ) {
51
- free ( led -> label ) ;
65
+ if (strcmp ( label , led -> label ) == 0 ) {
66
+ return ;
52
67
}
68
+ // if (led->label) {
69
+ free (led -> label );
70
+ // }
53
71
led -> label = strdup (label );
72
+ led -> changed = true;
54
73
}
55
74
56
75
fsemu_led_state_t fsemu_led_state (fsemu_led_t * led )
@@ -60,5 +79,35 @@ fsemu_led_state_t fsemu_led_state(fsemu_led_t *led)
60
79
61
80
void fsemu_led_set_state (fsemu_led_t * led , fsemu_led_state_t state )
62
81
{
82
+ if (state == led -> state ) {
83
+ return ;
84
+ }
85
+ fsemu_assert (state >= 0 && state < FSEMU_LED_MAX_STATES );
63
86
led -> state = state ;
87
+ // printf("LED \"%s\" -> state %d\n", led->label, state);
88
+ led -> changed = true;
89
+ }
90
+
91
+ int fsemu_led_brightness (fsemu_led_t * led )
92
+ {
93
+ return led -> brightness ;
94
+ }
95
+
96
+ void fsemu_led_set_brightness (fsemu_led_t * led , int brightness )
97
+ {
98
+ if (brightness == led -> brightness ) {
99
+ return ;
100
+ }
101
+ led -> brightness = brightness ;
102
+ // printf("LED \"%s\" -> brightness %d\n", led->label, brightness);
103
+ led -> changed = true;
104
+ }
105
+
106
+ bool fsemu_led_check_and_reset_changed (fsemu_led_t * led )
107
+ {
108
+ if (led -> changed ) {
109
+ led -> changed = false;
110
+ return true;
111
+ }
112
+ return false;
64
113
}
0 commit comments