Browse Source

frame redafinition in unsigned long int

Cyrille Meichel 10 years ago
parent
commit
a759acd336
5 changed files with 70 additions and 91 deletions
  1. 6 16
      emit/emit.c
  2. 37 37
      emit/home_easy.c
  3. 10 8
      emit/home_easy.h
  4. 1 1
      emit/test/Makefile
  5. 16 29
      emit/test/test.c

+ 6 - 16
emit/emit.c

@@ -45,6 +45,7 @@ int main(int argc, char** argv)
     unsigned char number = 1;
     unsigned char section = 'A';
     char *idString=0;
+    unsigned long int id=0;
     unsigned char onOff = ON;
     unsigned int verbose = 0;
     unsigned repeat = 5;
@@ -67,13 +68,7 @@ int main(int argc, char** argv)
                 break;
             case 'd':
                 // command id
-                idString = (char*)malloc(1+strlen(optarg)/2);
-                for(i=0; i<strlen(optarg)/2 + strlen(optarg) % 2; i++) {
-                    idString[i] = (optarg[i*2] - (optarg[i*2]<='9' ? '0' : (optarg[i*2]<='Z' ? 'A' : 'a')-10)) << 4;
-                    if (optarg[1+i*2]) {
-                        idString[i] += optarg[1+i*2] - (optarg[1+i*2]<='9' ? '0' : (optarg[1+i*2]<='Z' ? 'A' : 'a')-10);
-                    }
-                }
+                sscanf(optarg, "%08X", &id);
                 break;
             case 'x':
                 // OFF ?
@@ -90,13 +85,8 @@ int main(int argc, char** argv)
         }
     }
 
-    // No ID specified, it will be "00 00 00 00 00 00 0"
-    if (!idString) {
-        idString = (char*)malloc(7);
-        memset(idString, 0, 7);
-    }
-
     // Show informations
+    printf("device id: %08X\n", id);
     if (section == 'G') {
         printf("Sending command G: %s\n", (onOff == OFF) ? "OFF" : "ON");
     } else {
@@ -105,8 +95,8 @@ int main(int argc, char** argv)
 
     // Display mor information
     if (verbose) {
-        command = createHomeEasyCommand(idString, section, number, onOff);
-        printfByteBuffer(command);
+        command = createHomeEasyCommand(id, section, number, onOff);
+        printf("Frame to send: %08X\n", command);
 
         printf("Code to emit:\n");
         encoded = homeEasyEncode(&command);
@@ -115,7 +105,7 @@ int main(int argc, char** argv)
 
     // Send the data
     initIO();
-    sendHomeEasyCommand(idString, section, number, onOff, repeat);
+    sendHomeEasyCommand(id, section, number, onOff, repeat);
 
     // release the memory
     destroyByteBuffer(command);

+ 37 - 37
emit/home_easy.c

@@ -2,7 +2,22 @@
 #include <unistd.h>
 #include <malloc.h>
 #include <string.h>
-#include <wiringPi.h>
+
+#ifdef __arm__
+    #include <wiringPiSPI.h>
+#else
+    #define LOW 0
+    #define HIGH 1
+    #define INPUT 0
+    #define OUTPUT 1
+    int wiringPiSetup(void) {return 0;}
+    void pinMode (int a, int b) {a=b;}
+    void delayMicroseconds(unsigned int a) {a=a;}
+    void digitalWrite(int a, int b) {a=b;}
+    int digitalRead(int a) {return a;}
+    
+#endif
+
 #include "home_easy.h"
 #include "buffer.h"
 #include "utils.h"
@@ -24,16 +39,16 @@ unsigned char homeEasyPinIn = 2;
 /**
  * Encode bits with HomeEasy encoding (1 => 10, 0 => 01)
  *
- * @param buffer the buffuer to encode
+ * @param frame 32-bit frame to encode
  *
  * @return new buffer
  * */
-BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer)
+BYTE_BUFFER homeEasyEncode(unsigned long int frame)
 {
     BYTE_BUFFER result = createByteBuffer();
     unsigned int i;
-    for(i=0; i<buffer->size; i++) {
-        pushWord(&result, encodeByte(buffer->data[i]));
+    for(i=0; i<4; i++) {
+        pushWord(&result, encodeByte((frame >> ((3 - i) * 8)) & 0xff));
     }
     return result;
 }
@@ -43,18 +58,18 @@ BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer)
  *
  * @param buffer the buffuer to decode
  *
- * @return new buffer
+ * @return new frame
  * */
-BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer)
+unsigned long int homeEasyDecode(BYTE_BUFFER *buffer)
 {
-    BYTE_BUFFER result = createByteBuffer();
+    unsigned long int result = 0;
     unsigned short int word;
     unsigned char *byte = (unsigned char*)&word;
     unsigned int i;
     for(i=0; i<(buffer->size+1)/2; i++) {
         byte[1] = buffer->data[2*i];
         byte[0] = (2*i+1 < buffer->size ? buffer->data[2*i+1]:0);
-        pushByte(&result, decodeByte(word));
+        result += (decodeByte(word)) << ((3 - i)* 8);
     }
     return result;
 }
@@ -104,24 +119,21 @@ unsigned short int encodeByte(unsigned char byte)
  * @param section button section ('A' | 'B'  | 'C'  | 'D'  | 'G')
  * @param nb button number(1, 2, 3, 4)
  * @param on boolean for on/off
+ * 
+ * @return HomeEasy frame
  */
-BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on)
+unsigned long int createHomeEasyCommand(unsigned long int id, char section, unsigned char nb, unsigned char on)
 {
-    unsigned char last = id[3] & 0xc0;
-    BYTE_BUFFER command;
+    unsigned long int command = id << 6;
     char formatedSection = (section<='Z' ? section : section + 'A' - 'a');
     // adding global
-    last |= (formatedSection == 'G' ? 0x20 : 0);
+    command |= (formatedSection == 'G' ? 0x20 : 0);
     // adding on/off
-    last |= (on ? 0x10 : 0);
+    command |= (on ? 0x10 : 0);
     // adding section
-    last |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c;
+    command |= ((formatedSection == 'G' ? 0 : formatedSection - 'A') << 2) & 0x0c;
     // adding num
-    last |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;;
-    //Preparing output buffer
-    command = createByteBuffer();
-    pushBytes(&command, id, 3);
-    pushByte(&command, last);
+    command |= (formatedSection == 'G' ? 0 : nb-1) & 0x03;;
     return command;
 }
 
@@ -158,18 +170,17 @@ void sendFrame(BYTE_BUFFER frame, unsigned int repeat)
  * @param on boolean for on/off
  * @param repeat number of repeatition
  */
-void sendHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on, unsigned char repeat)
+void sendHomeEasyCommand(unsigned long int id, char section, unsigned char nb, unsigned char on, unsigned char repeat)
 {
-    BYTE_BUFFER command;
+    unsigned long int command;
     BYTE_BUFFER encoded;
     // build the command
     command = createHomeEasyCommand(id, section, nb, on);
     // encode the command
-    encoded = homeEasyEncode(&command);
+    encoded = homeEasyEncode(command);
     // send data
     sendFrame(encoded, repeat);
     // release the memory
-    destroyByteBuffer(command);
     destroyByteBuffer(encoded);
 }
 
@@ -180,20 +191,9 @@ void sendHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsi
  *
  * @return the device id
  */
-unsigned long int getHomeEasyId(BYTE_BUFFER buffer)
+unsigned long int getHomeEasyId(unsigned long int frame)
 {
-    unsigned long int id = 0;
-    unsigned long int byte;
-    int i;
-    // check that the frame is 32 bits
-    if (buffer.size != 4) {
-        return 0;
-    }
-    for (i=buffer.size; i>0; i--) {
-        byte = (unsigned long int) buffer.data[i-1];
-        id += byte <<  (8 * (buffer.size - i));
-    }
-    return id >> 6;
+    return frame >> 6;
 }
 
 /**

+ 10 - 8
emit/home_easy.h

@@ -16,20 +16,20 @@
 /**
  * Encode bits with HomeEasy encoding (1 => 10, 0 => 01)
  *
- * @param buffer the buffuer to encode
+ * @param frame 32-bit frame to encode
  *
  * @return new buffer
  * */
-BYTE_BUFFER homeEasyEncode(BYTE_BUFFER *buffer);
+BYTE_BUFFER homeEasyEncode(unsigned long int frame);
 
 /**
  * Decode bits with HomeEasy encoding (1 => 10, 0 => 01)
  *
  * @param buffer the buffuer to decode
  *
- * @return new buffer
+ * @return new frame
  * */
-BYTE_BUFFER homeEasyDecode(BYTE_BUFFER *buffer);
+unsigned long int homeEasyDecode(BYTE_BUFFER *buffer);
 
 /**
  * Encode a byte according to HomeEasy
@@ -50,14 +50,16 @@ unsigned short int encodeByte(unsigned char byte);
 unsigned char decodeByte(unsigned short int word);
 
 /**
- * Creata a complete command according to Chacon protocole
+ * Create a complete command according to Chacon protocole
  *
  * @param id command id (refer to your remote)
  * @param section button section ('A' | 'B'  | 'C'  | 'D'  | 'G')
  * @param nb button number(1, 2, 3, 4)
  * @param on boolean for on/off
+ * 
+ * @return HomeEasy frame
  */
-BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on);
+unsigned long int createHomeEasyCommand(unsigned long int id, char section, unsigned char nb, unsigned char on);
 
 /**
  * Send a complete command according to Chacon protocole
@@ -68,7 +70,7 @@ BYTE_BUFFER createHomeEasyCommand(unsigned char* id, char section, unsigned char
  * @param on boolean for on/off
  * @param repeat number of repeatition
  */
-void sendHomeEasyCommand(unsigned char* id, char section, unsigned char nb, unsigned char on, unsigned char repeat);
+void sendHomeEasyCommand(unsigned long int id, char section, unsigned char nb, unsigned char on, unsigned char repeat);
 
 /**
  * Send n times a data frame
@@ -99,7 +101,7 @@ int initIO();
  *
  * @return the device id
  */
-unsigned long int getHomeEasyId(BYTE_BUFFER buffer);
+unsigned long int getHomeEasyId(unsigned long int frame);
 
 /**
  * read the GPIO output pin

+ 1 - 1
emit/test/Makefile

@@ -1,7 +1,7 @@
 all : test
 
 test : test.o buffer.o home_easy.o utils.o
-	gcc -o $@ $^ -lwiringPi
+	gcc -o $@ $^
 
 test.o : test.c
 	gcc -c $< -o $@

+ 16 - 29
emit/test/test.c

@@ -40,14 +40,10 @@ void testPrintBits()
 void testGetId()
 {
     unsigned long int id;
-    BYTE_BUFFER buffer;
-    char bytes[] = {0x28, 0x29, 0x01, 0x9a};
+    unsigned long int frame = 0x2829019a;
     TEST_START;
 
-    buffer = createByteBuffer();
-    pushBytes(&buffer, bytes, 4);
-
-    id =  getHomeEasyId(buffer);
+    id =  getHomeEasyId(frame);
     printf("Should 00A0A406\nGet %08X\n", id);
 }
 
@@ -76,57 +72,48 @@ void testHomeEasyEncode()
 {
     BYTE_BUFFER source;
     BYTE_BUFFER encoded;
-    unsigned char bytes[] = {0x28, 0x28, 0x01};
+    unsigned long int frame = 0x2829019a;
     TEST_START;
 
-    source = createByteBuffer();
-    pushBytes(&source, bytes, 3);
-    printf("Input: ");
-    printfByteBuffer(source);
-    printf("Should: 59 95 59 95 55 56\nGet:    ");
+    printf("Input: %08X\n", frame);
+    printf("Should: 59 95 59 95 55 56 96 99\nGet:    ");
 
-    encoded = homeEasyEncode(&source);
+    encoded = homeEasyEncode(frame);
     printfByteBuffer(encoded);
 
-    destroyByteBuffer(source);
     destroyByteBuffer(encoded);
 }
 
 void testHomeEasyDecode()
 {
     BYTE_BUFFER source;
-    BYTE_BUFFER decoded;
-    unsigned char bytes[] = {0x59, 0x95, 0x59, 0x95, 0x55, 0x56};
+    unsigned long int decoded;
+    unsigned char bytes[] = {0x59, 0x95, 0x59, 0x96, 0x55, 0x56, 0x9A, 0x55};
     TEST_START;
 
     source = createByteBuffer();
-    pushBytes(&source, bytes, 6);
+    pushBytes(&source, bytes, 8);
     printf("Input: ");
     printfByteBuffer(source);
-    printf("Should: 28 28 01\nGet:    ");
-
-    decoded = homeEasyDecode(&source);
-    printfByteBuffer(decoded);
+    printf("Should: 282901B0\nGet:    %08X\n", homeEasyDecode(&source));
 
     destroyByteBuffer(source);
-    destroyByteBuffer(decoded);
 }
 
 
 void testHomeEasyCommand(char section, unsigned char num, unsigned char onOff)
 {
-    BYTE_BUFFER command;
+    unsigned long int command;
     BYTE_BUFFER encoded;
-    unsigned char id[] = {0x28, 0x28, 0x01, 0x80};
+    unsigned long int id = getHomeEasyId(0x2829019a);
     TEST_START;
 
     command = createHomeEasyCommand(id, section, num, onOff);
-    printfByteBuffer(command);
+    printf("%08X\n", command);
 
-    encoded = homeEasyEncode(&command);
+    encoded = homeEasyEncode(command);
     printfByteBuffer(encoded);
 
-    destroyByteBuffer(command);
     destroyByteBuffer(encoded);
 }
 
@@ -137,6 +124,8 @@ int main()
     testByteBuffer();
 
     testPrintBits();
+    
+    testGetId();
 
     testEncode();
 
@@ -148,7 +137,5 @@ int main()
 
     testHomeEasyCommand('D', 4, OFF);
 
-    testGetId();
-
     return 0;
 }