USB HID Keyboard and car audio controller for steering of TOYOTA car

Dependencies:   USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
zeus3110
Date:
Thu Aug 20 11:59:37 2015 +0000
Commit message:
First release

Changed in this revision

DEH970Ctrl.cpp Show annotated file Show diff for this revision Revisions of this file
DEH970Ctrl.h Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 30fe2eec5271 DEH970Ctrl.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DEH970Ctrl.cpp	Thu Aug 20 11:59:37 2015 +0000
@@ -0,0 +1,117 @@
+#include "DEH970Ctrl.h"
+
+
+unsigned char VolPlus[DATA_LENGTH]={0xB5,0x4A,0x50,0xAF};
+//unsigned char VolPlus[DATA_LENGTH]={0x55,0xAA,0xA5,0x5A};
+unsigned char VolMinus[DATA_LENGTH]={0xB5,0x4A,0xD0,0x2F};   
+
+DEH970Controller::DEH970Controller(PwmOut *Port)
+{
+    Locked=false;
+    IRPort=Port;
+    
+    PwmPortInit();
+}
+
+DEH970Controller::~DEH970Controller()
+{
+    
+}
+
+bool DEH970Controller::IsLocked()
+{
+    return Locked;   
+}
+
+void DEH970Controller::PwmPortInit()
+{
+#ifdef DEBUG
+    printf("PWM Port Init\n");
+#endif
+    IRPort->period_us(BurstPeriod);
+    IRPort->write(IR_OFF);    
+}
+
+void  DEH970Controller::SendSignal(unsigned char *Dp)
+{
+    DataBitPos=0;
+    DataBytePos=-1;
+    Data=Dp;
+    DataBit=false;
+    TimeOutIR=NULL;
+#ifdef DEBUG
+    printf("SendSignal\n");
+#endif
+    SendData();
+}
+
+void DEH970Controller::SendData()
+{
+    timestamp_t WaitTime;
+
+    if(TimeOutIR!=NULL)
+        delete TimeOutIR;
+
+    TimeOutIR=new Timeout();
+    DataBit=!DataBit;
+
+#ifdef DEBUG
+    printf("%d %d %d\n",DataBytePos,DataBitPos,DataBit);
+#endif
+    // Send Leader
+    if(DataBytePos==-1)
+    {
+        WaitTime=DataBit?LeaderOn:LeaderOff;
+        IRPort->write(DataBit?IR_ON:IR_OFF);
+        if(!DataBit)
+            DataBytePos++;
+    }
+    // Send Trailer
+    else if(DataBytePos==DATA_LENGTH)
+    {
+        WaitTime=DataBit?TrailerOn:TrailerOff;
+        IRPort->write(DataBit?IR_ON:IR_OFF);
+        if(!DataBit)
+            DataBytePos++;
+    }
+    // Finish if Sent Trailer
+    else if(DataBytePos==DATA_LENGTH+1)    
+    {
+#ifdef DEBUG
+            printf("finished\n");
+#endif
+           return;
+    }
+    // Send Data
+    else
+    {
+        if(DataBit)
+            WaitTime=DataOn;
+        else
+            WaitTime=((((int)Data[DataBytePos])&(0x00000080>>DataBitPos))==0)?DataOff0:DataOff1;
+#ifdef DEBUG2
+        printf("%x\n",(((int)Data[DataBytePos])&(0x00000080>>DataBitPos))!=0);
+#endif
+        IRPort->write(DataBit?IR_ON:IR_OFF);
+        if(!DataBit)
+        {   DataBitPos=(DataBitPos+1)%8;
+            if(DataBitPos==0)
+                DataBytePos++;
+        }
+    }
+
+    TimeOutIR->attach_us(this,&DEH970Controller::SendData,WaitTime);
+}
+
+void DEH970Controller::SendVolPlus()
+{
+#ifdef DEBUG
+    printf("Send Vol Plus\n");
+#endif
+    SendSignal(VolPlus);
+}
+
+void DEH970Controller::SendVolMinus()
+{
+    SendSignal(VolMinus);
+}
\ No newline at end of file
diff -r 000000000000 -r 30fe2eec5271 DEH970Ctrl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DEH970Ctrl.h	Thu Aug 20 11:59:37 2015 +0000
@@ -0,0 +1,43 @@
+#ifndef _DEH970_CTRL_H_
+#define _DEH970_CTRL_H_
+
+#include "mbed.h"
+
+#define DATA_LENGTH 4
+#define IR_ON 0.5
+#define IR_OFF 0
+
+const int BurstPeriod=26;
+const timestamp_t LeaderOn=8628;
+const timestamp_t LeaderOff=4137;
+const timestamp_t DataOn=626;    
+const timestamp_t DataOff1=1488;
+const timestamp_t DataOff0=424;
+const timestamp_t TrailerOn=626;
+const timestamp_t TrailerOff=25395;
+
+class DEH970Controller {
+protected:    
+private:
+    bool Locked;
+    PwmOut *IRPort;
+    int DataBitPos,DataBytePos;
+    unsigned char *Data;
+    bool DataBit;
+    Timeout *TimeOutIR;
+
+
+public:
+    bool IsLocked();  
+    DEH970Controller(PwmOut *Port);
+    ~DEH970Controller();
+    void PwmPortInit();
+    void SendSignal(unsigned char *Dp);
+    void SendLeaderOn();
+    void SendData();
+    
+    void SendVolPlus();
+    void SendVolMinus();
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 30fe2eec5271 USBDevice.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Thu Aug 20 11:59:37 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/USBDevice/#2af474687369
diff -r 000000000000 -r 30fe2eec5271 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Aug 20 11:59:37 2015 +0000
@@ -0,0 +1,139 @@
+#include "mbed.h"
+#include "USBKeyboard.h"
+#include "DEH970Ctrl.h"
+
+#define DEBUG
+
+//main loop
+const unsigned ADAvg=8;
+
+//main roop wait time(ms)
+const int wait_time_ms=100;
+const int wait_time_keydet_ms=500; 
+
+//AD value range
+//2.5ohm button (Mode or SEEK+)
+const unsigned Res2R5MinAD=0x0000;
+const unsigned Res2R5MaxAD=0x00C4;
+//0.3kohm button (SEEK-)
+const unsigned Res300MinAD=0x2F42;
+const unsigned Res300MaxAD=0x46E4;
+//1kohm button (VOL+)
+const unsigned Res1kMinAD=0x7332;
+const unsigned Res1kMaxAD=0x8ccc;
+//3.1kohm button (VOL-)
+const unsigned Res3kMinAD=0xAE33;
+const unsigned Res3kMaxAD=0xD4E9;
+
+
+Ticker tick;
+AnalogIn Key1(A0);
+AnalogIn Key2(A1);
+DigitalOut HBLed(LED2);
+DigitalOut IRLed(LED1);
+DigitalOut USBLed(LED3);
+PwmOut IRPort(PTB2);
+DEH970Controller *DEH970Ctrl;
+USBKeyboard *Keyboard;
+
+int main()
+{
+    unsigned Key1AD,Key2AD;
+    unsigned i;
+    
+    printf("start!\n");
+
+    DEH970Ctrl=new DEH970Controller(&IRPort);
+
+    printf("start!\n");
+
+    Keyboard=new USBKeyboard();
+
+    printf("USB start!\n");
+
+    HBLed=true;
+    IRLed=true;
+
+    printf("Start!");
+    
+    while(1)
+    {   Key1AD=0;
+        Key2AD=0;
+        IRLed=true;
+        USBLed=true;
+        
+        //キーAD値の8回平均を取得
+        for(i=0;i<ADAvg;i++)
+        {   Key1AD=Key1AD+(unsigned)Key1.read_u16();
+            Key2AD=Key2AD+(unsigned)Key2.read_u16();
+        }
+        
+        Key1AD/=ADAvg;
+        Key2AD/=ADAvg;
+        
+#ifdef DEBUG
+    printf("AD Value:%x %x\n",Key1AD,Key2AD);
+#endif
+
+        //MODE SW
+        if(Res2R5MinAD<=Key2AD && Key2AD<=Res2R5MaxAD)
+        {
+#ifdef DEBUG
+            printf("MODE SW\n");
+#endif
+            USBLed=false;
+            Keyboard->mediaControl(KEY_PLAY_PAUSE);
+            wait_ms(wait_time_keydet_ms);
+        }   
+        //SEEK+ SW
+        else if(Res2R5MinAD<=Key1AD && Key1AD<=Res2R5MaxAD)
+        {
+#ifdef DEBUG
+            printf("SEEK NEXT SW\n");
+#endif
+            USBLed=false;
+            Keyboard->mediaControl(KEY_NEXT_TRACK);
+            wait_ms(wait_time_keydet_ms);
+        }   
+        //SEEK- SW
+        else if(Res300MinAD<=Key1AD && Key1AD<=Res300MaxAD)
+        {
+#ifdef DEBUG
+            printf("SEEK PREV SW\n");
+#endif
+            USBLed=false;
+            Keyboard->mediaControl(KEY_PREVIOUS_TRACK);
+            wait_ms(wait_time_keydet_ms);
+        }   
+
+        //VOL+ SW
+        else if(Res1kMinAD<=Key1AD && Key1AD<=Res1kMaxAD)
+        {
+#ifdef DEBUG
+            printf("VOL UP SW\n");
+#endif
+            if(DEH970Ctrl->IsLocked()==false)
+            {   IRLed=false;
+                DEH970Ctrl->SendVolPlus();
+                wait_ms(wait_time_keydet_ms);
+            }
+        }   
+        //VOL- SW
+        else if(Res3kMinAD<=Key1AD && Key1AD<=Res3kMaxAD)
+        {
+#ifdef DEBUG
+            printf("VOL DOWN SW\n");
+#endif
+            if(DEH970Ctrl->IsLocked()==false)
+            {   IRLed=false;
+                DEH970Ctrl->SendVolMinus();
+                wait_ms(wait_time_keydet_ms);
+            }
+        }
+
+        wait_ms(wait_time_ms);
+        HBLed=!HBLed;
+    }
+    
+    // return(0);
+}
diff -r 000000000000 -r 30fe2eec5271 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Aug 20 11:59:37 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b9ad9a133dc7
\ No newline at end of file