modtronix H / modtronix_im4OLED

Fork of Adafruit_GFX by Neal Horman

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers im4oled.cpp Source File

im4oled.cpp

00001 /**
00002  * File:      im4oled.h
00003  *
00004  * Author:    Modtronix Engineering - www.modtronix.com
00005  *
00006  * Description:
00007  *
00008  * Software License Agreement:
00009  * This software has been written or modified by Modtronix Engineering. The code
00010  * may be modified and can be used free of charge for commercial and non commercial
00011  * applications. If this is modified software, any license conditions from original
00012  * software also apply. Any redistribution must include reference to 'Modtronix
00013  * Engineering' and web link(www.modtronix.com) in the file header.
00014  *
00015  * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
00016  * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
00017  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
00018  * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
00019  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
00020  */
00021 #include "mbed.h"
00022 #include "im4oled.h"
00023 #include "mx_tick.h"
00024 
00025 #define BTN_SAMPLES 4
00026 
00027 static MxTick  mxTick;
00028 
00029 #if (IM4OLED_VIA_PT01NZ==0)
00030 /*
00031  * Constructor
00032  */
00033 Im4OLED::Im4OLED(PinName pinOK, PinName pinStar, PinName pinUp, PinName pinDown)
00034     : btnOK(pinOK, PullUp), btnStar(pinStar, PullUp), btnUp(pinUp, PullUp), btnDown(pinDown, PullUp)
00035 {
00036     delayTillRepeat = 80;   //Repeats start after 800ms
00037     repeatPeriod = 20;      //200mS between button repeats
00038     repeatBtnId = 0xff;     //No button currently pressed donw
00039     repeatCount = 0;
00040     tmrRepeat = 0;
00041 
00042     // reset all the flags and counters
00043     memset(&arrButtons, 0, sizeof(arrButtons));
00044     memset(&arrBtnFallingCnt, 0, sizeof(arrBtnFallingCnt));
00045     memset(&arrBtnFlags, 0, sizeof(arrBtnFlags));
00046 
00047     // Read pins every 10ms
00048     _ticker.attach(this, &Im4OLED::_sample, 0.01);
00049 }
00050 #else
00051 /*
00052  * Constructor
00053  */
00054 Im4OLED::Im4OLED(PinName pinOKStar, PinName pinUpDown)
00055     : btnOKStar(pinOKStar, PullUp), btnUpDown(pinUpDown, PullUp)
00056 {
00057     delayTillRepeat = 80;   //Repeats start after 800ms
00058     repeatPeriod = 20;      //200mS between button repeats
00059     repeatBtnId = 0xff;     //No button currently pressed donw
00060     repeatCount = 0;
00061     tmrRepeat = 0;
00062 
00063     // reset all the flags and counters
00064     memset(&arrButtons, 0, sizeof(arrButtons));
00065     memset(&arrBtnFallingCnt, 0, sizeof(arrBtnFallingCnt));
00066     memset(&arrBtnFlags, 0, sizeof(arrBtnFlags));
00067 
00068     // Read pins every 10ms
00069     _ticker.attach(this, &Im4OLED::_sample, 0.01);
00070 }
00071 #endif
00072 
00073 
00074 void Im4OLED::_sample() {
00075     bool anyBtnPressed = false;    //Check if any button pressed
00076     uint16_t i;
00077     uint8_t val[IM4OLED_BUTTONS];
00078 
00079 #if (IM4OLED_VIA_PT01NZ==0)
00080     val[0] = btnOK.read();
00081     val[1] = btnStar.read();
00082     val[2] = btnUp.read();
00083     val[3] = btnDown.read();
00084 #else
00085     //All buttons not pressed
00086     val[0] = val[1] = val[2] = val[3] = 1;
00087 
00088     //Test if OK or Down is pressed. To test this, enable pullups(default) on both
00089     //pins, and test if pins are pulled low.
00090     if(btnOKStar.read() == 0) {
00091         val[0] = 0; //Star pressed
00092     }
00093     btnOKStar.mode(PullDown);
00094     if(btnUpDown.read() == 0) {
00095         val[3] = 0; //Down pressed
00096     }
00097     btnUpDown.mode(PullDown);
00098 
00099     //Test if Star or Up is pressed. To test this, enable pulldown on both
00100     //pins, and test if pins are pulled high.
00101     wait_us(5);     //Delay time for pulldown to settle
00102     if(btnOKStar.read() == 1) {
00103         val[1] = 0; //OK pressed
00104     }
00105     if(btnUpDown.read() == 1) {
00106         val[2] = 0; //Up pressed
00107     }
00108 
00109     //Enable default state - pullups on both pins
00110     btnOKStar.mode(PullUp);
00111     btnUpDown.mode(PullUp);
00112 #endif
00113 
00114     for(i=0; i<IM4OLED_BUTTONS; i++) {
00115         // Current button PRESSED /////////////////////////////////////////////
00116         if(val[i] == 0) {
00117             if(arrButtons[i] < BTN_SAMPLES) {
00118                 arrButtons[i]++;
00119             }
00120 
00121             //Debounced Button is pressed
00122             if(arrButtons[i] == BTN_SAMPLES) {
00123                 anyBtnPressed = true;  //Indicate that a button is pressed
00124 
00125                 //This button was already pressed
00126                 if (repeatBtnId == i) {
00127                     //The initial "Delay till repeat" time has not passed yet
00128                     if (repeatCount == 0) {
00129                         //Has the "Delay till repeat" time passed yet
00130                         if ((mxTick.read_ms10() - tmrRepeat) > delayTillRepeat) {
00131                             repeatCount++;
00132                             tmrRepeat = mxTick.read_ms10();
00133                             arrBtnFlags[i].flags.bit.fallingLatch = 0;  //Simulate a button press
00134                         }
00135                     }
00136                     //Button is repeating
00137                     else {
00138                         //Has the "period" time passed yet
00139                         if ((mxTick.read_ms10() - tmrRepeat) > repeatPeriod) {
00140                             repeatCount++;
00141                             tmrRepeat = mxTick.read_ms10();
00142                             arrBtnFlags[i].flags.bit.fallingLatch = 0;  //Simulate a button press
00143                         }
00144                     }
00145                 }
00146                 //This button was NOT pressed till now!
00147                 else {
00148                     repeatCount = 0;
00149                     tmrRepeat = mxTick.read_ms10();
00150                 }
00151 
00152                 //Button just change from released->pressed
00153                 if(arrBtnFlags[i].flags.bit.fallingLatch == 0) {
00154                     arrBtnFlags[i].flags.bit.fallingLatch = 1;
00155                     arrBtnFallingCnt[i]++;
00156                     repeatBtnId = i;    //Remember pressed button ID
00157                 }
00158             }
00159         }
00160         // Current button RELEASED ////////////////////////////////////////////
00161         else {
00162             if (arrButtons[i] > 0) {
00163                 arrButtons[i]--;
00164             }
00165 
00166             //Debounced Button is released
00167             if(arrButtons[i] == 0) {
00168                 //Reset fallingLatch
00169                 arrBtnFlags[i].flags.bit.fallingLatch = 0;
00170             }
00171         }
00172     }
00173 
00174     //All buttons released
00175     if(anyBtnPressed == false) {
00176         repeatBtnId = 0xff; //Indicate no button pressed
00177     }
00178 }
00179 
00180 bool Im4OLED::wasAnyBtnPressed(void) {
00181     for(int i=0; i<IM4OLED_BUTTONS; i++) {
00182         if(arrBtnFallingCnt[i]!=0) {
00183             return true;
00184         }
00185     }
00186     return false;
00187 }
00188 
00189 bool Im4OLED::resetAllFalling(void) {
00190     bool retVal = false;
00191     for(int i=0; i<IM4OLED_BUTTONS; i++) {
00192         if(getBtnFalling(i)!=0) {
00193             retVal=true;
00194         }
00195     }
00196     return retVal;
00197 }
00198 
00199 
00200 bool Im4OLED::getOkBtn(void) {
00201     //return btnOK.read();
00202     return arrButtons[BTN_ID_OK] == BTN_SAMPLES;
00203 }
00204 
00205 
00206 bool Im4OLED::getStarBtn(void) {
00207     return arrButtons[BTN_ID_STAR] == BTN_SAMPLES;
00208 }
00209 
00210 
00211 bool Im4OLED::getUpBtn(void) {
00212     return arrButtons[BTN_ID_UP] == BTN_SAMPLES;
00213 }
00214 
00215 
00216 bool Im4OLED::getDownBtn(void) {
00217     return arrButtons[BTN_ID_DOWN] == BTN_SAMPLES;
00218 }
00219 
00220 uint8_t Im4OLED::getBtnFalling(uint16_t btnID) {
00221     uint8_t retVal = 0;
00222     if(arrBtnFallingCnt[btnID]!=0) {
00223         retVal = arrBtnFallingCnt[btnID];
00224         arrBtnFallingCnt[btnID] = 0;
00225     }
00226     return retVal;
00227 }
00228 
00229 /**
00230  * Return number of times the OK button was pressed, and resets it to 0.
00231  */
00232 uint8_t Im4OLED::getOkBtnFalling(void) {
00233     return getBtnFalling(0);
00234 }
00235 
00236 /**
00237  * Return number of times the Star button was pressed, and resets it to 0.
00238  */
00239 uint8_t Im4OLED::getStarBtnFalling(void) {
00240     return getBtnFalling(1);
00241 }
00242 
00243 /**
00244  * Return number of times the Up button was pressed, and resets it to 0.
00245  */
00246 uint8_t Im4OLED::getUpBtnFalling(void) {
00247     return getBtnFalling(2);
00248 }
00249 
00250 /**
00251  * Return number of times the Down button was pressed, and resets it to 0.
00252  */
00253 uint8_t Im4OLED::getDownBtnFalling(void) {
00254     return getBtnFalling(3);
00255 }
00256 
00257