8 extern uint8_t *mem_top
, *mem_bottom
, *mem_task
, *mem_sds
;
10 struct sds
*sds_head(void)
12 return mem_sds
== mem_top
? NULL
13 : (struct sds
*)(mem_top
-sizeof(struct sds
));
16 struct sds
*sds_next(struct sds
*s
)
18 uint8_t *next
= (uint8_t *)s
- sizeof(struct sds
);
19 return next
< mem_sds
? NULL
: (struct sds
*)next
;
22 struct sds
*sds_get(uint8_t id
)
24 struct sds
*s
= sds_head();
34 void sds_register(void)
36 debug("free memory: %lu", mem_free());
38 if(mem_sds
-sizeof(struct sds
) < mem_task
){
39 die("Out of memory... Not enough to allocate sds struct");
42 struct sds
*s
= (struct sds
*)(mem_sds
-sizeof(struct sds
));
48 s
->type
= read_byte();
55 mem_sds
-= sizeof(struct sds
);
58 void sds_delete(uint8_t c
)
60 struct sds
*s
= sds_get(c
);
63 //We found the sds, now we move verything from the end of the sds up to
64 //the pointer to the right
65 uint8_t *end
= (uint8_t *)s
;
66 uint8_t *start
= end
+ sizeof(struct sds
) - 1;
67 for(int i
= 0; i
<end
-mem_sds
; i
++){
68 *(start
- i
) = *(end
- i
-1);
70 mem_sds
+= start
-end
+1;
74 bool sds_update(uint8_t id
)
77 struct sds
*s
= sds_get(id
);
90 s
->value
= read_byte();
93 debug("Received sds update %d: %d", s
->id
, s
->value
);
102 void sds_publish(int id
)
104 struct sds
*s
= sds_get(id
);
106 debug("Publish %d=%d", s
->id
, s
->value
);
119 case 'c': //Character
122 write_byte(s
->value
);
128 debug("SDS identifier unknown: %d", id
);
133 int sds_fetch(int id
)
135 struct sds
*s
= sds_get(id
);
139 debug("SDS identifier unknown: %d", id
);
144 void sds_store(int id
, int val
)
146 struct sds
*s
= sds_get(id
);
150 debug("SDS identifier unknown: %d", id
);