X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Fsds.c;h=2570017c8738ef35cd6552a0d9a7f50736b3dd94;hb=a81623aa9629a3c5c8f8bf920e494a596b30c4b7;hp=c5f69b653a5d23416b9c9534238c55c647976ee9;hpb=6c8939998c64aafb8cdfa40e52a227bf72767648;p=mTask.git diff --git a/client/sds.c b/client/sds.c index c5f69b6..2570017 100644 --- a/client/sds.c +++ b/client/sds.c @@ -1,86 +1,129 @@ #include #include -#ifndef STM -#include -#include -#endif - #include "interface.h" -#include "interpret.h" #include "sds.h" +#include "mem.h" -struct sds sdss[MAXSDSS]; -uint8_t c; +extern uint8_t *mem_top, *mem_bottom, *mem_task, *mem_sds; -void sds_init(void) +struct sds *sds_head(void) { - memset(&sdss, 0, sizeof(struct sds)*MAXSDSS); + return mem_sds == mem_top ? NULL + : (struct sds *)(mem_top-sizeof(struct sds)); } -void sds_register(void) +struct sds *sds_next(struct sds *s) +{ + uint8_t *next = (uint8_t *)s - sizeof(struct sds); + return next < mem_sds ? NULL : (struct sds *)next; +} + +struct sds *sds_get(uint8_t id) { - uint8_t cs; - for(cs = 0; csid == id){ break; + } + s = sds_next(s); + } + return s; +} - if(cs == MAXSDSS) - die("Trying to add too much sdss..."); +void sds_register(void) +{ + debug("free memory: %lu", mem_free()); + + if(mem_sds-sizeof(struct sds) < mem_task){ + die("Out of memory... Not enough to allocate sds struct"); + } + + struct sds *s = (struct sds *)(mem_sds-sizeof(struct sds)); - memset(&sdss[cs], 0, sizeof(struct sds)); //Read identifier - sdss[cs].id = read16(); + s->id = read16(); + //Read value - sdss[cs].value = read16(); + s->type = read_byte(); + s->value = read16(); + + write_byte('s'); + write16(s->id); + write_byte('\n'); - debug("Received sds %d: %d", sdss[cs].id, sdss[cs].value); - sdss[cs].used = true; + mem_sds -= sizeof(struct sds); } -bool sds_update(void) +void sds_delete(uint8_t c) { - uint8_t cs, id; - //Read identifier - id = read16(); - - for(cs = 0; csvalue = read16(); + debug("Received sds update %d: %d", s->id, s->value); + write_byte('u'); + write16(s->id); + write_byte('\n'); + return true; } return false; } void sds_publish(int id) { - uint8_t cs; - for(cs = 0; csid, s->value); + write_byte('p'); + write16(s->id); + write_byte(s->type); + + switch(s->type){ + //Long + case 'l': + //Int + case 'i': + write16(s->value); + break; + case 'b': //Bool + case 'c': //Character + case 'B': //Button + case 'L': //UserLED + write_byte(s->value); + break; } + write_byte('\n'); + return; + + } else { + debug("SDS identifier unknown: %d", id); + die(""); } - debug("SDS identifier unknown: %d", id); - die(""); } int sds_fetch(int id) { - uint8_t cs; - for(cs = 0; csvalue; + } debug("SDS identifier unknown: %d", id); die(""); return 0; @@ -88,13 +131,11 @@ int sds_fetch(int id) void sds_store(int id, int val) { - uint8_t cs; - for(cs = 0; csvalue = val; + } else { + debug("SDS identifier unknown: %d", id); + die(""); } - debug("SDS identifier unknown: %d", id); - die(""); }