fork of original freetronics library

Dependents:   AwsomeStation

Fork of Freetronics_16x2_LCD by Components

Committer:
rba90
Date:
Thu Sep 02 08:00:12 2021 +0000
Revision:
5:7b216d7781f8
Parent:
4:b4b2435f4a7f
Add support to MbedOS 6

Who changed what in which revision?

UserRevisionLine numberNew 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"
rba90 5:7b216d7781f8 24 #include "mbed_wait_api.h"
KKempeneers 0:01f3d38f8b6d 25 #include "freetronicsLCDShield.h"
KKempeneers 0:01f3d38f8b6d 26
rba90 5:7b216d7781f8 27 #define PULSE_E wait_ns(1); \
KKempeneers 0:01f3d38f8b6d 28 _e = 0; \
rba90 5:7b216d7781f8 29 wait_ns(1); \
KKempeneers 0:01f3d38f8b6d 30 _e = 1;
KKempeneers 0:01f3d38f8b6d 31
KKempeneers 0:01f3d38f8b6d 32
KKempeneers 0:01f3d38f8b6d 33 freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0)
KKempeneers 0:01f3d38f8b6d 34 : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) {
KKempeneers 0:01f3d38f8b6d 35 // Class constructor
KKempeneers 0:01f3d38f8b6d 36 // Init the display, wait 15ms to insure the power is up
KKempeneers 0:01f3d38f8b6d 37 _e = true;
KKempeneers 0:01f3d38f8b6d 38 _rs = false;
rba90 5:7b216d7781f8 39 ThisThread::sleep_for(15);
KKempeneers 0:01f3d38f8b6d 40
KKempeneers 0:01f3d38f8b6d 41 for (int i = 0; i < 3; i++){
KKempeneers 0:01f3d38f8b6d 42 writeByte(0x3);
rba90 5:7b216d7781f8 43 wait_us(1640); // this command takes 1.64ms, so wait for it
KKempeneers 0:01f3d38f8b6d 44 }
KKempeneers 0:01f3d38f8b6d 45
KKempeneers 0:01f3d38f8b6d 46 writeByte(0x2); // 4-bit mode
KKempeneers 0:01f3d38f8b6d 47 writeCommand(0x28); // Function set 001 BW N F - -
KKempeneers 0:01f3d38f8b6d 48 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 49 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 50 cls();
KKempeneers 0:01f3d38f8b6d 51
KKempeneers 0:01f3d38f8b6d 52 // Set the PWM period to 20ms
KKempeneers 0:01f3d38f8b6d 53 _bl.period_ms(1);
KKempeneers 0:01f3d38f8b6d 54 _bl.write(0.0);
KKempeneers 0:01f3d38f8b6d 55 }
KKempeneers 0:01f3d38f8b6d 56
KKempeneers 0:01f3d38f8b6d 57 void freetronicsLCDShield::setCursorPosition (int line, int col) {
KKempeneers 0:01f3d38f8b6d 58 // Set the new cursorposition
KKempeneers 0:01f3d38f8b6d 59 writeCommand(0x80 + (line * 0x40) + col);
KKempeneers 0:01f3d38f8b6d 60 }
KKempeneers 0:01f3d38f8b6d 61
KKempeneers 0:01f3d38f8b6d 62 void freetronicsLCDShield::setBackLight (bool blStatus) {
KKempeneers 0:01f3d38f8b6d 63 // Switch the backlight on (true) or off (false)
KKempeneers 0:01f3d38f8b6d 64 _bl = (blStatus) ? 1.0 : 0.0;
KKempeneers 0:01f3d38f8b6d 65 }
KKempeneers 0:01f3d38f8b6d 66
KKempeneers 0:01f3d38f8b6d 67 void freetronicsLCDShield::setBackLight (float blIntensity) {
KKempeneers 0:01f3d38f8b6d 68 // Switch the backlight on (true) or off (false)
KKempeneers 0:01f3d38f8b6d 69 _bl = blIntensity;
KKempeneers 0:01f3d38f8b6d 70 }
KKempeneers 0:01f3d38f8b6d 71
KKempeneers 0:01f3d38f8b6d 72 void freetronicsLCDShield::setCursor (bool cStatus, bool blink) {
KKempeneers 0:01f3d38f8b6d 73 int tmp = 0;
KKempeneers 0:01f3d38f8b6d 74
KKempeneers 0:01f3d38f8b6d 75 if (blink) tmp = 0x01;
KKempeneers 0:01f3d38f8b6d 76 if (cStatus) tmp |= 0x02;
KKempeneers 0:01f3d38f8b6d 77 writeCommand(0x0C + tmp);
KKempeneers 0:01f3d38f8b6d 78 }
KKempeneers 0:01f3d38f8b6d 79
KKempeneers 1:ddcefddda4a7 80 void freetronicsLCDShield::shift(bool direction) {
KKempeneers 1:ddcefddda4a7 81 if(direction == LEFT) shiftLeft();
KKempeneers 1:ddcefddda4a7 82 else shiftRight();
KKempeneers 1:ddcefddda4a7 83 }
KKempeneers 1:ddcefddda4a7 84
KKempeneers 1:ddcefddda4a7 85 void freetronicsLCDShield::shiftLeft(void) {
KKempeneers 1:ddcefddda4a7 86 writeCommand(0x18 + 0x04);
KKempeneers 1:ddcefddda4a7 87 }
KKempeneers 1:ddcefddda4a7 88
KKempeneers 1:ddcefddda4a7 89 void freetronicsLCDShield::shiftRight(void) {
KKempeneers 1:ddcefddda4a7 90 writeCommand(0x18);
KKempeneers 0:01f3d38f8b6d 91 }
KKempeneers 0:01f3d38f8b6d 92
KKempeneers 0:01f3d38f8b6d 93 void freetronicsLCDShield::cls(void) {
KKempeneers 0:01f3d38f8b6d 94 // Clear the display and place the cursor at 0, 0
KKempeneers 0:01f3d38f8b6d 95 writeCommand(0x01);
rba90 5:7b216d7781f8 96 wait_us(1640);
KKempeneers 0:01f3d38f8b6d 97 }
KKempeneers 0:01f3d38f8b6d 98
KKempeneers 0:01f3d38f8b6d 99 void freetronicsLCDShield::home(void) {
KKempeneers 0:01f3d38f8b6d 100 // Undo shift operations and place cursor at 0,0
KKempeneers 0:01f3d38f8b6d 101 writeCommand(0x02);
rba90 5:7b216d7781f8 102 wait_us(1640);
KKempeneers 0:01f3d38f8b6d 103 }
KKempeneers 0:01f3d38f8b6d 104
KKempeneers 1:ddcefddda4a7 105 void freetronicsLCDShield::writeCGRAM (char address, const char *ptr, char nbytes) {
KKempeneers 1:ddcefddda4a7 106 // Write the address only once, it is autoincremented
KKempeneers 1:ddcefddda4a7 107 writeCommand(0x40 | address);
KKempeneers 1:ddcefddda4a7 108
KKempeneers 1:ddcefddda4a7 109 // Write the data
KKempeneers 1:ddcefddda4a7 110 for(int i = 0; i < nbytes; i++) {
KKempeneers 1:ddcefddda4a7 111 writeData(*ptr++);
KKempeneers 1:ddcefddda4a7 112 }
KKempeneers 1:ddcefddda4a7 113 }
KKempeneers 1:ddcefddda4a7 114
KKempeneers 0:01f3d38f8b6d 115 // Low level output functions
KKempeneers 0:01f3d38f8b6d 116 void freetronicsLCDShield::writeByte (int byte) {
KKempeneers 0:01f3d38f8b6d 117 // Split the byte in high and low nibble, write high nibble first
KKempeneers 0:01f3d38f8b6d 118 _d = byte >> 4;
KKempeneers 0:01f3d38f8b6d 119 PULSE_E;
KKempeneers 0:01f3d38f8b6d 120 _d = byte >> 0;
KKempeneers 0:01f3d38f8b6d 121 PULSE_E;
KKempeneers 0:01f3d38f8b6d 122 // Most instructions take 40us
rba90 5:7b216d7781f8 123 wait_us(40);
KKempeneers 0:01f3d38f8b6d 124 }
KKempeneers 0:01f3d38f8b6d 125
KKempeneers 0:01f3d38f8b6d 126 void freetronicsLCDShield::writeData (int data) {
KKempeneers 0:01f3d38f8b6d 127 _rs = true;
KKempeneers 0:01f3d38f8b6d 128 writeByte(data);
KKempeneers 0:01f3d38f8b6d 129 }
KKempeneers 0:01f3d38f8b6d 130
KKempeneers 0:01f3d38f8b6d 131 void freetronicsLCDShield::writeCommand (int command) {
KKempeneers 0:01f3d38f8b6d 132 _rs = false;
KKempeneers 0:01f3d38f8b6d 133 writeByte(command);
KKempeneers 0:01f3d38f8b6d 134 }
KKempeneers 0:01f3d38f8b6d 135
rba90 4:b4b2435f4a7f 136 float freetronicsLCDShield::readButtonAnalog(void) {
KKempeneers 0:01f3d38f8b6d 137 return(_a0.read());
KKempeneers 0:01f3d38f8b6d 138 }
KKempeneers 0:01f3d38f8b6d 139
rba90 4:b4b2435f4a7f 140 freetronicsLCDShield::ButtonType_t freetronicsLCDShield::readButton(void) {
rba90 4:b4b2435f4a7f 141 float val;
rba90 4:b4b2435f4a7f 142
rba90 4:b4b2435f4a7f 143 // read analog value
rba90 4:b4b2435f4a7f 144 val = readButtonAnalog();
rba90 4:b4b2435f4a7f 145
rba90 4:b4b2435f4a7f 146 // determine which button is pressed
rba90 4:b4b2435f4a7f 147 // analog reading for each button are centered at 0, 144, 329, 504, 741, 1000
rba90 4:b4b2435f4a7f 148
rba90 4:b4b2435f4a7f 149 // right
rba90 4:b4b2435f4a7f 150 if (val < 0.0740f)
rba90 4:b4b2435f4a7f 151 {
rba90 4:b4b2435f4a7f 152 return freetronicsLCDShield::BTN_RIGHT;
rba90 4:b4b2435f4a7f 153 }
rba90 4:b4b2435f4a7f 154
rba90 4:b4b2435f4a7f 155 // up
rba90 4:b4b2435f4a7f 156 else if (val < 0.2625f)
rba90 4:b4b2435f4a7f 157 {
rba90 4:b4b2435f4a7f 158 return freetronicsLCDShield::BTN_UP;
rba90 4:b4b2435f4a7f 159 }
rba90 4:b4b2435f4a7f 160
rba90 4:b4b2435f4a7f 161 // down
rba90 4:b4b2435f4a7f 162 else if (val < 0.4855f)
rba90 4:b4b2435f4a7f 163 {
rba90 4:b4b2435f4a7f 164 return freetronicsLCDShield::BTN_DOWN;
rba90 4:b4b2435f4a7f 165 }
rba90 4:b4b2435f4a7f 166
rba90 4:b4b2435f4a7f 167 // left
rba90 4:b4b2435f4a7f 168 else if (val < 0.6815f)
rba90 4:b4b2435f4a7f 169 {
rba90 4:b4b2435f4a7f 170 return freetronicsLCDShield::BTN_LEFT;
rba90 4:b4b2435f4a7f 171 }
rba90 4:b4b2435f4a7f 172
rba90 4:b4b2435f4a7f 173 // none
rba90 4:b4b2435f4a7f 174 else if (val < 0.8465f)
rba90 4:b4b2435f4a7f 175 {
rba90 4:b4b2435f4a7f 176 return freetronicsLCDShield::BTN_NONE;
rba90 4:b4b2435f4a7f 177 }
rba90 4:b4b2435f4a7f 178
rba90 4:b4b2435f4a7f 179 // select
rba90 4:b4b2435f4a7f 180 else
rba90 4:b4b2435f4a7f 181 {
rba90 4:b4b2435f4a7f 182 return freetronicsLCDShield::BTN_SELECT;
rba90 4:b4b2435f4a7f 183 }
rba90 4:b4b2435f4a7f 184 }
rba90 4:b4b2435f4a7f 185
KKempeneers 0:01f3d38f8b6d 186 // Virtual functions for stream
KKempeneers 0:01f3d38f8b6d 187 int freetronicsLCDShield::_putc(int value) {
KKempeneers 0:01f3d38f8b6d 188 writeData(value);
KKempeneers 0:01f3d38f8b6d 189 return value;
KKempeneers 0:01f3d38f8b6d 190 }
KKempeneers 0:01f3d38f8b6d 191
KKempeneers 0:01f3d38f8b6d 192 int freetronicsLCDShield::_getc() {
KKempeneers 0:01f3d38f8b6d 193 return -1;
KKempeneers 0:01f3d38f8b6d 194 }