Library for Modtronix im4OLED board with 128x64 OLED and 4 buttons. For details, see product page http://modtronix.com/im4oled.html. Is a clone of Adafruit_GFX library, with some additional code added.
Fork of Adafruit_GFX by
im4oled.cpp@27:dd7d538d3849, 2016-08-19 (annotated)
- Committer:
- modtronix-com
- Date:
- Fri Aug 19 15:44:25 2016 +1000
- Revision:
- 27:dd7d538d3849
- Parent:
- 26:ef08580c35df
Added tag v1.1 for changeset ef08580c35df
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
modtronix-com |
22:f63aeb3769b5 | 1 | /** |
modtronix-com |
22:f63aeb3769b5 | 2 | * File: im4oled.h |
modtronix-com |
22:f63aeb3769b5 | 3 | * |
modtronix-com |
22:f63aeb3769b5 | 4 | * Author: Modtronix Engineering - www.modtronix.com |
modtronix-com |
22:f63aeb3769b5 | 5 | * |
modtronix-com |
22:f63aeb3769b5 | 6 | * Description: |
modtronix-com |
22:f63aeb3769b5 | 7 | * |
modtronix-com |
22:f63aeb3769b5 | 8 | * Software License Agreement: |
modtronix-com |
22:f63aeb3769b5 | 9 | * This software has been written or modified by Modtronix Engineering. The code |
modtronix-com |
22:f63aeb3769b5 | 10 | * may be modified and can be used free of charge for commercial and non commercial |
modtronix-com |
22:f63aeb3769b5 | 11 | * applications. If this is modified software, any license conditions from original |
modtronix-com |
22:f63aeb3769b5 | 12 | * software also apply. Any redistribution must include reference to 'Modtronix |
modtronix-com |
22:f63aeb3769b5 | 13 | * Engineering' and web link(www.modtronix.com) in the file header. |
modtronix-com |
22:f63aeb3769b5 | 14 | * |
modtronix-com |
22:f63aeb3769b5 | 15 | * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS, |
modtronix-com |
22:f63aeb3769b5 | 16 | * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
modtronix-com |
22:f63aeb3769b5 | 17 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE |
modtronix-com |
22:f63aeb3769b5 | 18 | * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
modtronix-com |
22:f63aeb3769b5 | 19 | * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
modtronix-com |
22:f63aeb3769b5 | 20 | */ |
modtronix-com |
26:ef08580c35df | 21 | #include "mbed.h" |
modtronix-com |
22:f63aeb3769b5 | 22 | #include "im4oled.h" |
modtronix-com |
22:f63aeb3769b5 | 23 | #include "mx_tick.h" |
modtronix | 18:cad3dec05e0d | 24 | |
modtronix | 18:cad3dec05e0d | 25 | #define BTN_SAMPLES 4 |
modtronix | 18:cad3dec05e0d | 26 | |
modtronix-com |
22:f63aeb3769b5 | 27 | static MxTick mxTick; |
modtronix-com |
22:f63aeb3769b5 | 28 | |
modtronix-com |
24:db31831844e4 | 29 | #if (IM4OLED_VIA_PT01NZ==0) |
modtronix | 18:cad3dec05e0d | 30 | /* |
modtronix | 18:cad3dec05e0d | 31 | * Constructor |
modtronix | 18:cad3dec05e0d | 32 | */ |
modtronix | 20:0d7144a4d1ca | 33 | Im4OLED::Im4OLED(PinName pinOK, PinName pinStar, PinName pinUp, PinName pinDown) |
modtronix-com |
22:f63aeb3769b5 | 34 | : btnOK(pinOK, PullUp), btnStar(pinStar, PullUp), btnUp(pinUp, PullUp), btnDown(pinDown, PullUp) |
modtronix-com |
22:f63aeb3769b5 | 35 | { |
modtronix-com |
22:f63aeb3769b5 | 36 | delayTillRepeat = 80; //Repeats start after 800ms |
modtronix-com |
22:f63aeb3769b5 | 37 | repeatPeriod = 20; //200mS between button repeats |
modtronix-com |
22:f63aeb3769b5 | 38 | repeatBtnId = 0xff; //No button currently pressed donw |
modtronix-com |
22:f63aeb3769b5 | 39 | repeatCount = 0; |
modtronix-com |
22:f63aeb3769b5 | 40 | tmrRepeat = 0; |
modtronix | 18:cad3dec05e0d | 41 | |
modtronix | 18:cad3dec05e0d | 42 | // reset all the flags and counters |
modtronix | 18:cad3dec05e0d | 43 | memset(&arrButtons, 0, sizeof(arrButtons)); |
modtronix-com |
22:f63aeb3769b5 | 44 | memset(&arrBtnFallingCnt, 0, sizeof(arrBtnFallingCnt)); |
modtronix | 18:cad3dec05e0d | 45 | memset(&arrBtnFlags, 0, sizeof(arrBtnFlags)); |
modtronix | 18:cad3dec05e0d | 46 | |
modtronix | 18:cad3dec05e0d | 47 | // Read pins every 10ms |
modtronix | 20:0d7144a4d1ca | 48 | _ticker.attach(this, &Im4OLED::_sample, 0.01); |
modtronix | 18:cad3dec05e0d | 49 | } |
modtronix-com |
24:db31831844e4 | 50 | #else |
modtronix-com |
24:db31831844e4 | 51 | /* |
modtronix-com |
24:db31831844e4 | 52 | * Constructor |
modtronix-com |
24:db31831844e4 | 53 | */ |
modtronix-com |
24:db31831844e4 | 54 | Im4OLED::Im4OLED(PinName pinOKStar, PinName pinUpDown) |
modtronix-com |
24:db31831844e4 | 55 | : btnOKStar(pinOKStar, PullUp), btnUpDown(pinUpDown, PullUp) |
modtronix-com |
24:db31831844e4 | 56 | { |
modtronix-com |
24:db31831844e4 | 57 | delayTillRepeat = 80; //Repeats start after 800ms |
modtronix-com |
24:db31831844e4 | 58 | repeatPeriod = 20; //200mS between button repeats |
modtronix-com |
24:db31831844e4 | 59 | repeatBtnId = 0xff; //No button currently pressed donw |
modtronix-com |
24:db31831844e4 | 60 | repeatCount = 0; |
modtronix-com |
24:db31831844e4 | 61 | tmrRepeat = 0; |
modtronix-com |
24:db31831844e4 | 62 | |
modtronix-com |
24:db31831844e4 | 63 | // reset all the flags and counters |
modtronix-com |
24:db31831844e4 | 64 | memset(&arrButtons, 0, sizeof(arrButtons)); |
modtronix-com |
24:db31831844e4 | 65 | memset(&arrBtnFallingCnt, 0, sizeof(arrBtnFallingCnt)); |
modtronix-com |
24:db31831844e4 | 66 | memset(&arrBtnFlags, 0, sizeof(arrBtnFlags)); |
modtronix-com |
24:db31831844e4 | 67 | |
modtronix-com |
24:db31831844e4 | 68 | // Read pins every 10ms |
modtronix-com |
24:db31831844e4 | 69 | _ticker.attach(this, &Im4OLED::_sample, 0.01); |
modtronix-com |
24:db31831844e4 | 70 | } |
modtronix-com |
24:db31831844e4 | 71 | #endif |
modtronix-com |
24:db31831844e4 | 72 | |
modtronix | 18:cad3dec05e0d | 73 | |
modtronix | 20:0d7144a4d1ca | 74 | void Im4OLED::_sample() { |
modtronix-com |
22:f63aeb3769b5 | 75 | bool anyBtnPressed = false; //Check if any button pressed |
modtronix | 18:cad3dec05e0d | 76 | uint16_t i; |
modtronix-com |
22:f63aeb3769b5 | 77 | uint8_t val[IM4OLED_BUTTONS]; |
modtronix | 18:cad3dec05e0d | 78 | |
modtronix-com |
24:db31831844e4 | 79 | #if (IM4OLED_VIA_PT01NZ==0) |
modtronix | 18:cad3dec05e0d | 80 | val[0] = btnOK.read(); |
modtronix | 18:cad3dec05e0d | 81 | val[1] = btnStar.read(); |
modtronix | 18:cad3dec05e0d | 82 | val[2] = btnUp.read(); |
modtronix | 18:cad3dec05e0d | 83 | val[3] = btnDown.read(); |
modtronix-com |
24:db31831844e4 | 84 | #else |
modtronix-com |
24:db31831844e4 | 85 | //All buttons not pressed |
modtronix-com |
24:db31831844e4 | 86 | val[0] = val[1] = val[2] = val[3] = 1; |
modtronix-com |
24:db31831844e4 | 87 | |
modtronix-com |
24:db31831844e4 | 88 | //Test if OK or Down is pressed. To test this, enable pullups(default) on both |
modtronix-com |
24:db31831844e4 | 89 | //pins, and test if pins are pulled low. |
modtronix-com |
24:db31831844e4 | 90 | if(btnOKStar.read() == 0) { |
modtronix-com |
24:db31831844e4 | 91 | val[0] = 0; //Star pressed |
modtronix-com |
24:db31831844e4 | 92 | } |
modtronix-com |
24:db31831844e4 | 93 | btnOKStar.mode(PullDown); |
modtronix-com |
24:db31831844e4 | 94 | if(btnUpDown.read() == 0) { |
modtronix-com |
24:db31831844e4 | 95 | val[3] = 0; //Down pressed |
modtronix-com |
24:db31831844e4 | 96 | } |
modtronix-com |
24:db31831844e4 | 97 | btnUpDown.mode(PullDown); |
modtronix-com |
24:db31831844e4 | 98 | |
modtronix-com |
24:db31831844e4 | 99 | //Test if Star or Up is pressed. To test this, enable pulldown on both |
modtronix-com |
24:db31831844e4 | 100 | //pins, and test if pins are pulled high. |
modtronix-com |
24:db31831844e4 | 101 | wait_us(5); //Delay time for pulldown to settle |
modtronix-com |
24:db31831844e4 | 102 | if(btnOKStar.read() == 1) { |
modtronix-com |
24:db31831844e4 | 103 | val[1] = 0; //OK pressed |
modtronix-com |
24:db31831844e4 | 104 | } |
modtronix-com |
24:db31831844e4 | 105 | if(btnUpDown.read() == 1) { |
modtronix-com |
24:db31831844e4 | 106 | val[2] = 0; //Up pressed |
modtronix-com |
24:db31831844e4 | 107 | } |
modtronix-com |
24:db31831844e4 | 108 | |
modtronix-com |
24:db31831844e4 | 109 | //Enable default state - pullups on both pins |
modtronix-com |
24:db31831844e4 | 110 | btnOKStar.mode(PullUp); |
modtronix-com |
24:db31831844e4 | 111 | btnUpDown.mode(PullUp); |
modtronix-com |
24:db31831844e4 | 112 | #endif |
modtronix | 18:cad3dec05e0d | 113 | |
modtronix-com |
22:f63aeb3769b5 | 114 | for(i=0; i<IM4OLED_BUTTONS; i++) { |
modtronix-com |
22:f63aeb3769b5 | 115 | // Current button PRESSED ///////////////////////////////////////////// |
modtronix | 18:cad3dec05e0d | 116 | if(val[i] == 0) { |
modtronix | 18:cad3dec05e0d | 117 | if(arrButtons[i] < BTN_SAMPLES) { |
modtronix | 18:cad3dec05e0d | 118 | arrButtons[i]++; |
modtronix | 18:cad3dec05e0d | 119 | } |
modtronix | 18:cad3dec05e0d | 120 | |
modtronix-com |
22:f63aeb3769b5 | 121 | //Debounced Button is pressed |
modtronix | 18:cad3dec05e0d | 122 | if(arrButtons[i] == BTN_SAMPLES) { |
modtronix-com |
22:f63aeb3769b5 | 123 | anyBtnPressed = true; //Indicate that a button is pressed |
modtronix-com |
22:f63aeb3769b5 | 124 | |
modtronix-com |
22:f63aeb3769b5 | 125 | //This button was already pressed |
modtronix-com |
22:f63aeb3769b5 | 126 | if (repeatBtnId == i) { |
modtronix-com |
22:f63aeb3769b5 | 127 | //The initial "Delay till repeat" time has not passed yet |
modtronix-com |
22:f63aeb3769b5 | 128 | if (repeatCount == 0) { |
modtronix-com |
22:f63aeb3769b5 | 129 | //Has the "Delay till repeat" time passed yet |
modtronix-com |
22:f63aeb3769b5 | 130 | if ((mxTick.read_ms10() - tmrRepeat) > delayTillRepeat) { |
modtronix-com |
22:f63aeb3769b5 | 131 | repeatCount++; |
modtronix-com |
22:f63aeb3769b5 | 132 | tmrRepeat = mxTick.read_ms10(); |
modtronix-com |
22:f63aeb3769b5 | 133 | arrBtnFlags[i].flags.bit.fallingLatch = 0; //Simulate a button press |
modtronix-com |
22:f63aeb3769b5 | 134 | } |
modtronix-com |
22:f63aeb3769b5 | 135 | } |
modtronix-com |
22:f63aeb3769b5 | 136 | //Button is repeating |
modtronix-com |
22:f63aeb3769b5 | 137 | else { |
modtronix-com |
22:f63aeb3769b5 | 138 | //Has the "period" time passed yet |
modtronix-com |
22:f63aeb3769b5 | 139 | if ((mxTick.read_ms10() - tmrRepeat) > repeatPeriod) { |
modtronix-com |
22:f63aeb3769b5 | 140 | repeatCount++; |
modtronix-com |
22:f63aeb3769b5 | 141 | tmrRepeat = mxTick.read_ms10(); |
modtronix-com |
22:f63aeb3769b5 | 142 | arrBtnFlags[i].flags.bit.fallingLatch = 0; //Simulate a button press |
modtronix-com |
22:f63aeb3769b5 | 143 | } |
modtronix-com |
22:f63aeb3769b5 | 144 | } |
modtronix-com |
22:f63aeb3769b5 | 145 | } |
modtronix-com |
22:f63aeb3769b5 | 146 | //This button was NOT pressed till now! |
modtronix-com |
22:f63aeb3769b5 | 147 | else { |
modtronix-com |
22:f63aeb3769b5 | 148 | repeatCount = 0; |
modtronix-com |
22:f63aeb3769b5 | 149 | tmrRepeat = mxTick.read_ms10(); |
modtronix-com |
22:f63aeb3769b5 | 150 | } |
modtronix-com |
22:f63aeb3769b5 | 151 | |
modtronix-com |
22:f63aeb3769b5 | 152 | //Button just change from released->pressed |
modtronix | 18:cad3dec05e0d | 153 | if(arrBtnFlags[i].flags.bit.fallingLatch == 0) { |
modtronix | 18:cad3dec05e0d | 154 | arrBtnFlags[i].flags.bit.fallingLatch = 1; |
modtronix-com |
22:f63aeb3769b5 | 155 | arrBtnFallingCnt[i]++; |
modtronix-com |
22:f63aeb3769b5 | 156 | repeatBtnId = i; //Remember pressed button ID |
modtronix | 18:cad3dec05e0d | 157 | } |
modtronix | 18:cad3dec05e0d | 158 | } |
modtronix | 18:cad3dec05e0d | 159 | } |
modtronix-com |
22:f63aeb3769b5 | 160 | // Current button RELEASED //////////////////////////////////////////// |
modtronix | 18:cad3dec05e0d | 161 | else { |
modtronix | 18:cad3dec05e0d | 162 | if (arrButtons[i] > 0) { |
modtronix | 18:cad3dec05e0d | 163 | arrButtons[i]--; |
modtronix | 18:cad3dec05e0d | 164 | } |
modtronix | 18:cad3dec05e0d | 165 | |
modtronix-com |
22:f63aeb3769b5 | 166 | //Debounced Button is released |
modtronix | 18:cad3dec05e0d | 167 | if(arrButtons[i] == 0) { |
modtronix | 18:cad3dec05e0d | 168 | //Reset fallingLatch |
modtronix | 18:cad3dec05e0d | 169 | arrBtnFlags[i].flags.bit.fallingLatch = 0; |
modtronix | 18:cad3dec05e0d | 170 | } |
modtronix | 18:cad3dec05e0d | 171 | } |
modtronix | 18:cad3dec05e0d | 172 | } |
modtronix-com |
22:f63aeb3769b5 | 173 | |
modtronix-com |
22:f63aeb3769b5 | 174 | //All buttons released |
modtronix-com |
22:f63aeb3769b5 | 175 | if(anyBtnPressed == false) { |
modtronix-com |
22:f63aeb3769b5 | 176 | repeatBtnId = 0xff; //Indicate no button pressed |
modtronix-com |
22:f63aeb3769b5 | 177 | } |
modtronix | 18:cad3dec05e0d | 178 | } |
modtronix | 18:cad3dec05e0d | 179 | |
modtronix-com |
22:f63aeb3769b5 | 180 | bool Im4OLED::wasAnyBtnPressed(void) { |
modtronix-com |
22:f63aeb3769b5 | 181 | for(int i=0; i<IM4OLED_BUTTONS; i++) { |
modtronix-com |
22:f63aeb3769b5 | 182 | if(arrBtnFallingCnt[i]!=0) { |
modtronix-com |
22:f63aeb3769b5 | 183 | return true; |
modtronix-com |
22:f63aeb3769b5 | 184 | } |
modtronix-com |
22:f63aeb3769b5 | 185 | } |
modtronix-com |
22:f63aeb3769b5 | 186 | return false; |
modtronix | 18:cad3dec05e0d | 187 | } |
modtronix | 18:cad3dec05e0d | 188 | |
modtronix-com |
22:f63aeb3769b5 | 189 | bool Im4OLED::resetAllFalling(void) { |
modtronix-com |
22:f63aeb3769b5 | 190 | bool retVal = false; |
modtronix-com |
22:f63aeb3769b5 | 191 | for(int i=0; i<IM4OLED_BUTTONS; i++) { |
modtronix-com |
22:f63aeb3769b5 | 192 | if(getBtnFalling(i)!=0) { |
modtronix-com |
22:f63aeb3769b5 | 193 | retVal=true; |
modtronix-com |
22:f63aeb3769b5 | 194 | } |
modtronix-com |
22:f63aeb3769b5 | 195 | } |
modtronix-com |
22:f63aeb3769b5 | 196 | return retVal; |
modtronix | 18:cad3dec05e0d | 197 | } |
modtronix | 18:cad3dec05e0d | 198 | |
modtronix | 18:cad3dec05e0d | 199 | |
modtronix-com |
22:f63aeb3769b5 | 200 | bool Im4OLED::getOkBtn(void) { |
modtronix-com |
22:f63aeb3769b5 | 201 | //return btnOK.read(); |
modtronix-com |
22:f63aeb3769b5 | 202 | return arrButtons[BTN_ID_OK] == BTN_SAMPLES; |
modtronix-com |
22:f63aeb3769b5 | 203 | } |
modtronix-com |
22:f63aeb3769b5 | 204 | |
modtronix-com |
22:f63aeb3769b5 | 205 | |
modtronix-com |
22:f63aeb3769b5 | 206 | bool Im4OLED::getStarBtn(void) { |
modtronix-com |
22:f63aeb3769b5 | 207 | return arrButtons[BTN_ID_STAR] == BTN_SAMPLES; |
modtronix | 18:cad3dec05e0d | 208 | } |
modtronix | 18:cad3dec05e0d | 209 | |
modtronix-com |
22:f63aeb3769b5 | 210 | |
modtronix-com |
22:f63aeb3769b5 | 211 | bool Im4OLED::getUpBtn(void) { |
modtronix-com |
22:f63aeb3769b5 | 212 | return arrButtons[BTN_ID_UP] == BTN_SAMPLES; |
modtronix | 18:cad3dec05e0d | 213 | } |
modtronix | 18:cad3dec05e0d | 214 | |
modtronix-com |
22:f63aeb3769b5 | 215 | |
modtronix-com |
22:f63aeb3769b5 | 216 | bool Im4OLED::getDownBtn(void) { |
modtronix-com |
22:f63aeb3769b5 | 217 | return arrButtons[BTN_ID_DOWN] == BTN_SAMPLES; |
modtronix-com |
22:f63aeb3769b5 | 218 | } |
modtronix-com |
22:f63aeb3769b5 | 219 | |
modtronix | 21:e9892783ce17 | 220 | uint8_t Im4OLED::getBtnFalling(uint16_t btnID) { |
modtronix | 21:e9892783ce17 | 221 | uint8_t retVal = 0; |
modtronix-com |
22:f63aeb3769b5 | 222 | if(arrBtnFallingCnt[btnID]!=0) { |
modtronix-com |
22:f63aeb3769b5 | 223 | retVal = arrBtnFallingCnt[btnID]; |
modtronix-com |
22:f63aeb3769b5 | 224 | arrBtnFallingCnt[btnID] = 0; |
modtronix | 18:cad3dec05e0d | 225 | } |
modtronix | 21:e9892783ce17 | 226 | return retVal; |
modtronix | 18:cad3dec05e0d | 227 | } |
modtronix | 18:cad3dec05e0d | 228 | |
modtronix | 18:cad3dec05e0d | 229 | /** |
modtronix | 21:e9892783ce17 | 230 | * Return number of times the OK button was pressed, and resets it to 0. |
modtronix | 18:cad3dec05e0d | 231 | */ |
modtronix | 21:e9892783ce17 | 232 | uint8_t Im4OLED::getOkBtnFalling(void) { |
modtronix | 18:cad3dec05e0d | 233 | return getBtnFalling(0); |
modtronix | 18:cad3dec05e0d | 234 | } |
modtronix | 18:cad3dec05e0d | 235 | |
modtronix | 18:cad3dec05e0d | 236 | /** |
modtronix | 21:e9892783ce17 | 237 | * Return number of times the Star button was pressed, and resets it to 0. |
modtronix | 18:cad3dec05e0d | 238 | */ |
modtronix | 21:e9892783ce17 | 239 | uint8_t Im4OLED::getStarBtnFalling(void) { |
modtronix | 18:cad3dec05e0d | 240 | return getBtnFalling(1); |
modtronix | 18:cad3dec05e0d | 241 | } |
modtronix | 18:cad3dec05e0d | 242 | |
modtronix | 18:cad3dec05e0d | 243 | /** |
modtronix | 21:e9892783ce17 | 244 | * Return number of times the Up button was pressed, and resets it to 0. |
modtronix | 18:cad3dec05e0d | 245 | */ |
modtronix | 21:e9892783ce17 | 246 | uint8_t Im4OLED::getUpBtnFalling(void) { |
modtronix | 18:cad3dec05e0d | 247 | return getBtnFalling(2); |
modtronix | 18:cad3dec05e0d | 248 | } |
modtronix | 18:cad3dec05e0d | 249 | |
modtronix | 18:cad3dec05e0d | 250 | /** |
modtronix | 21:e9892783ce17 | 251 | * Return number of times the Down button was pressed, and resets it to 0. |
modtronix | 18:cad3dec05e0d | 252 | */ |
modtronix | 21:e9892783ce17 | 253 | uint8_t Im4OLED::getDownBtnFalling(void) { |
modtronix | 18:cad3dec05e0d | 254 | return getBtnFalling(3); |
modtronix | 18:cad3dec05e0d | 255 | } |
modtronix | 18:cad3dec05e0d | 256 | |
modtronix | 18:cad3dec05e0d | 257 |