IRC Helicopter "SWIFT" Propo codec(Decode only) library

Dependents:   SwiftPropoIR_TestProgram spinner2

Files at this revision

API Documentation at this revision

Comitter:
suupen
Date:
Sun Jun 23 07:25:04 2013 +0000
Commit message:
IRC Helicpoter "SWIFT" Propo codec(decode only) library

Changed in this revision

CodecSwift.h Show annotated file Show diff for this revision Revisions of this file
DecodeSwift.cpp Show annotated file Show diff for this revision Revisions of this file
DecodeSwift.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CodecSwift.h	Sun Jun 23 07:25:04 2013 +0000
@@ -0,0 +1,68 @@
+/**
+ * Codec IR Propo of Swift common class
+ * Version 0.0      130623
+ *
+ * Writer:suupen
+ */
+
+#ifndef _CODECSWIFT_H_
+#define _CODECSWIFT_H_
+
+#include <mbed.h>
+
+
+
+class CodecSwift {
+public:
+
+    typedef struct{
+        uint8_t count;
+        uint8_t band;   // 1 to 255
+        float slottle;  // 0(min) to 1.0(max)
+        float ladder;   // -1.0(Left) to 1.0(Right)
+        float elevator;  // -1.0(backward) to 1.0(forward)
+        float trim;     // -1.0 to 1.0
+    } normalizePropo_t;
+
+
+
+    typedef struct{
+        uint8_t count;      // recive count [1/1 [recive]/count]
+        uint8_t band;       // 0x03:A band  0x02:B 0x01:C
+        uint8_t slottle;    // 0x00 - 0x72
+        int8_t trim;      //  -23 to 29
+        int8_t ladder;      // right:1 to 15,  left:-1 to -15
+        int8_t elevator;    // forward: 1 to 15 , backward -1 to -15
+    } swiftPropo_t;
+
+static const uint8_t BAND_A = 0x03;
+static const uint8_t BAND_B = 0x02;
+static const uint8_t BAND_C = 0x01;
+
+static const uint8_t SLOTTLE_MIN = 0x00;
+static const uint8_t SLOTTLE_MAX = 114;
+
+static const int8_t TRIM_MIN = -23;
+static const int8_t TRIM_MAX = 23;
+
+static const int8_t LADDER_MIN = -15;
+static const int8_t LADDER_MAX = 15;
+
+static const int8_t ELEVATOR_MIN = -15;
+static const int8_t ELEVATOR_MAX = 15;
+
+
+private:
+    
+ CodecSwift();
+
+
+};
+
+#endif
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DecodeSwift.cpp	Sun Jun 23 07:25:04 2013 +0000
@@ -0,0 +1,178 @@
+/**
+ * IR Propo Decode class
+ *  Version 0.0     130623
+ *
+ * Writer:suupen
+ */
+ #include "CodecSwift.h"
+ #include "DecodeSwift.h"
+ 
+ /**
+ * Constructor DecodeSwift
+ *
+ */
+ DecodeSwift::DecodeSwift(){
+    // nothing
+ }
+ 
+ /**
+ * Destructor.
+ */
+ DecodeSwift::~DecodeSwift(){
+    // nothing
+ }
+ 
+
+/**
+ * Get swift controller data.
+ *
+ * @param data Pointer to propo recive data.
+ *
+ * @param swift Pointer to swift.
+ *
+ * @return receive answer : ture:recive ok   false:recive ng.
+ */
+bool DecodeSwift::decode(uint8_t *data, CodecSwift::swiftPropo_t *swift) {
+    bool ans = false;
+    int8_t wk1 = 0;
+    int8_t wk2;
+    
+    int8_t wk3;
+    int8_t trimWork;
+    
+    // parity calculate
+   
+    for(uint8_t c=0; c < 3; c++){
+        wk1 += data[c] & 0x3f;
+    }
+    if(0 != (wk1 & 0x0f)){
+        wk1 = wk1 + 0x10 - 0x01;
+    }
+    else{
+        wk1 |= 0x0f;
+    }
+    
+    wk1 &= 0x3f;    
+    wk2 = data[3] & 0x3f;
+    if(wk1 ==  wk2){
+        // parity ok
+        ans =true;
+        swift->count++;      // recive count [1/1 [recive]/count]
+        swift->band = (data[3] >> 6 ) & 0x03;        // 0x03:A band  0x02:B 0x01:C
+        swift->slottle = data[0] & 0x7f;    // 0x00 - 0x72
+        
+        trimWork = data[2] & 0x1F;
+        if((data[2] & 0x20) == 0x20){
+            trimWork = -trimWork;
+        }
+        else{
+            // nothine
+        }
+        swift->trim = trimWork;
+        
+        // right:1 to 15,  left:-1 to -15
+        wk3 = (data[1] >> 4) & 0x0f;
+        if(0x80 == (data[2] & 0x80)){
+            swift->ladder = wk3;
+        }
+        else{
+            swift->ladder = -wk3;
+        }
+  
+        // forward: 1 to 15 , backward -1 to -15
+        wk3 = (data[1] & 0x0f);
+        if(0x40 == (data[2] & 0x40)){
+            swift->elevator = wk3;
+        }
+        else{
+            swift->elevator = -wk3;
+        }
+    }
+    else{
+        // parity ng
+        
+        ans = false;
+    }
+     
+
+    return ans;
+
+}
+
+
+/**
+ * Normalize Swift data.
+ *
+ * @param buf Pointer to propo recive data.
+ *
+ * @param propo normalize propo data.
+ *
+ * @return receive answer : ture:recive ok   false:recive ng.
+ */
+bool DecodeSwift::normalize(uint8_t *buf, CodecSwift::normalizePropo_t *propo) {
+    bool ans = false;
+    bool decodeCheck =false;
+    
+    CodecSwift::swiftPropo_t swift;
+    decodeCheck = DecodeSwift::decode(buf, &swift);
+    
+    if(decodeCheck == true){
+        ans = true;
+     
+        propo->count++;// = swift.count;
+        
+        switch(swift.band){
+        case CodecSwift::BAND_A:
+            propo->band = 1;
+            break;
+        case CodecSwift::BAND_B:
+            propo->band = 2;
+            break;
+        case CodecSwift::BAND_C:
+            propo->band = 3;
+            break;
+        default:
+            propo->band = 0;
+            break;
+        }
+        
+        if(swift.slottle > CodecSwift::SLOTTLE_MAX){swift.slottle = CodecSwift::SLOTTLE_MAX;}
+        propo->slottle = (float)swift.slottle / CodecSwift::SLOTTLE_MAX;
+        
+        if(swift.ladder >= 0){
+            if(swift.ladder > CodecSwift::LADDER_MAX){swift.ladder = CodecSwift::LADDER_MAX;}
+            propo->ladder = (float)swift.ladder / CodecSwift::LADDER_MAX;
+        }
+        else{
+            if(swift.ladder < CodecSwift::LADDER_MIN){swift.ladder = CodecSwift::LADDER_MIN;}        
+            propo->ladder = -((float)swift.ladder / CodecSwift::LADDER_MIN);
+        }
+        
+        if(swift.elevator >= 0){
+            if(swift.elevator > CodecSwift::ELEVATOR_MAX){swift.elevator = CodecSwift::ELEVATOR_MAX;}       
+            propo->elevator = (float)swift.elevator / CodecSwift::ELEVATOR_MAX;
+        }
+        else{
+            if(swift.elevator < CodecSwift::ELEVATOR_MIN){swift.elevator = CodecSwift::ELEVATOR_MIN;}
+            propo->elevator = -((float)swift.elevator / CodecSwift::ELEVATOR_MIN);
+        }
+        
+        if(swift.trim >=0 ){
+            if(swift.trim > CodecSwift::TRIM_MAX){swift.trim = CodecSwift::TRIM_MAX;}
+            propo->trim = (float)swift.trim / CodecSwift::TRIM_MAX;
+        }
+        else{
+            if(swift.trim < CodecSwift::TRIM_MIN){swift.trim = CodecSwift::TRIM_MIN;}
+            propo->trim = -((float)swift.trim / CodecSwift::TRIM_MIN);
+        }
+    }
+    else{
+        // NG
+        ans = false;
+    }
+
+    return ans;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DecodeSwift.h	Sun Jun 23 07:25:04 2013 +0000
@@ -0,0 +1,63 @@
+/**
+ * Decode IR Propo Swift class
+ *  Version 0.0     130623
+ *
+ * Writer:suupen
+ */
+
+#ifndef _DECODESWIFT_H_
+#define _DECODESWIFT_H_
+
+#include <mbed.h>
+#include "CodecSwift.h"
+
+
+/**
+ * Decode class.
+ */
+class DecodeSwift {
+public:
+
+
+/**
+ * Constructor
+ */
+  DecodeSwift();
+  
+/**
+ * Destructor.
+ */
+  ~DecodeSwift();
+    
+/**
+ * Get swift propo analysis data.
+ *
+ * @param data IR recive data
+ *
+ * @param swift Pointer to swift.
+ *
+ * @return receive answer : ture:recive ok   false:recive ng.
+ */
+    bool decode(uint8_t *data, CodecSwift::swiftPropo_t *swift);
+
+/**
+ * Normalize Swift data.
+ *
+ * @param buf Pointer to propo recive data.
+ *
+ * @param propo normalize propo data.
+ *
+ * @return receive answer : ture:recive ok   false:recive ng.
+ */
+bool normalize(uint8_t *buf, CodecSwift::normalizePropo_t *propo);
+
+private:
+    
+ 
+
+
+};
+
+#endif
+
+