MAXREFDES143#: DeepCover Embedded Security in IoT Authenticated Sensing & Notification
Dependencies: MaximInterface mbed
The MAXREFDES143# is an Internet of Things (IoT) embedded security reference design, built to protect an industrial sensing node by means of authentication and notification to a web server. The hardware includes a peripheral module representing a protected sensor node monitoring operating temperature and remaining life of a filter (simulated through ambient light sensing) and an mbed shield representing a controller node responsible for monitoring one or more sensor nodes. The design is hierarchical with each controller node communicating data from connected sensor nodes to a web server that maintains a centralized log and dispatches notifications as necessary. The mbed shield contains a Wi-Fi module, a DS2465 coprocessor with 1-Wire® master function, an LCD, LEDs, and pushbuttons. The protected sensor node contains a DS28E15 authenticator, a DS7505 temperature sensor, and a MAX44009 light sensor. The mbed shield communicates to a web server by the onboard Wi-Fi module and to the protected sensor node with I2C and 1-Wire. The MAXREFDES143# is equipped with a standard shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The simplicity of this design enables rapid integration into any star-topology IoT network requiring the heightened security with low overhead provided by the SHA-256 symmetric-key algorithm.
More information about the MAXREFDES143# is available on the Maxim Integrated website.
Display.cpp@1:e1c7c1c636af, 2016-04-14 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Thu Apr 14 19:48:01 2016 +0000
- Revision:
- 1:e1c7c1c636af
- Child:
- 6:b6bafd0a7013
Add initial source files.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 1:e1c7c1c636af | 1 | /******************************************************************************* |
IanBenzMaxim | 1:e1c7c1c636af | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 1:e1c7c1c636af | 3 | * |
IanBenzMaxim | 1:e1c7c1c636af | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 1:e1c7c1c636af | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 1:e1c7c1c636af | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 1:e1c7c1c636af | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 1:e1c7c1c636af | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 1:e1c7c1c636af | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 1:e1c7c1c636af | 10 | * |
IanBenzMaxim | 1:e1c7c1c636af | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 1:e1c7c1c636af | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 1:e1c7c1c636af | 13 | * |
IanBenzMaxim | 1:e1c7c1c636af | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 1:e1c7c1c636af | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 1:e1c7c1c636af | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 1:e1c7c1c636af | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 1:e1c7c1c636af | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 1:e1c7c1c636af | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 1:e1c7c1c636af | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 1:e1c7c1c636af | 21 | * |
IanBenzMaxim | 1:e1c7c1c636af | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 1:e1c7c1c636af | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 1:e1c7c1c636af | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 1:e1c7c1c636af | 25 | * |
IanBenzMaxim | 1:e1c7c1c636af | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 1:e1c7c1c636af | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 1:e1c7c1c636af | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 1:e1c7c1c636af | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 1:e1c7c1c636af | 30 | * ownership rights. |
IanBenzMaxim | 1:e1c7c1c636af | 31 | ******************************************************************************* |
IanBenzMaxim | 1:e1c7c1c636af | 32 | */ |
IanBenzMaxim | 1:e1c7c1c636af | 33 | |
IanBenzMaxim | 1:e1c7c1c636af | 34 | #include <sstream> |
IanBenzMaxim | 1:e1c7c1c636af | 35 | #include "Display.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 36 | #include "mbed.h" |
IanBenzMaxim | 1:e1c7c1c636af | 37 | |
IanBenzMaxim | 1:e1c7c1c636af | 38 | //LCD Commands |
IanBenzMaxim | 1:e1c7c1c636af | 39 | //If the RS bit is set to logic 1, these display bytes are stored in the display RAM at the address specified by the data pointer. The data pointer is |
IanBenzMaxim | 1:e1c7c1c636af | 40 | //automatically updated and the data is directed to the intended ST7036i device. If the RS bit of the last control byte is set to |
IanBenzMaxim | 1:e1c7c1c636af | 41 | //logic 0, these command bytes will be decoded and the setting of the device will be changed according to the received commands. |
IanBenzMaxim | 1:e1c7c1c636af | 42 | enum LCD_Commands |
IanBenzMaxim | 1:e1c7c1c636af | 43 | { |
IanBenzMaxim | 1:e1c7c1c636af | 44 | ControlByte = 0x00, //Only one control byte will be sent. Only a stream of data bytes is allowed to follow. |
IanBenzMaxim | 1:e1c7c1c636af | 45 | ControlByte_RS_Set = 0x40, //Only one control byte will be sent with the RS bit set. Only a stream of data bytes is allowed to follow. |
IanBenzMaxim | 1:e1c7c1c636af | 46 | ControlBytes = 0x80, //Another control byte will follow, unless an I2C Stop condition is received. |
IanBenzMaxim | 1:e1c7c1c636af | 47 | ControlBytes_RS_Set = 0xC0, //RS Set and another control byte will follow, unless an I2C Stop condition is received. |
IanBenzMaxim | 1:e1c7c1c636af | 48 | }; |
IanBenzMaxim | 1:e1c7c1c636af | 49 | |
IanBenzMaxim | 1:e1c7c1c636af | 50 | //LCD Instructions |
IanBenzMaxim | 1:e1c7c1c636af | 51 | enum LCD_Instructions |
IanBenzMaxim | 1:e1c7c1c636af | 52 | { |
IanBenzMaxim | 1:e1c7c1c636af | 53 | ClearDisplay = 0x01, |
IanBenzMaxim | 1:e1c7c1c636af | 54 | Display_OFF = 0x08, //Display off |
IanBenzMaxim | 1:e1c7c1c636af | 55 | Display_ON = 0x0C, //Display on, cursor off, cursor position off |
IanBenzMaxim | 1:e1c7c1c636af | 56 | ReturnHome = 0x02, |
IanBenzMaxim | 1:e1c7c1c636af | 57 | SetDdramAddress = 0x80 |
IanBenzMaxim | 1:e1c7c1c636af | 58 | }; |
IanBenzMaxim | 1:e1c7c1c636af | 59 | |
IanBenzMaxim | 1:e1c7c1c636af | 60 | // LED Driver Port Registers |
IanBenzMaxim | 1:e1c7c1c636af | 61 | // Initial port state 0x80 |
IanBenzMaxim | 1:e1c7c1c636af | 62 | enum LED_Driver_Ports |
IanBenzMaxim | 1:e1c7c1c636af | 63 | { |
IanBenzMaxim | 1:e1c7c1c636af | 64 | P1 = 0x01, |
IanBenzMaxim | 1:e1c7c1c636af | 65 | P2 = 0x02, // Blue LED |
IanBenzMaxim | 1:e1c7c1c636af | 66 | P3 = 0x03, // Green LED |
IanBenzMaxim | 1:e1c7c1c636af | 67 | P4 = 0x04 // Red LED |
IanBenzMaxim | 1:e1c7c1c636af | 68 | }; |
IanBenzMaxim | 1:e1c7c1c636af | 69 | |
IanBenzMaxim | 1:e1c7c1c636af | 70 | // Convert a byte color value into the representation used by the MAX7306 PWM registers |
IanBenzMaxim | 1:e1c7c1c636af | 71 | static std::uint8_t convertColorToPwmRegVal(std::uint8_t color) |
IanBenzMaxim | 1:e1c7c1c636af | 72 | { |
IanBenzMaxim | 1:e1c7c1c636af | 73 | const std::uint8_t staticOffRegVal = 0x80; // LED is static off by setting to input |
IanBenzMaxim | 1:e1c7c1c636af | 74 | const std::uint8_t staticOnRegVal = 0x00; // LED is static on |
IanBenzMaxim | 1:e1c7c1c636af | 75 | const std::uint8_t minOnRegVal = 0x01; // LED on for minimum duty cycle |
IanBenzMaxim | 1:e1c7c1c636af | 76 | |
IanBenzMaxim | 1:e1c7c1c636af | 77 | std::uint8_t regVal; |
IanBenzMaxim | 1:e1c7c1c636af | 78 | if (color == 0x00) // Use static off for no color |
IanBenzMaxim | 1:e1c7c1c636af | 79 | { |
IanBenzMaxim | 1:e1c7c1c636af | 80 | regVal = staticOffRegVal; |
IanBenzMaxim | 1:e1c7c1c636af | 81 | } |
IanBenzMaxim | 1:e1c7c1c636af | 82 | else if (color == 0xFF) // Use static on for full color |
IanBenzMaxim | 1:e1c7c1c636af | 83 | { |
IanBenzMaxim | 1:e1c7c1c636af | 84 | regVal = staticOnRegVal; |
IanBenzMaxim | 1:e1c7c1c636af | 85 | } |
IanBenzMaxim | 1:e1c7c1c636af | 86 | else // Use standard PWN for all other values |
IanBenzMaxim | 1:e1c7c1c636af | 87 | { |
IanBenzMaxim | 1:e1c7c1c636af | 88 | // The 3 least significant bits cannot be rendered with the MAX7306 |
IanBenzMaxim | 1:e1c7c1c636af | 89 | regVal = color >> 3; |
IanBenzMaxim | 1:e1c7c1c636af | 90 | if (regVal == staticOnRegVal) |
IanBenzMaxim | 1:e1c7c1c636af | 91 | regVal = minOnRegVal; |
IanBenzMaxim | 1:e1c7c1c636af | 92 | } |
IanBenzMaxim | 1:e1c7c1c636af | 93 | return regVal; |
IanBenzMaxim | 1:e1c7c1c636af | 94 | } |
IanBenzMaxim | 1:e1c7c1c636af | 95 | |
IanBenzMaxim | 1:e1c7c1c636af | 96 | Display::Display(I2C & I2C_intf, uint8_t LCD_I2C_addr, uint8_t LED_driver_I2C_addr) |
IanBenzMaxim | 1:e1c7c1c636af | 97 | : m_I2C_intf(I2C_intf), m_LCD_I2C_addr(LCD_I2C_addr), m_LED_driver_I2C_addr(LED_driver_I2C_addr) |
IanBenzMaxim | 1:e1c7c1c636af | 98 | { |
IanBenzMaxim | 1:e1c7c1c636af | 99 | |
IanBenzMaxim | 1:e1c7c1c636af | 100 | } |
IanBenzMaxim | 1:e1c7c1c636af | 101 | |
IanBenzMaxim | 1:e1c7c1c636af | 102 | void Display::initialize(void) |
IanBenzMaxim | 1:e1c7c1c636af | 103 | { |
IanBenzMaxim | 1:e1c7c1c636af | 104 | initializeLCD(); |
IanBenzMaxim | 1:e1c7c1c636af | 105 | initializeLED_Driver(); |
IanBenzMaxim | 1:e1c7c1c636af | 106 | } |
IanBenzMaxim | 1:e1c7c1c636af | 107 | |
IanBenzMaxim | 1:e1c7c1c636af | 108 | void Display::initializeLED_Driver(void) |
IanBenzMaxim | 1:e1c7c1c636af | 109 | { |
IanBenzMaxim | 1:e1c7c1c636af | 110 | const std::uint8_t Configuration26 = 0x26; //intial port state 0xEC |
IanBenzMaxim | 1:e1c7c1c636af | 111 | const std::uint8_t Configuration27 = 0x27; //intial port state 0x8F |
IanBenzMaxim | 1:e1c7c1c636af | 112 | |
IanBenzMaxim | 1:e1c7c1c636af | 113 | //Intial mode |
IanBenzMaxim | 1:e1c7c1c636af | 114 | //write to Configuration Register 0x26 |
IanBenzMaxim | 1:e1c7c1c636af | 115 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 116 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 117 | m_I2C_intf.write(Configuration26); |
IanBenzMaxim | 1:e1c7c1c636af | 118 | //RST does reset PWM/blink counters, RST resets registers to power-on-reset state |
IanBenzMaxim | 1:e1c7c1c636af | 119 | m_I2C_intf.write(0x1F); |
IanBenzMaxim | 1:e1c7c1c636af | 120 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 121 | |
IanBenzMaxim | 1:e1c7c1c636af | 122 | //Write to Configuration Register 0x27 |
IanBenzMaxim | 1:e1c7c1c636af | 123 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 124 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 125 | m_I2C_intf.write(Configuration27); |
IanBenzMaxim | 1:e1c7c1c636af | 126 | //Enable bus time out, set P1,P2,P3 to be controlled by their registers (0x01,0x02,0x03) |
IanBenzMaxim | 1:e1c7c1c636af | 127 | m_I2C_intf.write(0x0E); |
IanBenzMaxim | 1:e1c7c1c636af | 128 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 129 | } |
IanBenzMaxim | 1:e1c7c1c636af | 130 | |
IanBenzMaxim | 1:e1c7c1c636af | 131 | void Display::setBackLightColor(const Color & color) |
IanBenzMaxim | 1:e1c7c1c636af | 132 | { |
IanBenzMaxim | 1:e1c7c1c636af | 133 | // Red |
IanBenzMaxim | 1:e1c7c1c636af | 134 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 135 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 136 | m_I2C_intf.write(P4); |
IanBenzMaxim | 1:e1c7c1c636af | 137 | m_I2C_intf.write(convertColorToPwmRegVal(color.R)); |
IanBenzMaxim | 1:e1c7c1c636af | 138 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 139 | |
IanBenzMaxim | 1:e1c7c1c636af | 140 | // Green |
IanBenzMaxim | 1:e1c7c1c636af | 141 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 142 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 143 | m_I2C_intf.write(P3); |
IanBenzMaxim | 1:e1c7c1c636af | 144 | m_I2C_intf.write(convertColorToPwmRegVal(color.G)); |
IanBenzMaxim | 1:e1c7c1c636af | 145 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 146 | |
IanBenzMaxim | 1:e1c7c1c636af | 147 | // Blue |
IanBenzMaxim | 1:e1c7c1c636af | 148 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 149 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 150 | m_I2C_intf.write(P2); |
IanBenzMaxim | 1:e1c7c1c636af | 151 | m_I2C_intf.write(convertColorToPwmRegVal(color.B)); |
IanBenzMaxim | 1:e1c7c1c636af | 152 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 153 | } |
IanBenzMaxim | 1:e1c7c1c636af | 154 | |
IanBenzMaxim | 1:e1c7c1c636af | 155 | void Display::clearLine(Line line) |
IanBenzMaxim | 1:e1c7c1c636af | 156 | { |
IanBenzMaxim | 1:e1c7c1c636af | 157 | writeCompleteLine("", line); |
IanBenzMaxim | 1:e1c7c1c636af | 158 | setCursorPosition(line); |
IanBenzMaxim | 1:e1c7c1c636af | 159 | } |
IanBenzMaxim | 1:e1c7c1c636af | 160 | |
IanBenzMaxim | 1:e1c7c1c636af | 161 | void Display::clearDisplay(void) |
IanBenzMaxim | 1:e1c7c1c636af | 162 | { |
IanBenzMaxim | 1:e1c7c1c636af | 163 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 164 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 165 | m_I2C_intf.write(ControlByte); //No more control bytes will be sent |
IanBenzMaxim | 1:e1c7c1c636af | 166 | m_I2C_intf.write(ClearDisplay); |
IanBenzMaxim | 1:e1c7c1c636af | 167 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 168 | } |
IanBenzMaxim | 1:e1c7c1c636af | 169 | |
IanBenzMaxim | 1:e1c7c1c636af | 170 | void Display::initializeLCD(void) |
IanBenzMaxim | 1:e1c7c1c636af | 171 | { |
IanBenzMaxim | 1:e1c7c1c636af | 172 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 173 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 174 | m_I2C_intf.write(ControlByte); //No more control bytes will be sent |
IanBenzMaxim | 1:e1c7c1c636af | 175 | //************************************************************************************ |
IanBenzMaxim | 1:e1c7c1c636af | 176 | m_I2C_intf.write(0x38); //Function Set IS[2:1] = 0,0 (&h38 = Single height font, 0x3C = double height font) |
IanBenzMaxim | 1:e1c7c1c636af | 177 | m_I2C_intf.write(0x39); //Function Set IS[2:1] = (0,1) |
IanBenzMaxim | 1:e1c7c1c636af | 178 | //When IS[2:1]=(0,0): normal instruction be selected(refer instruction table 0) |
IanBenzMaxim | 1:e1c7c1c636af | 179 | //When IS[2:1]=(0,1): extension instruction be selected(refer instruction table 1 ) |
IanBenzMaxim | 1:e1c7c1c636af | 180 | //When IS[2:1]=(1,0): extension instruction be selected(refer instruction table 2 ) |
IanBenzMaxim | 1:e1c7c1c636af | 181 | m_I2C_intf.write(0x14); //BIAS SET |
IanBenzMaxim | 1:e1c7c1c636af | 182 | m_I2C_intf.write(0x70); //CONTRAST (was 0x78) |
IanBenzMaxim | 1:e1c7c1c636af | 183 | m_I2C_intf.write(0x5E); //POWER/ICON CONTROL/CONTRAST (upper two bits) |
IanBenzMaxim | 1:e1c7c1c636af | 184 | m_I2C_intf.write(0x6D); //FOLLOWER CONTROL |
IanBenzMaxim | 1:e1c7c1c636af | 185 | m_I2C_intf.write(Display_ON); //Display on, cursor on, cursor position on |
IanBenzMaxim | 1:e1c7c1c636af | 186 | m_I2C_intf.write(ClearDisplay); //Clear Display |
IanBenzMaxim | 1:e1c7c1c636af | 187 | m_I2C_intf.write(0x06); //ENTRY MODE |
IanBenzMaxim | 1:e1c7c1c636af | 188 | //************************************************************************************ |
IanBenzMaxim | 1:e1c7c1c636af | 189 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 190 | } |
IanBenzMaxim | 1:e1c7c1c636af | 191 | |
IanBenzMaxim | 1:e1c7c1c636af | 192 | void Display::writeCharacter(std::uint8_t character) |
IanBenzMaxim | 1:e1c7c1c636af | 193 | { |
IanBenzMaxim | 1:e1c7c1c636af | 194 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 195 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 196 | m_I2C_intf.write(ControlByte_RS_Set); //No more control bytes will be sent |
IanBenzMaxim | 1:e1c7c1c636af | 197 | m_I2C_intf.write(character); //Display on, cursor on, cursor position on |
IanBenzMaxim | 1:e1c7c1c636af | 198 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 199 | } |
IanBenzMaxim | 1:e1c7c1c636af | 200 | |
IanBenzMaxim | 1:e1c7c1c636af | 201 | void Display::writeText(const std::string & text) |
IanBenzMaxim | 1:e1c7c1c636af | 202 | { |
IanBenzMaxim | 1:e1c7c1c636af | 203 | const char RETURN_CHAR = 0x16; |
IanBenzMaxim | 1:e1c7c1c636af | 204 | |
IanBenzMaxim | 1:e1c7c1c636af | 205 | std::size_t length = text.length(); |
IanBenzMaxim | 1:e1c7c1c636af | 206 | if (length > lineLength) |
IanBenzMaxim | 1:e1c7c1c636af | 207 | length = lineLength; |
IanBenzMaxim | 1:e1c7c1c636af | 208 | |
IanBenzMaxim | 1:e1c7c1c636af | 209 | //Write to LCD |
IanBenzMaxim | 1:e1c7c1c636af | 210 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 211 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 212 | m_I2C_intf.write(ControlByte_RS_Set); |
IanBenzMaxim | 1:e1c7c1c636af | 213 | |
IanBenzMaxim | 1:e1c7c1c636af | 214 | for(std::size_t i = 0; i < length; i++) |
IanBenzMaxim | 1:e1c7c1c636af | 215 | { |
IanBenzMaxim | 1:e1c7c1c636af | 216 | if(text[i] != RETURN_CHAR) |
IanBenzMaxim | 1:e1c7c1c636af | 217 | m_I2C_intf.write(text[i]); |
IanBenzMaxim | 1:e1c7c1c636af | 218 | } |
IanBenzMaxim | 1:e1c7c1c636af | 219 | |
IanBenzMaxim | 1:e1c7c1c636af | 220 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 221 | } |
IanBenzMaxim | 1:e1c7c1c636af | 222 | |
IanBenzMaxim | 1:e1c7c1c636af | 223 | void Display::setCursorPosition(Line line, std::size_t position) |
IanBenzMaxim | 1:e1c7c1c636af | 224 | { |
IanBenzMaxim | 1:e1c7c1c636af | 225 | if (position > (lineLength - 1)) // Set to last line character for values outside the upper bound |
IanBenzMaxim | 1:e1c7c1c636af | 226 | position = (lineLength - 1); |
IanBenzMaxim | 1:e1c7c1c636af | 227 | |
IanBenzMaxim | 1:e1c7c1c636af | 228 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 229 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 230 | m_I2C_intf.write(ControlByte); // No more control bytes will be sent |
IanBenzMaxim | 1:e1c7c1c636af | 231 | if(line == SecondLine) // Offset for second line |
IanBenzMaxim | 1:e1c7c1c636af | 232 | position += 0x40; |
IanBenzMaxim | 1:e1c7c1c636af | 233 | m_I2C_intf.write(SetDdramAddress | position); |
IanBenzMaxim | 1:e1c7c1c636af | 234 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 235 | } |
IanBenzMaxim | 1:e1c7c1c636af | 236 | |
IanBenzMaxim | 1:e1c7c1c636af | 237 | void Display::writeLine(const std::string & text, Line line) |
IanBenzMaxim | 1:e1c7c1c636af | 238 | { |
IanBenzMaxim | 1:e1c7c1c636af | 239 | setCursorPosition(line); |
IanBenzMaxim | 1:e1c7c1c636af | 240 | writeText(text); |
IanBenzMaxim | 1:e1c7c1c636af | 241 | } |
IanBenzMaxim | 1:e1c7c1c636af | 242 | |
IanBenzMaxim | 1:e1c7c1c636af | 243 | void Display::writeCompleteLine(const std::string & text, Line line) |
IanBenzMaxim | 1:e1c7c1c636af | 244 | { |
IanBenzMaxim | 1:e1c7c1c636af | 245 | // Add padding to user's string |
IanBenzMaxim | 1:e1c7c1c636af | 246 | std::string writeText(text); |
IanBenzMaxim | 1:e1c7c1c636af | 247 | if (writeText.length() < lineLength) |
IanBenzMaxim | 1:e1c7c1c636af | 248 | writeText.append(lineLength - writeText.length(), ' '); |
IanBenzMaxim | 1:e1c7c1c636af | 249 | |
IanBenzMaxim | 1:e1c7c1c636af | 250 | writeLine(writeText, line); |
IanBenzMaxim | 1:e1c7c1c636af | 251 | } |
IanBenzMaxim | 1:e1c7c1c636af | 252 | |
IanBenzMaxim | 1:e1c7c1c636af | 253 | void Display::writeMessage(const std::string & message) |
IanBenzMaxim | 1:e1c7c1c636af | 254 | { |
IanBenzMaxim | 1:e1c7c1c636af | 255 | if (message.length() > lineLength) |
IanBenzMaxim | 1:e1c7c1c636af | 256 | { |
IanBenzMaxim | 1:e1c7c1c636af | 257 | // Find split point |
IanBenzMaxim | 1:e1c7c1c636af | 258 | std::istringstream messageStream(message); |
IanBenzMaxim | 1:e1c7c1c636af | 259 | std::string word; |
IanBenzMaxim | 1:e1c7c1c636af | 260 | std::size_t splitIndex = 0; |
IanBenzMaxim | 1:e1c7c1c636af | 261 | do |
IanBenzMaxim | 1:e1c7c1c636af | 262 | { |
IanBenzMaxim | 1:e1c7c1c636af | 263 | if (word.length() > 0) |
IanBenzMaxim | 1:e1c7c1c636af | 264 | splitIndex += (word.length() + 1); |
IanBenzMaxim | 1:e1c7c1c636af | 265 | std::getline(messageStream, word, ' '); |
IanBenzMaxim | 1:e1c7c1c636af | 266 | } while ((splitIndex + word.length()) <= lineLength); |
IanBenzMaxim | 1:e1c7c1c636af | 267 | if (splitIndex == 0) // First word is too long |
IanBenzMaxim | 1:e1c7c1c636af | 268 | { |
IanBenzMaxim | 1:e1c7c1c636af | 269 | writeCompleteLine(message.substr(0, lineLength), FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 270 | writeCompleteLine(message.substr(lineLength), SecondLine); |
IanBenzMaxim | 1:e1c7c1c636af | 271 | } |
IanBenzMaxim | 1:e1c7c1c636af | 272 | else |
IanBenzMaxim | 1:e1c7c1c636af | 273 | { |
IanBenzMaxim | 1:e1c7c1c636af | 274 | writeCompleteLine(message.substr(0, splitIndex - 1), FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 275 | writeCompleteLine(message.substr(splitIndex), SecondLine); |
IanBenzMaxim | 1:e1c7c1c636af | 276 | } |
IanBenzMaxim | 1:e1c7c1c636af | 277 | } |
IanBenzMaxim | 1:e1c7c1c636af | 278 | else |
IanBenzMaxim | 1:e1c7c1c636af | 279 | { |
IanBenzMaxim | 1:e1c7c1c636af | 280 | writeCompleteLine(message, FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 281 | writeCompleteLine("", SecondLine); |
IanBenzMaxim | 1:e1c7c1c636af | 282 | } |
IanBenzMaxim | 1:e1c7c1c636af | 283 | } |