fork of original freetronics library
Fork of Freetronics_16x2_LCD by
freetronicsLCDShield.cpp@4:b4b2435f4a7f, 2016-06-02 (annotated)
- Committer:
- rba90
- Date:
- Thu Jun 02 13:31:16 2016 +0000
- Revision:
- 4:b4b2435f4a7f
- Parent:
- 1:ddcefddda4a7
- Child:
- 5:7b216d7781f8
redefine readButton feature
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KKempeneers | 1:ddcefddda4a7 | 1 | /* mbed freetronicsLCDShield Library, written by Koen J.F. Kempeneers |
KKempeneers | 1:ddcefddda4a7 | 2 | * koen.kempeneers@damiaaninstituut.be |
KKempeneers | 1:ddcefddda4a7 | 3 | * |
KKempeneers | 1:ddcefddda4a7 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
KKempeneers | 1:ddcefddda4a7 | 5 | * of this software and associated documentation files (the "Software"), to deal |
KKempeneers | 1:ddcefddda4a7 | 6 | * in the Software without restriction, including without limitation the rights |
KKempeneers | 1:ddcefddda4a7 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
KKempeneers | 1:ddcefddda4a7 | 8 | * copies of the Software, and to permit persons to whom the Software is |
KKempeneers | 1:ddcefddda4a7 | 9 | * furnished to do so, subject to the following conditions: |
KKempeneers | 1:ddcefddda4a7 | 10 | * |
KKempeneers | 1:ddcefddda4a7 | 11 | * The above copyright notice and this permission notice shall be included in |
KKempeneers | 1:ddcefddda4a7 | 12 | * all copies or substantial portions of the Software. |
KKempeneers | 1:ddcefddda4a7 | 13 | * |
KKempeneers | 1:ddcefddda4a7 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
KKempeneers | 1:ddcefddda4a7 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
KKempeneers | 1:ddcefddda4a7 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
KKempeneers | 1:ddcefddda4a7 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
KKempeneers | 1:ddcefddda4a7 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
KKempeneers | 1:ddcefddda4a7 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
KKempeneers | 1:ddcefddda4a7 | 20 | * THE SOFTWARE. |
KKempeneers | 1:ddcefddda4a7 | 21 | */ |
KKempeneers | 1:ddcefddda4a7 | 22 | |
KKempeneers | 0:01f3d38f8b6d | 23 | #include "mbed.h" |
KKempeneers | 0:01f3d38f8b6d | 24 | #include "freetronicsLCDShield.h" |
KKempeneers | 0:01f3d38f8b6d | 25 | |
KKempeneers | 0:01f3d38f8b6d | 26 | #define PULSE_E wait(0.000001f); \ |
KKempeneers | 0:01f3d38f8b6d | 27 | _e = 0; \ |
KKempeneers | 0:01f3d38f8b6d | 28 | wait(0.000001f); \ |
KKempeneers | 0:01f3d38f8b6d | 29 | _e = 1; |
KKempeneers | 0:01f3d38f8b6d | 30 | |
KKempeneers | 0:01f3d38f8b6d | 31 | |
KKempeneers | 0:01f3d38f8b6d | 32 | freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0) |
KKempeneers | 0:01f3d38f8b6d | 33 | : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) { |
KKempeneers | 0:01f3d38f8b6d | 34 | // Class constructor |
KKempeneers | 0:01f3d38f8b6d | 35 | // Init the display, wait 15ms to insure the power is up |
KKempeneers | 0:01f3d38f8b6d | 36 | _e = true; |
KKempeneers | 0:01f3d38f8b6d | 37 | _rs = false; |
KKempeneers | 0:01f3d38f8b6d | 38 | wait(0.015f); |
KKempeneers | 0:01f3d38f8b6d | 39 | |
KKempeneers | 0:01f3d38f8b6d | 40 | for (int i = 0; i < 3; i++){ |
KKempeneers | 0:01f3d38f8b6d | 41 | writeByte(0x3); |
KKempeneers | 0:01f3d38f8b6d | 42 | wait(0.00164f); // this command takes 1.64ms, so wait for it |
KKempeneers | 0:01f3d38f8b6d | 43 | } |
KKempeneers | 0:01f3d38f8b6d | 44 | |
KKempeneers | 0:01f3d38f8b6d | 45 | writeByte(0x2); // 4-bit mode |
KKempeneers | 0:01f3d38f8b6d | 46 | writeCommand(0x28); // Function set 001 BW N F - - |
KKempeneers | 0:01f3d38f8b6d | 47 | writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off |
KKempeneers | 0:01f3d38f8b6d | 48 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
KKempeneers | 0:01f3d38f8b6d | 49 | cls(); |
KKempeneers | 0:01f3d38f8b6d | 50 | |
KKempeneers | 0:01f3d38f8b6d | 51 | // Set the PWM period to 20ms |
KKempeneers | 0:01f3d38f8b6d | 52 | _bl.period_ms(1); |
KKempeneers | 0:01f3d38f8b6d | 53 | _bl.write(0.0); |
KKempeneers | 0:01f3d38f8b6d | 54 | } |
KKempeneers | 0:01f3d38f8b6d | 55 | |
KKempeneers | 0:01f3d38f8b6d | 56 | void freetronicsLCDShield::setCursorPosition (int line, int col) { |
KKempeneers | 0:01f3d38f8b6d | 57 | // Set the new cursorposition |
KKempeneers | 0:01f3d38f8b6d | 58 | writeCommand(0x80 + (line * 0x40) + col); |
KKempeneers | 0:01f3d38f8b6d | 59 | } |
KKempeneers | 0:01f3d38f8b6d | 60 | |
KKempeneers | 0:01f3d38f8b6d | 61 | void freetronicsLCDShield::setBackLight (bool blStatus) { |
KKempeneers | 0:01f3d38f8b6d | 62 | // Switch the backlight on (true) or off (false) |
KKempeneers | 0:01f3d38f8b6d | 63 | _bl = (blStatus) ? 1.0 : 0.0; |
KKempeneers | 0:01f3d38f8b6d | 64 | } |
KKempeneers | 0:01f3d38f8b6d | 65 | |
KKempeneers | 0:01f3d38f8b6d | 66 | void freetronicsLCDShield::setBackLight (float blIntensity) { |
KKempeneers | 0:01f3d38f8b6d | 67 | // Switch the backlight on (true) or off (false) |
KKempeneers | 0:01f3d38f8b6d | 68 | _bl = blIntensity; |
KKempeneers | 0:01f3d38f8b6d | 69 | } |
KKempeneers | 0:01f3d38f8b6d | 70 | |
KKempeneers | 0:01f3d38f8b6d | 71 | void freetronicsLCDShield::setCursor (bool cStatus, bool blink) { |
KKempeneers | 0:01f3d38f8b6d | 72 | int tmp = 0; |
KKempeneers | 0:01f3d38f8b6d | 73 | |
KKempeneers | 0:01f3d38f8b6d | 74 | if (blink) tmp = 0x01; |
KKempeneers | 0:01f3d38f8b6d | 75 | if (cStatus) tmp |= 0x02; |
KKempeneers | 0:01f3d38f8b6d | 76 | writeCommand(0x0C + tmp); |
KKempeneers | 0:01f3d38f8b6d | 77 | } |
KKempeneers | 0:01f3d38f8b6d | 78 | |
KKempeneers | 1:ddcefddda4a7 | 79 | void freetronicsLCDShield::shift(bool direction) { |
KKempeneers | 1:ddcefddda4a7 | 80 | if(direction == LEFT) shiftLeft(); |
KKempeneers | 1:ddcefddda4a7 | 81 | else shiftRight(); |
KKempeneers | 1:ddcefddda4a7 | 82 | } |
KKempeneers | 1:ddcefddda4a7 | 83 | |
KKempeneers | 1:ddcefddda4a7 | 84 | void freetronicsLCDShield::shiftLeft(void) { |
KKempeneers | 1:ddcefddda4a7 | 85 | writeCommand(0x18 + 0x04); |
KKempeneers | 1:ddcefddda4a7 | 86 | } |
KKempeneers | 1:ddcefddda4a7 | 87 | |
KKempeneers | 1:ddcefddda4a7 | 88 | void freetronicsLCDShield::shiftRight(void) { |
KKempeneers | 1:ddcefddda4a7 | 89 | writeCommand(0x18); |
KKempeneers | 0:01f3d38f8b6d | 90 | } |
KKempeneers | 0:01f3d38f8b6d | 91 | |
KKempeneers | 0:01f3d38f8b6d | 92 | void freetronicsLCDShield::cls(void) { |
KKempeneers | 0:01f3d38f8b6d | 93 | // Clear the display and place the cursor at 0, 0 |
KKempeneers | 0:01f3d38f8b6d | 94 | writeCommand(0x01); |
KKempeneers | 0:01f3d38f8b6d | 95 | wait(0.00164f); |
KKempeneers | 0:01f3d38f8b6d | 96 | } |
KKempeneers | 0:01f3d38f8b6d | 97 | |
KKempeneers | 0:01f3d38f8b6d | 98 | void freetronicsLCDShield::home(void) { |
KKempeneers | 0:01f3d38f8b6d | 99 | // Undo shift operations and place cursor at 0,0 |
KKempeneers | 0:01f3d38f8b6d | 100 | writeCommand(0x02); |
KKempeneers | 0:01f3d38f8b6d | 101 | wait(0.00164f); |
KKempeneers | 0:01f3d38f8b6d | 102 | } |
KKempeneers | 0:01f3d38f8b6d | 103 | |
KKempeneers | 1:ddcefddda4a7 | 104 | void freetronicsLCDShield::writeCGRAM (char address, const char *ptr, char nbytes) { |
KKempeneers | 1:ddcefddda4a7 | 105 | // Write the address only once, it is autoincremented |
KKempeneers | 1:ddcefddda4a7 | 106 | writeCommand(0x40 | address); |
KKempeneers | 1:ddcefddda4a7 | 107 | |
KKempeneers | 1:ddcefddda4a7 | 108 | // Write the data |
KKempeneers | 1:ddcefddda4a7 | 109 | for(int i = 0; i < nbytes; i++) { |
KKempeneers | 1:ddcefddda4a7 | 110 | writeData(*ptr++); |
KKempeneers | 1:ddcefddda4a7 | 111 | } |
KKempeneers | 1:ddcefddda4a7 | 112 | } |
KKempeneers | 1:ddcefddda4a7 | 113 | |
KKempeneers | 0:01f3d38f8b6d | 114 | // Low level output functions |
KKempeneers | 0:01f3d38f8b6d | 115 | void freetronicsLCDShield::writeByte (int byte) { |
KKempeneers | 0:01f3d38f8b6d | 116 | // Split the byte in high and low nibble, write high nibble first |
KKempeneers | 0:01f3d38f8b6d | 117 | _d = byte >> 4; |
KKempeneers | 0:01f3d38f8b6d | 118 | PULSE_E; |
KKempeneers | 0:01f3d38f8b6d | 119 | _d = byte >> 0; |
KKempeneers | 0:01f3d38f8b6d | 120 | PULSE_E; |
KKempeneers | 0:01f3d38f8b6d | 121 | // Most instructions take 40us |
KKempeneers | 0:01f3d38f8b6d | 122 | wait(0.000040f); |
KKempeneers | 0:01f3d38f8b6d | 123 | } |
KKempeneers | 0:01f3d38f8b6d | 124 | |
KKempeneers | 0:01f3d38f8b6d | 125 | void freetronicsLCDShield::writeData (int data) { |
KKempeneers | 0:01f3d38f8b6d | 126 | _rs = true; |
KKempeneers | 0:01f3d38f8b6d | 127 | writeByte(data); |
KKempeneers | 0:01f3d38f8b6d | 128 | } |
KKempeneers | 0:01f3d38f8b6d | 129 | |
KKempeneers | 0:01f3d38f8b6d | 130 | void freetronicsLCDShield::writeCommand (int command) { |
KKempeneers | 0:01f3d38f8b6d | 131 | _rs = false; |
KKempeneers | 0:01f3d38f8b6d | 132 | writeByte(command); |
KKempeneers | 0:01f3d38f8b6d | 133 | } |
KKempeneers | 0:01f3d38f8b6d | 134 | |
rba90 | 4:b4b2435f4a7f | 135 | float freetronicsLCDShield::readButtonAnalog(void) { |
KKempeneers | 0:01f3d38f8b6d | 136 | return(_a0.read()); |
KKempeneers | 0:01f3d38f8b6d | 137 | } |
KKempeneers | 0:01f3d38f8b6d | 138 | |
rba90 | 4:b4b2435f4a7f | 139 | freetronicsLCDShield::ButtonType_t freetronicsLCDShield::readButton(void) { |
rba90 | 4:b4b2435f4a7f | 140 | float val; |
rba90 | 4:b4b2435f4a7f | 141 | |
rba90 | 4:b4b2435f4a7f | 142 | // read analog value |
rba90 | 4:b4b2435f4a7f | 143 | val = readButtonAnalog(); |
rba90 | 4:b4b2435f4a7f | 144 | |
rba90 | 4:b4b2435f4a7f | 145 | // determine which button is pressed |
rba90 | 4:b4b2435f4a7f | 146 | // analog reading for each button are centered at 0, 144, 329, 504, 741, 1000 |
rba90 | 4:b4b2435f4a7f | 147 | |
rba90 | 4:b4b2435f4a7f | 148 | // right |
rba90 | 4:b4b2435f4a7f | 149 | if (val < 0.0740f) |
rba90 | 4:b4b2435f4a7f | 150 | { |
rba90 | 4:b4b2435f4a7f | 151 | return freetronicsLCDShield::BTN_RIGHT; |
rba90 | 4:b4b2435f4a7f | 152 | } |
rba90 | 4:b4b2435f4a7f | 153 | |
rba90 | 4:b4b2435f4a7f | 154 | // up |
rba90 | 4:b4b2435f4a7f | 155 | else if (val < 0.2625f) |
rba90 | 4:b4b2435f4a7f | 156 | { |
rba90 | 4:b4b2435f4a7f | 157 | return freetronicsLCDShield::BTN_UP; |
rba90 | 4:b4b2435f4a7f | 158 | } |
rba90 | 4:b4b2435f4a7f | 159 | |
rba90 | 4:b4b2435f4a7f | 160 | // down |
rba90 | 4:b4b2435f4a7f | 161 | else if (val < 0.4855f) |
rba90 | 4:b4b2435f4a7f | 162 | { |
rba90 | 4:b4b2435f4a7f | 163 | return freetronicsLCDShield::BTN_DOWN; |
rba90 | 4:b4b2435f4a7f | 164 | } |
rba90 | 4:b4b2435f4a7f | 165 | |
rba90 | 4:b4b2435f4a7f | 166 | // left |
rba90 | 4:b4b2435f4a7f | 167 | else if (val < 0.6815f) |
rba90 | 4:b4b2435f4a7f | 168 | { |
rba90 | 4:b4b2435f4a7f | 169 | return freetronicsLCDShield::BTN_LEFT; |
rba90 | 4:b4b2435f4a7f | 170 | } |
rba90 | 4:b4b2435f4a7f | 171 | |
rba90 | 4:b4b2435f4a7f | 172 | // none |
rba90 | 4:b4b2435f4a7f | 173 | else if (val < 0.8465f) |
rba90 | 4:b4b2435f4a7f | 174 | { |
rba90 | 4:b4b2435f4a7f | 175 | return freetronicsLCDShield::BTN_NONE; |
rba90 | 4:b4b2435f4a7f | 176 | } |
rba90 | 4:b4b2435f4a7f | 177 | |
rba90 | 4:b4b2435f4a7f | 178 | // select |
rba90 | 4:b4b2435f4a7f | 179 | else |
rba90 | 4:b4b2435f4a7f | 180 | { |
rba90 | 4:b4b2435f4a7f | 181 | return freetronicsLCDShield::BTN_SELECT; |
rba90 | 4:b4b2435f4a7f | 182 | } |
rba90 | 4:b4b2435f4a7f | 183 | } |
rba90 | 4:b4b2435f4a7f | 184 | |
KKempeneers | 0:01f3d38f8b6d | 185 | // Virtual functions for stream |
KKempeneers | 0:01f3d38f8b6d | 186 | int freetronicsLCDShield::_putc(int value) { |
KKempeneers | 0:01f3d38f8b6d | 187 | writeData(value); |
KKempeneers | 0:01f3d38f8b6d | 188 | return value; |
KKempeneers | 0:01f3d38f8b6d | 189 | } |
KKempeneers | 0:01f3d38f8b6d | 190 | |
KKempeneers | 0:01f3d38f8b6d | 191 | int freetronicsLCDShield::_getc() { |
KKempeneers | 0:01f3d38f8b6d | 192 | return -1; |
KKempeneers | 0:01f3d38f8b6d | 193 | } |