このライブラリはSW認識を行います。 SWは1個から20個まで認識できます。 SWの認識周期は10msです。 SWはmbedの端子とGNDの間に接続します。 This library does the recognition SW. SW, one can recognize up to 20. Recognition of the SW period is 10ms. SW is connected to GND terminal of mbed. ON the SW, OFF (level) data output. Output data from OFF to ON edge of the SW. Output data from ON to OFF edge of the SW. SWのON,OFF(レベル)データを出力します。 SWのOFFからONのエッジデータを出力します。 SWのONからOFFのエッジデータを出力します。

Dependents:   kitchenTimer_Clock SwDigitalLibraryExampleProgram

Libraryの使い方については次のHPを参照してください。 http://mbed.org/users/suupen/code/SwDigitalLibraryExampleProgram/wiki/Homepage

Files at this revision

API Documentation at this revision

Comitter:
suupen
Date:
Sat Nov 26 11:38:42 2011 +0000
Child:
1:356fb47220a0
Commit message:
V1.0 2011/11/26

Changed in this revision

SwDigital.cpp Show annotated file Show diff for this revision Revisions of this file
SwDigital.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SwDigital.cpp	Sat Nov 26 11:38:42 2011 +0000
@@ -0,0 +1,200 @@
+//======================================================================
+//    SwDigital.cpp
+//
+// pwmOutput - digtalOutput de sound syuturyoku suru library
+//======================================================================
+#define _SWDIGITAL_C
+
+#include "mbed.h"
+#include "SwDigital.h"
+
+//======================================================================
+// syokika
+//======================================================================
+
+    SwDigital::SwDigital(PinName sw0, PinName sw1, PinName sw2, PinName sw3, PinName sw4,
+                         PinName sw5, PinName sw6, PinName sw7, PinName sw8, PinName sw9,
+                         PinName sw10, PinName sw11, PinName sw12, PinName sw13, PinName sw14,
+                         PinName sw15, PinName sw16, PinName sw17, PinName sw18, PinName sw19
+                         ):
+                         _sw0(sw0),   _sw1(sw1),   _sw2(sw2),   _sw3(sw3),   _sw4(sw4),
+                         _sw5(sw5),   _sw6(sw6),   _sw7(sw7),   _sw8(sw8),   _sw9(sw9),
+                         _sw10(sw10), _sw11(sw11), _sw12(sw12), _sw13(sw13), _sw14(sw14),
+                         _sw15(sw15), _sw16(sw16), _sw17(sw17), _sw18(sw18), _sw19(sw19)
+                         {
+       
+    //---------------------------------
+    // touroku sareta Sw Suu wo kakunin
+    //---------------------------------
+    if(sw0 != NC){D_swSuu = 1; _sw0.mode(PullUp);}
+    if(sw1 != NC){D_swSuu = 2; _sw1.mode(PullUp);}
+    if(sw2 != NC){D_swSuu = 3; _sw2.mode(PullUp);}
+    if(sw3 != NC){D_swSuu = 4; _sw3.mode(PullUp);}
+    if(sw4 != NC){D_swSuu = 5; _sw4.mode(PullUp);}
+    
+    if(sw5 != NC){D_swSuu = 6; _sw5.mode(PullUp);}
+    if(sw6 != NC){D_swSuu = 7; _sw6.mode(PullUp);}
+    if(sw7 != NC){D_swSuu = 8; _sw7.mode(PullUp);}
+    if(sw8 != NC){D_swSuu = 9; _sw8.mode(PullUp);}
+    if(sw9 != NC){D_swSuu = 10; _sw9.mode(PullUp);}    
+    
+    if(sw10 != NC){D_swSuu = 11; _sw10.mode(PullUp);}
+    if(sw11 != NC){D_swSuu = 12; _sw11.mode(PullUp);}
+    if(sw12 != NC){D_swSuu = 13; _sw12.mode(PullUp);}
+    if(sw13 != NC){D_swSuu = 14; _sw13.mode(PullUp);}
+    if(sw14 != NC){D_swSuu = 15; _sw14.mode(PullUp);}
+    
+    if(sw15 != NC){D_swSuu = 16; _sw15.mode(PullUp);}
+    if(sw16 != NC){D_swSuu = 17; _sw16.mode(PullUp);}
+    if(sw17 != NC){D_swSuu = 18; _sw17.mode(PullUp);}
+    if(sw18 != NC){D_swSuu = 19; _sw18.mode(PullUp);}
+    if(sw19 != NC){D_swSuu = 20; _sw19.mode(PullUp);}    
+    
+    // sw ninsiki syuki 10[ms]
+    swCheckTimer.attach_us(this, &SwDigital::input, Z_matchcycle); 
+        
+}
+
+void SwDigital::input(void){
+    // genzai level input
+    switch(D_swSuu){
+    case 20:
+        B_kariLevel[19] = ((B_kariLevel[19] << 1) | (((uint8_t)~_sw19) & 0x01));
+        //break;
+    case 19:
+        B_kariLevel[18] = ((B_kariLevel[18] << 1) | (((uint8_t)~_sw18) & 0x01));
+        //break;
+    case 18:
+        B_kariLevel[17] = ((B_kariLevel[17] << 1) | (((uint8_t)~_sw17) & 0x01));
+        //break;        
+    case 17:
+        B_kariLevel[16] = ((B_kariLevel[16] << 1) | (((uint8_t)~_sw16) & 0x01));
+        //break;        
+    case 16:
+        B_kariLevel[15] = ((B_kariLevel[15] << 1) | (((uint8_t)~_sw15) & 0x01));
+        //break;       
+    case 15:
+        B_kariLevel[14] = ((B_kariLevel[14] << 1) | (((uint8_t)~_sw14) & 0x01));
+        //break;
+    case 14:
+        B_kariLevel[13] = ((B_kariLevel[13] << 1) | (((uint8_t)~_sw13) & 0x01));
+        //break;
+    case 13:
+        B_kariLevel[12] = ((B_kariLevel[12] << 1) | (((uint8_t)~_sw12) & 0x01));
+        //break;        
+    case 12:
+        B_kariLevel[11] = ((B_kariLevel[11] << 1) | (((uint8_t)~_sw11) & 0x01));
+        //break;        
+     case 11:
+        B_kariLevel[10] = ((B_kariLevel[10] << 1) | (((uint8_t)~_sw10) & 0x01));
+        //break;               
+        
+    case 10:
+        B_kariLevel[9] = ((B_kariLevel[9] << 1) | (((uint8_t)~_sw9) & 0x01));
+        //break;
+    case 9:
+        B_kariLevel[8] = ((B_kariLevel[8] << 1) | (((uint8_t)~_sw8) & 0x01));
+        //break;
+    case 8:
+        B_kariLevel[7] = ((B_kariLevel[7] << 1) | (((uint8_t)~_sw7) & 0x01));
+        //break;        
+    case 7:
+        B_kariLevel[6] = ((B_kariLevel[6] << 1) | (((uint8_t)~_sw6) & 0x01));
+        //break;        
+    case 6:
+        B_kariLevel[5] = ((B_kariLevel[5] << 1) | (((uint8_t)~_sw5) & 0x01));
+        //break;       
+    case 5:
+        B_kariLevel[4] = ((B_kariLevel[4] << 1) | (((uint8_t)~_sw4) & 0x01));
+        //break;
+    case 4:
+        B_kariLevel[3] = ((B_kariLevel[3] << 1) | (((uint8_t)~_sw3) & 0x01));
+        //break;
+    case 3:
+        B_kariLevel[2] = ((B_kariLevel[2] << 1) | (((uint8_t)~_sw2) & 0x01));
+        //break;        
+    case 2:
+        B_kariLevel[1] = ((B_kariLevel[1] << 1) | (((uint8_t)~_sw1) & 0x01));
+        //break;        
+    case 1:
+        B_kariLevel[0] = ((B_kariLevel[0] << 1) | (((uint8_t)~_sw0) & 0x01));
+        //break;                       
+    default:
+        // nothing
+        break;
+    }
+    
+    // kakutei Level kosin
+    uint8_t kakutei = 0;
+    for(uint8_t i = 0; i < D_swSuu; i++){
+        uint8_t work = B_kariLevel[i] & Z_itchiPattern;
+        if(work == 0x00){
+            // off kakutei
+            D_oldLevel[i] = D_nowLevel[i];
+            D_nowLevel[i] = Z_levelOff;
+            kakutei = 1;
+        }
+        else if(work == Z_itchiPattern){
+            // on kakutei
+            D_oldLevel[i] = D_nowLevel[i];
+            D_nowLevel[i] = Z_levelOn;
+            kakutei = 1;
+        }
+        else{
+            // nothing
+        }
+        
+        if(kakutei == 1){
+            // edge kosin
+            if((D_oldLevel[i] == Z_levelOff) && (D_nowLevel[i] == Z_levelOn)){
+                B_edgeOn[i] = Z_edgeAri;
+            }
+            if((D_oldLevel[i] == Z_levelOn) && (D_nowLevel[i] == Z_levelOff)){
+                B_edgeOff[i] = Z_edgeAri;
+            }
+        }
+
+    }              
+}
+
+void SwDigital::refreshEdgeData(void){
+    for(uint8_t i = 0; i < D_swSuu; i++){
+        __disable_irq(); 	// Disable Interrupts
+        D_edgeOn[i]  = B_edgeOn[i];
+        D_edgeOff[i] = B_edgeOff[i];
+        
+        B_edgeOn[i]  = Z_edgeNasi;
+        B_edgeOff[i] = Z_edgeNasi;
+        __enable_irq(); 	// Enable Interrupts
+    }
+}
+
+uint8_t SwDigital::checkEdgeOn(uint8_t swNo){
+    uint8_t ans = 0;
+    
+    if(D_edgeOn[swNo] == Z_edgeAri){
+        ans = 1;
+    } 
+
+    return (ans);
+}
+
+uint8_t SwDigital::checkEdgeOff(uint8_t swNo){
+    uint8_t ans = 0;
+    
+    if(D_edgeOff[swNo] == Z_edgeAri){
+        ans = 1;
+    } 
+
+    return (ans);
+}
+
+uint8_t SwDigital::checkLevel(uint8_t swNo){
+    uint8_t ans = 0;
+    
+    if(D_nowLevel[swNo] == Z_levelOn){
+        ans = 1;
+    } 
+
+    return (ans);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SwDigital.h	Sat Nov 26 11:38:42 2011 +0000
@@ -0,0 +1,208 @@
+/* SwDigital Library
+ * Copyright (c) 2011 suupen
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/***********************************************************************/
+/*                                                                     */
+/*    SwDigital.h                                                      */
+/*                                                                     */
+/*                                                                     */
+/*    V0.1 : 2011/11/13                                                */
+/***********************************************************************/
+#ifndef _SWDIGITAL_H
+#define _SWDIGITAL_H
+
+#include "mbed.h"
+
+/** SWDIGITAL control class, based on a "mbed function"
+*
+* Example:
+* @code
+* //============================================
+* // SwDigital Library example program
+* // 
+* //  <schematic>
+* //
+* //  mbed
+* //                sw1
+* //                ---- 
+* //    p20 --------o  o----------- GND
+* //
+* //                sw2
+* //                ----
+* //    p21 --------o  o------------GND
+* //
+* //=============================================
+*
+*
+*#include "mbed.h"
+*#include "SwDigital.h"
+*
+*SwDigital sw(p20,p21);  // p20 : sw1 control LED1,LED2
+*                        // p21 : sw2 control LED3,LED4
+*
+*DigitalOut led1(LED1);
+*DigitalOut led2(LED2);
+*DigitalOut led3(LED3);
+*DigitalOut led4(LED4);
+*
+*int main() {
+*    while(1) {
+*        // sw level and edge data refresh
+*        sw.transEdgeData();
+*        
+*        // tact action (sw level = on : led1 = on)
+*        led1 = sw.checkLevel(0);
+*        
+*        // tact action (sw level = off : led2 = on)
+*        led2 = !sw.checkLevel(0);
+*        
+*        // toggle action (level Off to On)
+*        if(sw.checkEdgeOn(1) == 1){
+*             led3 = !led3;
+*        }
+*
+*        // toggle action (level On to Off)
+*        if(sw.checkEdgeOff(1) == 1){
+*             led4 = !led4;
+*        }
+*    }
+*}
+*
+* @endcode
+*/
+
+class SwDigital {
+public:
+
+    /** Create a sound object connected to the specified PwmOut pin & DigitalOut pin
+    *
+    * @param pin sw0 to sw19 : sw input pin 
+    *
+    * sw can be set from 1 to 20.
+    * Recognition of the SW period is 10ms
+    */
+    SwDigital(PinName sw0  = NC, PinName sw1  = NC, PinName sw2  = NC, PinName sw3  = NC, PinName sw4  = NC,
+              PinName sw5  = NC, PinName sw6  = NC, PinName sw7  = NC, PinName sw8  = NC, PinName sw9  = NC,
+              PinName sw10 = NC, PinName sw11 = NC, PinName sw12 = NC, PinName sw13 = NC, PinName sw14 = NC,
+              PinName sw15 = NC, PinName sw16 = NC, PinName sw17 = NC, PinName sw18 = NC, PinName sw19 = NC
+              );
+
+    /** refresh edge data
+    *
+    * @param none 
+    * @param return none
+    *
+    *  main de edge data wo tukau maeni jiko suru
+    */
+    void refreshEdgeData(void);
+    
+    /** Check Off to On edge
+    *
+    * @param uint8_t swNo : check swNo 0 to 19 
+    * @param return uint8_t  On edge check  0: edge Nasi  1: edge Ari
+    *
+    */
+    uint8_t checkEdgeOn(uint8_t swNo);
+
+    /** Check On to Off edge
+    *
+    * @param uint8_t swNo : check swNo 0 to 19 
+    * @param return uint8_t Off edge check   0 : Nasi   1 : Ari
+    *
+    */
+    uint8_t checkEdgeOff(uint8_t swNo);
+    
+    /** Check On to Off edge
+    *
+    * @param uint8_t swNo : check swNo 0 to 19 
+    * @param return uint8_t sw level check   0 : Off   1 : On
+    *
+    */    
+    uint8_t checkLevel(uint8_t swNo);
+    
+//protected:    
+private:
+   DigitalIn _sw0;
+   DigitalIn _sw1;
+   DigitalIn _sw2;
+   DigitalIn _sw3;
+   DigitalIn _sw4;
+
+   DigitalIn _sw5;
+   DigitalIn _sw6;
+   DigitalIn _sw7;
+   DigitalIn _sw8;
+   DigitalIn _sw9;
+
+   DigitalIn _sw10;
+   DigitalIn _sw11;
+   DigitalIn _sw12;
+   DigitalIn _sw13;
+   DigitalIn _sw14;
+
+   DigitalIn _sw15;
+   DigitalIn _sw16;
+   DigitalIn _sw17;
+   DigitalIn _sw18;
+   DigitalIn _sw19;
+   
+   Ticker swCheckTimer;
+   
+   void input(void);
+
+   #define Z_matchcycle (10000) // 10000[us](10[ms]) to 100000[us](100[ms])  1[us]/count
+   
+   uint8_t D_swSuu;         // touroku sareta Sw Suu  1 to 20
+   #define Z_swSuuMax  (20)    // SW no saidai suu
+   
+   uint8_t B_kariLevel[Z_swSuuMax]; // kakutei mae no ninsiki Level 0bit:saisin(t) 1bit:t-1, ... ,7bit:t-7  0:Off  1:On
+    // match number define
+    //#define Z_itchiPattern     (0x03)   // 2kai itch
+    #define Z_itchiPattern    (0x07)  // 3kai itchi
+    //#define Z_itchiPattern    (0x0f)  // 4kai itchi
+    //#define Z_itchiPattern    (0x1f)  // 5kai itchi
+    //#define Z_itchiPattern    (0x3f)  // 6kai itchi
+    //#define Z_itchiPattern    (0x7f)  // 7kai itchi
+    //#define Z_itchiPattern    (0xff)  // 8kai itchi
+   
+   // sw level data
+   uint8_t D_nowLevel[Z_swSuuMax];  // saisin no kakutei Level 0:Off  1:On
+   uint8_t D_oldLevel[Z_swSuuMax];  // zenkai no kakutei Level 0:Off  1:On
+    #define Z_levelOff (0)
+    #define Z_levelOn (1)
+   
+   // sw edge data
+   // swDigital.c naibu hensu 
+   uint8_t B_edgeOn[Z_swSuuMax];    // off kara on  no ninsiki(on edge)  0:Nasi  1:Ari
+   uint8_t B_edgeOff[Z_swSuuMax];   // on  kara off no ninsiki(off edge) 0:Nasi  1:Ari
+   // user use hensu
+   uint8_t D_edgeOn[Z_swSuuMax];    // off kara on  no ninsiki(on edge)  0:Nasi  1:Ari
+   uint8_t D_edgeOff[Z_swSuuMax];   // on  kara off no ninsiki(off edge) 0:Nasi  1:Ari
+    #define Z_edgeNasi (0)
+    #define Z_edgeAri  (1)
+
+
+
+
+};    
+
+#endif    // _SWDIGITAL_H