Browse Source

Listen Chacon codes

Cyrille 10 years ago
parent
commit
f2a76083c5
6 changed files with 300 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 4 0
      README.md
  3. 10 0
      listen/Makefile
  4. 99 0
      listen/listen.c
  5. 138 0
      listen/listenlib.c
  6. 46 0
      listen/listenlib.h

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+listen/*.o
+listen/listen
+listen/*.txt

+ 4 - 0
README.md

@@ -2,3 +2,7 @@ chacon-rpi
 ==========
 
 This is a set of programs to pilot domotic Chacon modules
+
+listen
+------
+Listen Chacon code

+ 10 - 0
listen/Makefile

@@ -0,0 +1,10 @@
+all : listen
+
+listen : listen.o listenlib.o
+	gcc -o listen listen.o listenlib.o -lwiringPi
+
+listen.o : listen.c
+	gcc -c listen.c
+
+listenlib.o : listenlib.c listenlib.h
+	gcc -c listenlib.c

+ 99 - 0
listen/listen.c

@@ -0,0 +1,99 @@
+#include <stdio.h>
+#include <wiringPi.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <malloc.h>
+#include "listenlib.h"
+
+/**
+ * Usage of this program
+ */
+void usage(char** argv)
+{
+    fprintf(stderr, "Chacon analyzer V1.0\nC. Meichel\n2013, October\n");
+    fprintf(stderr, "Syntaxe : %s [options]\n", argv[0]);
+    fprintf(stderr, "\t-o output:\n\t\toutput file\n");
+    fprintf(stderr, "\t-n number:\n\t\tnumber of bit to analyze (default 100000)\n");
+    fprintf(stderr, "\t-t number:\n\t\ttempo between samples in µs (default 10)\n");
+    fprintf(stderr, "\t-v:\n\t\tverbose\n");
+    fprintf(stderr, "\t-a:\n\t\tperform a complete analyse even if an error was encountered\n");
+}
+
+/**
+ * Main program
+ *
+ * @param argc number of arguments passed to the program
+ * @param argv array of arguments passed to the program
+ *
+ * @return status
+ */
+int main (int argc, char** argv)
+{
+    char optstring[] = "n:t:o:va";
+    unsigned int i;
+    unsigned char previousBit;
+    unsigned char* data;
+    struct timeval* start;
+    int option;
+    unsigned int duration = 10;
+    unsigned long int samples = 100000;
+    unsigned char verbose = 0;
+    unsigned char all = 0;
+    FILE* output=0;
+
+    /* reading options */
+    opterr=0; /* Pas de message d'erreur automatique */
+    while ((option = getopt(argc, argv, optstring)) != -1) {
+        switch (option) {
+            case 't':
+                sscanf(optarg, "%d", &duration);
+                break;
+            case 'n':
+                sscanf(optarg, "%lu", &samples);
+                break;
+            case 'a':
+                all = 1;
+                break;
+            case 'v':
+                verbose = 1;
+                break;
+            case 'o':
+                output = fopen(optarg, "w");
+                if (output==0) {
+                    fprintf(stderr, "Could not open file %s\n", optarg);
+                }
+                break;
+            default:
+                usage(argv);
+                return 0;
+                break;
+        }
+    }
+
+    /* Configure the GPIO */
+    wiringPiSetup () ;
+    pinMode (LED, INPUT);
+
+    /* Read the data */
+    fprintf(stderr, "Reading data\n");
+    data = (unsigned char*)malloc(samples);
+    readData(data, samples, duration);
+
+    /* Analyzing the data */
+    fprintf(stderr, "Analyzing\n");
+    analyse(data, samples, output ? output : stdout, all);
+
+    if (verbose) {
+        fprintf(stdout, "\n\nRawData\n");
+        previousBit=0;
+        for(i=0; i<samples; i++) {
+           if ((previousBit == 0) && (data[i] == 1))
+               fprintf(stdout, "\n");
+           fprintf(stdout, "%c", data[i]+'0');
+           previousBit = data[i];
+       }
+    }
+
+    free(data);
+    return 0 ;
+}

+ 138 - 0
listen/listenlib.c

@@ -0,0 +1,138 @@
+#include <stdio.h>
+#include <wiringPi.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <malloc.h>
+#include "listenlib.h"
+
+
+/**
+ * Calculate a duration
+ *
+ * @param start start of the computing or null if
+ * this is the first time this function is inoked
+ *
+ * @return current time
+ */
+struct timeval* showTime(struct timeval* start)
+{
+    struct timeval* stop;
+    long tv_sec;
+    long tv_usec;
+    stop = (struct timeval*)malloc(sizeof(struct timeval));
+    gettimeofday(stop, 0);
+    if (!start) return stop;
+    tv_sec = (start->tv_usec > stop->tv_usec) ? stop->tv_sec - start->tv_sec - 1 : stop->tv_sec - start->tv_sec;
+    tv_usec = (start->tv_usec > stop->tv_usec) ? 1000000 + stop->tv_usec - start->tv_usec : stop->tv_usec - start->tv_usec;
+    fprintf(stderr, "%lus %lums %luµs\n", tv_sec, tv_usec/1000, tv_sec % 1000);
+    return stop;
+}
+
+/**
+ * Reading data from GPIO
+ *
+ * @param data buffer data
+ * @param samples number of samples to read
+ * @param duration waiting time between samples
+ */
+void readData(unsigned char* data, unsigned long int samples, unsigned int duration)
+{
+    struct timeval* start;
+    unsigned long int i;
+    start = showTime(0);
+    for(i=0; i<samples; i++) {
+        data[i] = digitalRead(LED);
+        usleep(duration);
+    }
+    data[samples] = 123;
+    showTime(start);
+}
+
+/**
+ * Calculate the length of the frame
+ *
+ * @param data data to scan
+ * @param high length of high level
+ * @param low length of low level
+ *
+ * @return total length of the frame
+ */
+unsigned int frameSize(unsigned char* data, unsigned int* high, unsigned int* low)
+{
+    unsigned int i=0;
+    if (high) *high = 0;
+    if (data[i] == 1) {
+        for(i=0; data[i]; i++) ;
+        if (high) *high = i;
+    }
+    for(;  !data[i]; i++) ;
+    if (low) *low = i - *high;
+    return i;
+}
+
+/**
+ * Analyse the data
+ *
+ * @param data buffer data
+ * @param samples number of samples to read
+ * @param output output to render the result
+ * @param all if false the analyze stops at the first error encoutered
+ */
+void analyse(unsigned char* data, unsigned long int samples, FILE* output, unsigned char all)
+{
+    unsigned long int cursor=0;
+    int detectMarker = 1;
+    int currentMarker = 0;
+    unsigned int lowLengthFrame;
+    unsigned int highLengthFrame;
+    unsigned int totalLengthFrame;
+    unsigned char currentByte = 0;
+    unsigned char bitNumber = 0;
+    unsigned char startData = 0;
+    while (cursor<samples) {
+        totalLengthFrame = frameSize(&data[cursor], &highLengthFrame, &lowLengthFrame);
+        if (currentMarker>0) {
+            /* Try to detect a start frame marker */
+            if ((lowLengthFrame > 20) && (lowLengthFrame<50)) {
+                detectMarker++;
+                bitNumber = 0;
+                currentByte = 0;
+                fprintf(output, "\n%02d: ", detectMarker);
+            } else {
+                currentMarker = detectMarker;
+            }
+            /* we are in the data range */
+            if (currentMarker == detectMarker) {
+                /* Push bit */
+                currentByte = currentByte << 1;
+                if ((lowLengthFrame<2) || (highLengthFrame>5)) {
+                    fprintf(output, "There might be an error from here\n");
+                    if (!all) return;
+		}
+                currentByte += (lowLengthFrame < 5 ? 0 : 1);
+                /* check if the byte is completed */
+                if (bitNumber == 7) {
+                    bitNumber = 0;
+                    fprintf(output, "%02X ", currentByte);
+                    currentByte = 0;
+                } else {
+                   bitNumber++;
+                }
+            }
+            /* We are in the data range */
+        } else {
+            if ((lowLengthFrame > 20) && (lowLengthFrame<50) && (startData)) {
+                currentMarker = 1;
+                currentByte = 0;
+                bitNumber = 0;
+                fprintf(output, "DataStart at %lu\n", cursor);
+                fprintf(output, "\n%02d: ", detectMarker);
+            }
+            if (lowLengthFrame > 100) {
+                startData = 1;
+            }
+        }
+        cursor += totalLengthFrame;
+    }
+}
+

+ 46 - 0
listen/listenlib.h

@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <sys/time.h>
+
+// LED Pin - wiringPi pin 0 is BCM_GPIO 17.
+#define	LED	0
+
+
+/**
+ * Calculate a duration
+ *
+ * @param start start of the computing or null if
+ * this is the first time this function is inoked
+ *
+ * @return current time
+ */
+struct timeval* showTime(struct timeval* start);
+
+/**
+ * Reading data from GPIO
+ *
+ * @param data buffer data
+ * @param samples number of samples to read
+ * @param duration waiting time between samples
+ */
+void readData(unsigned char* data, unsigned long int samples, unsigned int duration);
+
+/**
+ * Calculate the length of the frame
+ *
+ * @param data data to scan
+ * @param high length of high level
+ * @param low length of low level
+ *
+ * @return total length of the frame
+ */
+unsigned int frameSize(unsigned char* data, unsigned int* high, unsigned int* low);
+
+/**
+ * Analyse the data
+ *
+ * @param data buffer data
+ * @param samples number of samples to read
+ * @param output output to render the result
+ * @param all if false the analyze stops at the first error encoutered
+ */
+void analyse(unsigned char* data, unsigned long int samples, FILE* output, unsigned char all);