First class data visualization and communication library with embedded devices. Code is maintained at github.com/Overdrivr/Telemetry

Dependents:   telemetry_car_demo telemetry_demo_FRDM-TFC telemetry_example_01 telemetry_indexed_data_demo ... more

Files at this revision

API Documentation at this revision

Comitter:
Overdrivr
Date:
Tue Apr 12 07:40:10 2016 +0000
Parent:
6:f5e1b079bffd
Commit message:
Release 2.0.0

Changed in this revision

Telemetry.cpp Show annotated file Show diff for this revision Revisions of this file
Telemetry.hpp Show annotated file Show diff for this revision Revisions of this file
c_api/dictionnary.cpp Show annotated file Show diff for this revision Revisions of this file
c_api/dictionnary.h Show annotated file Show diff for this revision Revisions of this file
c_api/telemetry_core.cpp Show annotated file Show diff for this revision Revisions of this file
c_api/telemetry_core.h Show annotated file Show diff for this revision Revisions of this file
c_api/telemetry_version.h Show annotated file Show diff for this revision Revisions of this file
--- a/Telemetry.cpp	Wed Mar 09 13:41:27 2016 +0000
+++ b/Telemetry.cpp	Tue Apr 12 07:40:10 2016 +0000
@@ -38,6 +38,41 @@
     pc.baud(bauds);
 }
 
+void Telemetry::attach_f32_to(const char * topic, float * variable)
+{
+    attach_f32(topic, variable);
+}
+
+void Telemetry::attach_u8_to(const char * topic, uint8_t * variable)
+{
+    attach_u8(topic, variable);
+}
+
+void Telemetry::attach_u16_to(const char * topic, uint16_t * variable)
+{
+    attach_u16(topic, variable);
+}
+
+void Telemetry::attach_u32_to(const char * topic, uint32_t * variable)
+{
+    attach_u32(topic, variable);
+}
+
+void Telemetry::attach_i8_to(const char * topic, int8_t * variable)
+{
+    attach_i8(topic, variable);
+}
+
+void Telemetry::attach_i16_to(const char * topic, int16_t * variable)
+{
+    attach_i16(topic, variable);
+}
+
+void Telemetry::attach_i32_to(const char * topic, int32_t * variable)
+{
+    attach_i32(topic, variable);
+}
+
 void Telemetry::begin(uint32_t bauds)
 {
     pc.baud(bauds);
--- a/Telemetry.hpp	Wed Mar 09 13:41:27 2016 +0000
+++ b/Telemetry.hpp	Tue Apr 12 07:40:10 2016 +0000
@@ -20,6 +20,15 @@
 
       void begin(uint32_t bauds = 9600);
 
+      //void attach_to(const char * topic, );
+      void attach_f32_to(const char * topic, float * variable);
+      void attach_u8_to(const char * topic, uint8_t * variable);
+      void attach_u16_to(const char * topic, uint16_t * variable);
+      void attach_u32_to(const char * topic, uint32_t * variable);
+      void attach_i8_to(const char * topic, int8_t * variable);
+      void attach_i16_to(const char * topic, int16_t * variable);
+      void attach_i32_to(const char * topic, int32_t * variable);
+
       TM_transport * get_transport();
 
       void pub(const char * topic, const char * msg);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c_api/dictionnary.cpp	Tue Apr 12 07:40:10 2016 +0000
@@ -0,0 +1,111 @@
+#include "dictionnary.h"
+#include "stdlib.h"
+#include "stdint.h"
+#include "string.h"
+
+/* hash: form hash value for string s */
+unsigned hash(const char * s)
+{
+    unsigned hashval;
+    for (hashval = 0; *s != '\0'; s++)
+      hashval = *s + 31 * hashval;
+    return hashval % HASHSIZE;
+}
+
+char * strdup(const char * s) /* make a duplicate of s */
+{
+    char *p;
+    p = (char *) malloc(strlen(s)+1); /* +1 for ’\0’ */
+    if (p != NULL)
+       strcpy(p, s);
+    return p;
+}
+
+void init_entry(struct nlist * entry)
+{
+  entry->ptr_f32 = NULL;
+  entry->ptr_u8 = NULL;
+  entry->ptr_u16 = NULL;
+  entry->ptr_u32 = NULL;
+  entry->ptr_i8 = NULL;
+  entry->ptr_i16 = NULL;
+  entry->ptr_i32 = NULL;
+  entry->ptr_function = NULL;
+}
+
+void init_table(struct nlist ** hashtab)
+{
+  uint32_t i = 0;
+  for(i = 0 ; i < HASHSIZE ; i++)
+  {
+    hashtab[i] = NULL;
+  }
+}
+
+/* lookup: look for s in hashtab */
+struct nlist *lookup(struct nlist ** hashtab, const char * key)
+{
+    struct nlist *np;
+
+    for (np = hashtab[hash(key)]; np != NULL; np = np->next)
+    {
+      if (strcmp(key, np->key) == 0)
+      {
+        return np; /* found */
+      }
+    }
+    return NULL; /* not found */
+}
+
+struct nlist * install(struct nlist ** hashtab, const char * key, void * ptr, ptr_type type)
+{
+    struct nlist * np;
+    unsigned hashval;
+
+    if ((np = lookup(hashtab, key)) == NULL)
+    {
+        // Allocate new hastable entry and initialize it
+        np = (struct nlist *) malloc(sizeof(*np));
+
+        // If allocation failed
+        if (np == NULL || (np->key = strdup(key)) == NULL)
+          return NULL;
+
+        init_entry(np);
+
+        hashval = hash(key);
+        np->next = hashtab[hashval];
+        hashtab[hashval] = np;
+    }
+
+    // Set value
+    switch(type)
+    {
+        case ptr_f32:
+          np->ptr_f32 = (float *)(ptr);
+          break;
+        case ptr_u8:
+          np->ptr_u8 = (uint8_t *)(ptr);
+          break;
+        case ptr_u16:
+          np->ptr_u16 = (uint16_t *)(ptr);
+          break;
+        case ptr_u32:
+          np->ptr_u32 = (uint32_t *)(ptr);
+          break;
+        case ptr_i8:
+          np->ptr_i8 = (int8_t *)(ptr);
+          break;
+        case ptr_i16:
+          np->ptr_i16 = (int16_t *)(ptr);
+          break;
+        case ptr_i32:
+          np->ptr_i32 = (int32_t *)(ptr);
+          break;
+        case ptr_function:
+          np->ptr_function = ptr;
+          break;
+    }
+
+    return np;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c_api/dictionnary.h	Tue Apr 12 07:40:10 2016 +0000
@@ -0,0 +1,44 @@
+#ifndef TELEMETRY_DICTIONNARY_H_
+#define TELEMETRY_DICTIONNARY_H_
+
+#include "stdint.h"
+
+
+enum ptr_type {
+  ptr_f32 = 0,
+  ptr_u8 = 1,
+  ptr_u16 = 2,
+  ptr_u32 = 3,
+  ptr_i8 = 4,
+  ptr_i16 = 5,
+  ptr_i32 = 6,
+  ptr_function = 8
+};
+
+typedef enum ptr_type ptr_type;
+
+struct nlist { /* table entry: */
+    struct nlist *next; /* next entry in chain */
+    char * key;
+
+    // Table can store for a given key all following pointers
+    float     * ptr_f32;
+    uint8_t   * ptr_u8;
+    uint16_t  * ptr_u16;
+    uint32_t  * ptr_u32;
+    int8_t    * ptr_i8;
+    int16_t   * ptr_i16;
+    int32_t   * ptr_i32;
+    void      * ptr_function;
+};
+
+#define HASHSIZE 101
+
+void init_table(struct nlist ** hashtab);
+
+/* lookup: look for s in hashtab */
+struct nlist * lookup(struct nlist ** hashtab, const char * key);
+
+struct nlist * install(struct nlist ** hashtab, const char * key, void * ptr, ptr_type type);
+
+#endif
--- a/c_api/telemetry_core.cpp	Wed Mar 09 13:41:27 2016 +0000
+++ b/c_api/telemetry_core.cpp	Tue Apr 12 07:40:10 2016 +0000
@@ -1,6 +1,7 @@
 #include "telemetry_core.h"
 #include "framing.h"
 #include "crc16.h"
+#include "dictionnary.h"
 
 static TM_state * statePtr;
 static TM_transport * transportPtr;
@@ -8,6 +9,9 @@
 static uint8_t outgoingBuffer[OUTGOING_BUFFER_SIZE];
 static char topicBuffer[TOPIC_BUFFER_SIZE];
 
+struct nlist * hashtab[HASHSIZE];
+
+
 static void (*userCallback)(TM_state * s, TM_msg * m);
 
 uint16_t header(TM_type type);
@@ -31,6 +35,49 @@
   outgoing_storage(outgoingBuffer, OUTGOING_BUFFER_SIZE);
   set_on_incoming_frame(on_incoming_frame);
   set_on_incoming_error(on_incoming_error);
+
+  // Setup update dictionnary
+  init_table(hashtab);
+}
+
+void attach(const char * name, void (*callback)(TM_msg * m))
+{
+    install(hashtab, name, (void*)(callback), ptr_function);
+}
+
+void attach_f32(const char * name, float * variable)
+{
+    install(hashtab, name, (void*)(variable), ptr_f32);
+}
+
+void attach_u8(const char * name, uint8_t * variable)
+{
+    install(hashtab, name, (void*)(variable), ptr_u8);
+}
+
+void attach_u16(const char * name, uint16_t * variable)
+{
+    install(hashtab, name, (void*)(variable), ptr_u16);
+}
+
+void attach_u32(const char * name, uint32_t * variable)
+{
+    install(hashtab, name, (void*)(variable), ptr_u32);
+}
+
+void attach_i8(const char * name, int8_t * variable)
+{
+    install(hashtab, name, (void*)(variable), ptr_i8);
+}
+
+void attach_i16(const char * name, int16_t * variable)
+{
+    install(hashtab, name, (void*)(variable), ptr_i16);
+}
+
+void attach_i32(const char * name, int32_t * variable)
+{
+    install(hashtab, name, (void*)(variable), ptr_i32);
 }
 
 void publish(const char * t, const char * msg)
@@ -178,6 +225,61 @@
   }
 }
 
+void try_update_hashtable(TM_msg * msg)
+{
+  struct nlist * np = lookup(hashtab, msg->topic);
+
+  // Topic not found
+  if(np == NULL)
+    return;
+
+  switch(msg->type)
+  {
+      case TM_float32:
+        // If hashtable has an entry of type float 32 under received topic
+        if (np->ptr_f32 == NULL)
+          break;
+        emplace_f32(msg, np->ptr_f32);
+        break;
+      case TM_uint8:
+        // If hashtable has an entry of type float 32 under received topic
+        if (np->ptr_u8 == NULL)
+          break;
+        emplace_u8(msg, np->ptr_u8);
+        break;
+      case TM_uint16:
+        // If hashtable has an entry of type float 32 under received topic
+        if (np->ptr_u16 == NULL)
+          break;
+        emplace_u16(msg, np->ptr_u16);
+        break;
+      case TM_uint32:
+        // If hashtable has an entry of type float 32 under received topic
+        if (np->ptr_u32 == NULL)
+          break;
+        emplace_u32(msg, np->ptr_u32);
+        break;
+      case TM_int8:
+        // If hashtable has an entry of type float 32 under received topic
+        if (np->ptr_i8 == NULL)
+          break;
+        emplace_i8(msg, np->ptr_i8);
+        break;
+      case TM_int16:
+        // If hashtable has an entry of type float 32 under received topic
+        if (np->ptr_i16 == NULL)
+          break;
+        emplace_i16(msg, np->ptr_i16);
+        break;
+      case TM_int32:
+        // If hashtable has an entry of type float 32 under received topic
+        if (np->ptr_i32 == NULL)
+          break;
+        emplace_i32(msg, np->ptr_i32);
+        break;
+  }
+}
+
 void on_incoming_frame(uint8_t * storage, uint32_t size)
 {
   if(size < 2)
@@ -230,7 +332,10 @@
   packet.buffer = (void *)(ptr);
   packet.size = (uint32_t)payloadSize;
 
-  // Call callback
+  // Try update variable if found in hash table
+  try_update_hashtable(&packet);
+
+  // Call global handler
   userCallback(statePtr,&packet);
 }
 
--- a/c_api/telemetry_core.h	Wed Mar 09 13:41:27 2016 +0000
+++ b/c_api/telemetry_core.h	Tue Apr 12 07:40:10 2016 +0000
@@ -9,6 +9,15 @@
 #define OUTGOING_BUFFER_SIZE 128
 #define TOPIC_BUFFER_SIZE 64
 
+void attach(const char * name, void (*callback)(TM_msg * m));
+void attach_f32(const char * name, float * variable);
+void attach_u8(const char * name, uint8_t * variable);
+void attach_u16(const char * name, uint16_t * variable);
+void attach_u32(const char * name, uint32_t * variable);
+void attach_i8(const char * name, int8_t * variable);
+void attach_i16(const char * name, int16_t * variable);
+void attach_i32(const char * name, int32_t * variable);
+
 void init_telemetry(TM_transport * t);
 
 void publish(const char * topic, const char * msg);
--- a/c_api/telemetry_version.h	Wed Mar 09 13:41:27 2016 +0000
+++ b/c_api/telemetry_version.h	Tue Apr 12 07:40:10 2016 +0000
@@ -1,8 +1,8 @@
 #ifndef TELEMETRY_VERSION_H_
 #define TELEMETRY_VERSION_H_
 
-#define TELEMETRY_VERSION_MAJOR 1
+#define TELEMETRY_VERSION_MAJOR 2
 #define TELEMETRY_VERSION_MINOR 0
-#define TELEMETRY_VERSION_PATCH 3
+#define TELEMETRY_VERSION_PATCH 0
 
 #endif