アナログポート から スイッチを3つ認識するライブラリです。 6つのアナログポートを同時に認識して、18個のSWまで認識できます。

Dependents:   kitchenTimer LPC1114FN28_kitchenTimer_Clock

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SwAnalog.h Source File

SwAnalog.h

00001 /* SwAnalog Library
00002  * Copyright (c) 2012 suupen
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 /***********************************************************************/
00024 /*                                                                     */
00025 /*    SwAnalog.h                                                       */
00026 /*                                                                     */
00027 /*                                                                     */
00028 /*    2012/02/12 : V1.0                                                */
00029 /***********************************************************************/
00030 #ifndef _SWANALOG_H
00031 #define _SWANALOG_H
00032 
00033 #include "mbed.h"
00034 
00035 //#define LPC1768
00036 
00037 /** SWANALOG control class, based on a "mbed function"
00038 *
00039 * Example:
00040 * @code
00041 * // *******************************************************************
00042 * // SwAnalogInput Library example program
00043 * //  mbed no analog port de 3hon no sw wo ninsiki suru.
00044 * //  6hon no analog port (p15 - p20) de 6 * 3 = 18 ko no sw ninsiki ga dekiru.
00045 * //
00046 * // <schematic>
00047 * //   -.- mbed VOUT(+3.3[V])
00048 * //    |                                               |--------------------> mbed p20(ADinput)
00049 * //    |   ---------       ---------       ---------   |   ---------
00050 * //    .---| Rsw2  |---.---| Rsw1  |---.---| Rsw0  |---.---| Rout  |----|
00051 * //    |   ---------   |   ---------   |   ---------   |   ---------    |
00052 * //    |     ----      |     -----     |     -----     |                |
00053 * //    |-----o  o------.-----o  o------.-----o  o------|              -----
00054 * //           SW2            SW1              SW0                      mbed GND(0[V])
00055 * // 
00056 * //  Rsw2 : 8.2[kohm], Rsw1 = 3.9[kohm], Rsw0 = 2.0[kohm], Rout = 1.0[kohm] (R no seido ha +-1[%])
00057 * //
00058 * // <program no naiyo>
00059 * //  mbed LED1 : sw0 no level de tento  sw Off = led Off, sw On = led On
00060 * //  mbed LED2 : sw1 no level de tento  sw Off = led Off, sw On = led On
00061 * //  mbed LED3 : sw2 no level de tento  sw Off = led Off, sw On = led On
00062 * //  mbed LED4 : sw0,sw1,sw2 no Off kara On henka de led tento jotai wo hanten.
00063 * //
00064 * // 
00065 * // <rireki>
00066 * // 2012/02/12 : syohan
00067 * // *********************************************************************
00068 *
00069 * #include "mbed.h"
00070 * #include "SwAnalog.h"
00071 *
00072 * DigitalOut led1(LED1);
00073 * DigitalOut led2(LED2);
00074 * DigitalOut led3(LED3);
00075 * DigitalOut led4(LED4);
00076 *
00077 * SwAnalog sw(p20);  // p20(adinput) :sw0,sw1,sw2
00078 *                   
00079 * int main() {
00080 *     while(1) {
00081 *         // sw level and edge data refresh
00082 *         sw.refreshEdgeData();
00083 *         
00084 *         // tact action (sw0 level = on : led1 = on)
00085 *         led1 = sw.checkLevel(0);
00086 *         
00087 *         // tact action (sw1 level = off : led2 = on)
00088 *         led2 = sw.checkLevel(1);
00089 *         
00090 *         // tact action (sw2 level = off : led3 = on)
00091 *         led3 = sw.checkLevel(2);
00092 *         
00093 *         // toggle action (sw0 to sw3 level Off to On : led4 invert )
00094 *         if((sw.checkEdgeOn(0) == 1)   // sw0
00095 *         || (sw.checkEdgeOn(1) == 1)   // sw1
00096 *         || (sw.checkEdgeOn(2) == 1)   // sw2
00097 *         ){
00098 *              led4 = !led4;
00099 *         }
00100 *     }
00101 * }
00102 * @endcode
00103 */
00104 
00105 class SwAnalog {
00106 public:
00107 
00108     /** Create a sw analog input object connected to the specified analog Input pin
00109     *
00110     * @param PinName adinput0 : analog input pin(pin15 to pin20) : sw0 kara sw2   no ninsiki 
00111     * @param PinName adinput1 : analog input pin(pin15 to pin20) : sw3 kara sw5   no ninsiki 
00112     * @param PinName adinput2 : analog input pin(pin15 to pin20) : sw6 kara sw8   no ninsiki
00113     * @param PinName adinput3 : analog input pin(pin15 to pin20) : sw9 kara sw11  no ninsiki     
00114     * @param PinName adinput4 : analog input pin(pin15 to pin20) : sw12 kara sw14 no ninsiki     
00115     * @param PinName adinput5 : analog input pin(pin15 to pin20) : sw15 kara sw17 no ninsiki     
00116     *
00117     * Recognition of the SW period is 10ms
00118     */
00119   
00120 #ifdef LPC1768
00121     SwAnalog(PinName adinput0  = NC, PinName adinput1  = NC, PinName adinput2 = NC, PinName adinput3  = NC, PinName adinput4  = NC,
00122               PinName adinput5  = NC
00123               );
00124 #else // ~LPC1768
00125    SwAnalog(PinName adinput0  = NC, PinName adinput1  = NC
00126               );
00127 #endif // LPC1768
00128     /** refresh edge data
00129     *
00130     * @param none 
00131     * @param return none
00132     *
00133     *  main de edge data wo tukau maeni jiko suru
00134     */
00135     void refreshEdgeData(void);
00136     
00137     /** Check Off to On edge
00138     *
00139     * @param uint8_t swNo     : 0:sw0, 1:sw1, ... ,17:sw17     
00140     * @param return uint8_t  On edge check  0: edge Nasi  1: edge Ari
00141     *
00142     */
00143     uint8_t checkEdgeOn(uint8_t swNo);
00144 
00145     /** Check On to Off edge
00146     *
00147     * @param uint8_t swNo     : 0:sw0, 1:sw1, ... ,17:sw17   
00148     * @param return uint8_t Off edge check   0 : Nasi   1 : Ari
00149     *
00150     */
00151     uint8_t checkEdgeOff(uint8_t swNo);
00152     
00153     /** Check sw Level
00154     *
00155     * @param uint8_t swNo     : 0:sw0, 1:sw1, ... ,17:sw17   
00156     * @param return uint8_t sw level check   0 : Off   1 : On
00157     *
00158     */    
00159     uint8_t checkLevel(uint8_t swNo);
00160     
00161     /** SW Number call name
00162     *
00163     * @param adinput0 : Z_sw1 to Z_sw3
00164     * @param adinput1 : Z_sw4 to Z_sw6
00165     * @param  ....
00166     * @param adinput5 : Z_sw16 tp Z_sw18
00167     */
00168 #ifdef LPC1768
00169     enum{
00170         Z_sw0,  // adinput1 no sw
00171         Z_sw1,
00172         Z_sw2,
00173         
00174         Z_sw3,  // adinput2 no sw
00175         Z_sw4,
00176         Z_sw5,
00177         
00178         Z_sw6,  // adinput3 no sw
00179         Z_sw7,
00180         Z_sw8,
00181         
00182         Z_sw9, // adinput4 no sw
00183         Z_sw10,
00184         Z_sw11,
00185         
00186         Z_sw12, // adinput5 no sw
00187         Z_sw13,
00188         Z_sw14,
00189         
00190         Z_sw15, // adinput6 no sw
00191         Z_sw16,
00192         Z_sw17
00193         };
00194 #else // ~LPC1768
00195     enum{
00196         Z_sw0,  // adinput1 no sw
00197         Z_sw1,
00198         Z_sw2,
00199         
00200         Z_sw3,  // adinput2 no sw
00201         Z_sw4,
00202         Z_sw5
00203     };
00204 #endif // LPC1768
00205 //protected:    
00206 
00207 #ifdef LPC1768
00208    AnalogIn _adinput0;
00209    AnalogIn _adinput1;
00210    AnalogIn _adinput2;
00211    AnalogIn _adinput3;
00212    AnalogIn _adinput4;
00213 
00214    AnalogIn _adinput5;
00215 #else // ~LPC1768
00216    AnalogIn _adinput0;
00217    AnalogIn _adinput1;
00218 #endif   
00219    
00220    Ticker swCheckTimer;
00221    
00222    void input(void);
00223 
00224    #define Z_matchcycle (10000) // 10000[us](10[ms]) to 100000[us](100[ms])  1[us]/count
00225    
00226    uint8_t D_swPinSuu;         // touroku sareta Sw Pin Suu  1 to Z_swPinSuuMax
00227    #define Z_swPinSuuMax  (6)  // SW warituke pin suu max
00228    #define Z_swInNoMax    (3)  // 1pin atari no sw setuzoku suu (1pin ni 3ko no sw setuzoku)
00229    
00230    uint8_t B_kariLevel[Z_swPinSuuMax * Z_swInNoMax]; // kakutei mae no ninsiki Level 0bit:saisin(t) 1bit:t-1, ... ,7bit:t-7  0:Off  1:On
00231     // match number define
00232     //#define Z_itchiPattern     (0x03)   // 2kai itch
00233     #define Z_itchiPattern    (0x07)  // 3kai itchi
00234     //#define Z_itchiPattern    (0x0f)  // 4kai itchi
00235     //#define Z_itchiPattern    (0x1f)  // 5kai itchi
00236     //#define Z_itchiPattern    (0x3f)  // 6kai itchi
00237     //#define Z_itchiPattern    (0x7f)  // 7kai itchi
00238     //#define Z_itchiPattern    (0xff)  // 8kai itchi
00239    
00240    // sw level data
00241    uint8_t D_nowLevel[Z_swPinSuuMax * Z_swInNoMax];  // saisin no kakutei Level 0:Off  1:On
00242    uint8_t D_oldLevel[Z_swPinSuuMax * Z_swInNoMax];  // zenkai no kakutei Level 0:Off  1:On
00243     #define Z_levelOff (0)
00244     #define Z_levelOn  (1)
00245    
00246    // sw edge data
00247    // swDigital.c naibu hensu 
00248    uint8_t B_edgeOn[Z_swPinSuuMax * Z_swInNoMax];    // off kara on  no ninsiki(on edge)  0:Nasi  1:Ari
00249    uint8_t B_edgeOff[Z_swPinSuuMax * Z_swInNoMax];   // on  kara off no ninsiki(off edge) 0:Nasi  1:Ari
00250    // user use hensu
00251    uint8_t D_edgeOn[Z_swPinSuuMax * Z_swInNoMax];    // off kara on  no ninsiki(on edge)  0:Nasi  1:Ari
00252    uint8_t D_edgeOff[Z_swPinSuuMax * Z_swInNoMax];   // on  kara off no ninsiki(off edge) 0:Nasi  1:Ari
00253     #define Z_edgeNasi (0)
00254     #define Z_edgeAri  (1)
00255 
00256     //------------------
00257     // Resistor network
00258     //------------------
00259     
00260     //   -.- mbed VOUT(+3.3[V])
00261     //    |                                               |--------------------> mbed p15 - p20(analog port)
00262     //    |   ---------       ---------       ---------   |   ---------
00263     //    .---| Rsw2  |---.---| Rsw1  |---.---| Rsw0  |---.---| Rout  |----|
00264     //    |   ---------   |   ---------   |   ---------   |   ---------    |
00265     //    |     ----      |     -----     |     -----     |                |
00266     //    |-----o  o------.-----o  o------.-----o  o------|              -----
00267     //           SW2            SW1              SW0                      mbed GND(0[V])
00268     // 
00269     //    |                                                                |
00270     //    |<----------------------- Rall --------------------------------->|    
00271     //    |                          |                                     |
00272     //                               ----> Z_R0 to Z_R7
00273     
00274     
00275     #define Z_Rsw2 (8200.0F)   // SW2 no R (1/1 [ohm]/count)
00276     #define Z_Rsw1 (3900.0F)   // SW1 no R (1/1 [ohm]/count)
00277     #define Z_Rsw0 (2000.0F)   // adinput1 no R (1/1 [ohm]/count)
00278     #define Z_Rout (1000.0F)   // Vout no R (1/1 [ohm]/count)
00279     //Z_Rsw2,Z_Rsw1,Z_Rsw0,Z_Rout niwa +-1[%]no seido no teiko wo tukau koto
00280     
00281     #define Z_gosaMax (1.020F) // Z_Rout(max) / Z_Rx(min) = (Z_Rout * 1.01) / (Z_Rx * 0.99) = (Z_Rout / Z_Rx) * 1.020
00282     #define Z_gosaMin (0.990F) // Z_Rout(min) / Z_Rx(max) = (Z_Rout * 0.99) / (Z_Rx * 1.01) = (Z_Rout / Z_Rx) * 0.980
00283     
00284     // Rall keisanchi
00285                                                            // SW2  SW1  SW0
00286     #define Z_R0 ((Z_Rsw2 + Z_Rsw1 + Z_Rsw0 + Z_Rout))     // OFF  OFF  OFF
00287     #define Z_R1 ((Z_Rsw2 + Z_Rsw1 + 0      + Z_Rout))     // OFF  OFF   ON
00288     #define Z_R2 ((Z_Rsw2 + 0      + Z_Rsw0 + Z_Rout))     // OFF   ON  OFF 
00289     #define Z_R3 ((Z_Rsw2 + 0      + 0      + Z_Rout))     // OFF   ON   ON
00290     #define Z_R4 ((0      + Z_Rsw1 + Z_Rsw0 + Z_Rout))     //  ON  OFF  OFF
00291     #define Z_R5 ((0      + Z_Rsw1 + 0      + Z_Rout))     //  ON  OFF   ON
00292     #define Z_R6 ((0      + 0      + Z_Rsw0 + Z_Rout))     //  ON   ON  OFF
00293     #define Z_R7 ((0      + 0      + 0      + Z_Rout))     //  ON   ON   ON    
00294     
00295     // Rout : Rall (max , min)
00296     #define Z_R0max  (((Z_Rout * Z_gosaMax) / Z_R0))
00297     #define Z_R0min  (((Z_Rout * Z_gosaMin) / Z_R0))
00298     #define Z_R1max  (((Z_Rout * Z_gosaMax) / Z_R1))
00299     #define Z_R1min  (((Z_Rout * Z_gosaMin) / Z_R1))
00300     #define Z_R2max  (((Z_Rout * Z_gosaMax) / Z_R2))
00301     #define Z_R2min  (((Z_Rout * Z_gosaMin) / Z_R2))    
00302     #define Z_R3max  (((Z_Rout * Z_gosaMax) / Z_R3))
00303     #define Z_R3min  (((Z_Rout * Z_gosaMin) / Z_R3))    
00304     #define Z_R4max  (((Z_Rout * Z_gosaMax) / Z_R4))
00305     #define Z_R4min  (((Z_Rout * Z_gosaMin) / Z_R4))
00306     #define Z_R5max  (((Z_Rout * Z_gosaMax) / Z_R5))
00307     #define Z_R5min  (((Z_Rout * Z_gosaMin) / Z_R5))    
00308     #define Z_R6max  (((Z_Rout * Z_gosaMax) / Z_R6))
00309     #define Z_R6min  (((Z_Rout * Z_gosaMin) / Z_R6))   
00310     #define Z_R7max  (((Z_Rout * Z_gosaMax) / Z_R7))
00311     #define Z_R7min  (((Z_Rout * Z_gosaMin) / Z_R7))    
00312     
00313     // threshold lvevel
00314     //  GND(0[V]) -> 0_1 -> 1_2 -> 2_3 -> 3_4 -> 4_5 -> 5_6 -> 6_7 -> Vcc(3.3[V]) 
00315     //  --------------+------+------+------+------+------+------+---------------
00316     //   SW0 |    OFF     ON    OFF     ON    OFF    ON     OFF     ON     
00317     //   SW1 |    OFF    OFF     ON     ON    OFF   OFF      ON     ON
00318     //   SW2 |    OFF    OFF    OFF    OFF     ON    ON      ON     ON 
00319     
00320     #define Z_threshold0_1  (((Z_R0max + Z_R1min) / 2))
00321     #define Z_threshold1_2  (((Z_R1max + Z_R2min) / 2))    
00322     #define Z_threshold2_3  (((Z_R2max + Z_R3min) / 2))    
00323     #define Z_threshold3_4  (((Z_R3max + Z_R4min) / 2))        
00324     #define Z_threshold4_5  (((Z_R4max + Z_R5min) / 2))    
00325     #define Z_threshold5_6  (((Z_R5max + Z_R6min) / 2))    
00326     #define Z_threshold6_7  (((Z_R6max + Z_R7min) / 2))            
00327     
00328     void adInput(float ad, uint8_t swInNo);
00329 
00330 };    
00331 
00332 #endif    // _SWANALOG_H