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@32:0a09505a656d, 2017-11-06 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Nov 06 17:34:13 2017 -0600
- Revision:
- 32:0a09505a656d
- Parent:
- 17:41be4896ed6d
Updated to use MaximInterface library.
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 | 32:0a09505a656d | 31 | *******************************************************************************/ |
IanBenzMaxim | 1:e1c7c1c636af | 32 | |
IanBenzMaxim | 1:e1c7c1c636af | 33 | #include <sstream> |
IanBenzMaxim | 32:0a09505a656d | 34 | #include <I2C.h> |
IanBenzMaxim | 32:0a09505a656d | 35 | #include <wait_api.h> |
IanBenzMaxim | 1:e1c7c1c636af | 36 | #include "Display.hpp" |
IanBenzMaxim | 1:e1c7c1c636af | 37 | |
IanBenzMaxim | 32:0a09505a656d | 38 | // LCD Commands |
IanBenzMaxim | 32:0a09505a656d | 39 | // If the RS bit is set to logic 1, these display bytes are stored in the |
IanBenzMaxim | 32:0a09505a656d | 40 | // display RAM at the address specified by the data pointer. The data pointer is |
IanBenzMaxim | 32:0a09505a656d | 41 | // automatically updated and the data is directed to the intended ST7036i |
IanBenzMaxim | 32:0a09505a656d | 42 | // device. If the RS bit of the last control byte is set to logic 0, these |
IanBenzMaxim | 32:0a09505a656d | 43 | // command bytes will be decoded and the setting of the device will be changed |
IanBenzMaxim | 32:0a09505a656d | 44 | // according to the received commands. |
IanBenzMaxim | 32:0a09505a656d | 45 | enum LCD_Commands { |
IanBenzMaxim | 32:0a09505a656d | 46 | // Only one control byte will be sent. |
IanBenzMaxim | 32:0a09505a656d | 47 | // Only a stream of data bytes is allowed to follow. |
IanBenzMaxim | 32:0a09505a656d | 48 | ControlByte = 0x00, |
IanBenzMaxim | 32:0a09505a656d | 49 | // Only one control byte will be sent with the RS bit set. |
IanBenzMaxim | 32:0a09505a656d | 50 | // Only a stream of data bytes is allowed to follow. |
IanBenzMaxim | 32:0a09505a656d | 51 | ControlByte_RS_Set = 0x40, |
IanBenzMaxim | 32:0a09505a656d | 52 | // Another control byte will follow, unless an I2C Stop condition is received. |
IanBenzMaxim | 32:0a09505a656d | 53 | ControlBytes = 0x80, |
IanBenzMaxim | 32:0a09505a656d | 54 | // RS Set and another control byte will follow, unless an I2C Stop condition |
IanBenzMaxim | 32:0a09505a656d | 55 | // is received. |
IanBenzMaxim | 32:0a09505a656d | 56 | ControlBytes_RS_Set =0xC0 |
IanBenzMaxim | 1:e1c7c1c636af | 57 | }; |
IanBenzMaxim | 1:e1c7c1c636af | 58 | |
IanBenzMaxim | 32:0a09505a656d | 59 | // LCD Instructions |
IanBenzMaxim | 32:0a09505a656d | 60 | enum LCD_Instructions { |
IanBenzMaxim | 1:e1c7c1c636af | 61 | ClearDisplay = 0x01, |
IanBenzMaxim | 32:0a09505a656d | 62 | Display_OFF = 0x08, // Display off |
IanBenzMaxim | 32:0a09505a656d | 63 | Display_ON = 0x0C, // Display on, cursor off, cursor position off |
IanBenzMaxim | 1:e1c7c1c636af | 64 | ReturnHome = 0x02, |
IanBenzMaxim | 1:e1c7c1c636af | 65 | SetDdramAddress = 0x80 |
IanBenzMaxim | 1:e1c7c1c636af | 66 | }; |
IanBenzMaxim | 1:e1c7c1c636af | 67 | |
IanBenzMaxim | 1:e1c7c1c636af | 68 | // LED Driver Port Registers |
IanBenzMaxim | 1:e1c7c1c636af | 69 | // Initial port state 0x80 |
IanBenzMaxim | 32:0a09505a656d | 70 | enum LED_Driver_Ports { |
IanBenzMaxim | 1:e1c7c1c636af | 71 | P1 = 0x01, |
IanBenzMaxim | 32:0a09505a656d | 72 | P2 = 0x02, // Blue LED |
IanBenzMaxim | 32:0a09505a656d | 73 | P3 = 0x03, // Green LED |
IanBenzMaxim | 32:0a09505a656d | 74 | P4 = 0x04 // Red LED |
IanBenzMaxim | 1:e1c7c1c636af | 75 | }; |
IanBenzMaxim | 1:e1c7c1c636af | 76 | |
IanBenzMaxim | 1:e1c7c1c636af | 77 | // Convert a byte color value into the representation used by the MAX7306 PWM registers |
IanBenzMaxim | 32:0a09505a656d | 78 | static uint8_t convertColorToPwmRegVal(uint8_t color) { |
IanBenzMaxim | 6:b6bafd0a7013 | 79 | const uint8_t staticOffRegVal = 0x80; // LED is static off by setting to input |
IanBenzMaxim | 32:0a09505a656d | 80 | const uint8_t staticOnRegVal = 0x00; // LED is static on |
IanBenzMaxim | 32:0a09505a656d | 81 | const uint8_t minOnRegVal = 0x01; // LED on for minimum duty cycle |
IanBenzMaxim | 32:0a09505a656d | 82 | |
IanBenzMaxim | 6:b6bafd0a7013 | 83 | uint8_t regVal; |
IanBenzMaxim | 1:e1c7c1c636af | 84 | if (color == 0x00) // Use static off for no color |
IanBenzMaxim | 1:e1c7c1c636af | 85 | { |
IanBenzMaxim | 1:e1c7c1c636af | 86 | regVal = staticOffRegVal; |
IanBenzMaxim | 32:0a09505a656d | 87 | } else if (color == 0xFF) // Use static on for full color |
IanBenzMaxim | 1:e1c7c1c636af | 88 | { |
IanBenzMaxim | 1:e1c7c1c636af | 89 | regVal = staticOnRegVal; |
IanBenzMaxim | 32:0a09505a656d | 90 | } else // Use standard PWN for all other values |
IanBenzMaxim | 1:e1c7c1c636af | 91 | { |
IanBenzMaxim | 1:e1c7c1c636af | 92 | // The 3 least significant bits cannot be rendered with the MAX7306 |
IanBenzMaxim | 1:e1c7c1c636af | 93 | regVal = color >> 3; |
IanBenzMaxim | 1:e1c7c1c636af | 94 | if (regVal == staticOnRegVal) |
IanBenzMaxim | 1:e1c7c1c636af | 95 | regVal = minOnRegVal; |
IanBenzMaxim | 1:e1c7c1c636af | 96 | } |
IanBenzMaxim | 1:e1c7c1c636af | 97 | return regVal; |
IanBenzMaxim | 1:e1c7c1c636af | 98 | } |
IanBenzMaxim | 1:e1c7c1c636af | 99 | |
IanBenzMaxim | 32:0a09505a656d | 100 | Display::Display(mbed::I2C & I2C_intf, uint8_t LCD_I2C_addr, |
IanBenzMaxim | 32:0a09505a656d | 101 | uint8_t LED_driver_I2C_addr) |
IanBenzMaxim | 32:0a09505a656d | 102 | : m_I2C_intf(I2C_intf), m_LCD_I2C_addr(LCD_I2C_addr), |
IanBenzMaxim | 32:0a09505a656d | 103 | m_LED_driver_I2C_addr(LED_driver_I2C_addr) {} |
IanBenzMaxim | 1:e1c7c1c636af | 104 | |
IanBenzMaxim | 32:0a09505a656d | 105 | void Display::initialize() { |
IanBenzMaxim | 1:e1c7c1c636af | 106 | initializeLCD(); |
IanBenzMaxim | 1:e1c7c1c636af | 107 | initializeLED_Driver(); |
IanBenzMaxim | 1:e1c7c1c636af | 108 | } |
IanBenzMaxim | 1:e1c7c1c636af | 109 | |
IanBenzMaxim | 32:0a09505a656d | 110 | void Display::initializeLED_Driver() { |
IanBenzMaxim | 32:0a09505a656d | 111 | const uint8_t Configuration26 = 0x26; // Intial port state 0xEC |
IanBenzMaxim | 32:0a09505a656d | 112 | const uint8_t Configuration27 = 0x27; // Intial port state 0x8F |
IanBenzMaxim | 32:0a09505a656d | 113 | |
IanBenzMaxim | 32:0a09505a656d | 114 | // Intial mode |
IanBenzMaxim | 32:0a09505a656d | 115 | // Write to Configuration Register 0x26 |
IanBenzMaxim | 1:e1c7c1c636af | 116 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 117 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 118 | m_I2C_intf.write(Configuration26); |
IanBenzMaxim | 32:0a09505a656d | 119 | // RST resets registers to power-on-reset state |
IanBenzMaxim | 32:0a09505a656d | 120 | // RST does reset PWM/blink counters, |
IanBenzMaxim | 32:0a09505a656d | 121 | m_I2C_intf.write(0x1F); |
IanBenzMaxim | 1:e1c7c1c636af | 122 | m_I2C_intf.stop(); |
IanBenzMaxim | 32:0a09505a656d | 123 | |
IanBenzMaxim | 32:0a09505a656d | 124 | // Write to Configuration Register 0x27 |
IanBenzMaxim | 1:e1c7c1c636af | 125 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 126 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 127 | m_I2C_intf.write(Configuration27); |
IanBenzMaxim | 32:0a09505a656d | 128 | // Enable bus time out, and set P1, P2, P3 to be controlled by their registers |
IanBenzMaxim | 32:0a09505a656d | 129 | // (0x01, 0x02, 0x03) |
IanBenzMaxim | 32:0a09505a656d | 130 | m_I2C_intf.write(0x0E); |
IanBenzMaxim | 1:e1c7c1c636af | 131 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 132 | } |
IanBenzMaxim | 1:e1c7c1c636af | 133 | |
IanBenzMaxim | 32:0a09505a656d | 134 | void Display::setBackLightColor(const Color & color) { |
IanBenzMaxim | 1:e1c7c1c636af | 135 | // Red |
IanBenzMaxim | 1:e1c7c1c636af | 136 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 137 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 138 | m_I2C_intf.write(P4); |
IanBenzMaxim | 32:0a09505a656d | 139 | m_I2C_intf.write(convertColorToPwmRegVal(color.R)); |
IanBenzMaxim | 1:e1c7c1c636af | 140 | m_I2C_intf.stop(); |
IanBenzMaxim | 32:0a09505a656d | 141 | |
IanBenzMaxim | 1:e1c7c1c636af | 142 | // Green |
IanBenzMaxim | 1:e1c7c1c636af | 143 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 144 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 145 | m_I2C_intf.write(P3); |
IanBenzMaxim | 32:0a09505a656d | 146 | m_I2C_intf.write(convertColorToPwmRegVal(color.G)); |
IanBenzMaxim | 1:e1c7c1c636af | 147 | m_I2C_intf.stop(); |
IanBenzMaxim | 32:0a09505a656d | 148 | |
IanBenzMaxim | 1:e1c7c1c636af | 149 | // Blue |
IanBenzMaxim | 1:e1c7c1c636af | 150 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 151 | m_I2C_intf.write(m_LED_driver_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 152 | m_I2C_intf.write(P2); |
IanBenzMaxim | 32:0a09505a656d | 153 | m_I2C_intf.write(convertColorToPwmRegVal(color.B)); |
IanBenzMaxim | 1:e1c7c1c636af | 154 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 155 | } |
IanBenzMaxim | 1:e1c7c1c636af | 156 | |
IanBenzMaxim | 32:0a09505a656d | 157 | void Display::clearLine(Line line) { |
IanBenzMaxim | 1:e1c7c1c636af | 158 | writeCompleteLine("", line); |
IanBenzMaxim | 1:e1c7c1c636af | 159 | setCursorPosition(line); |
IanBenzMaxim | 1:e1c7c1c636af | 160 | } |
IanBenzMaxim | 1:e1c7c1c636af | 161 | |
IanBenzMaxim | 32:0a09505a656d | 162 | void Display::clearDisplay() { |
IanBenzMaxim | 1:e1c7c1c636af | 163 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 164 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 32:0a09505a656d | 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 | 32:0a09505a656d | 170 | void Display::initializeLCD() { |
IanBenzMaxim | 1:e1c7c1c636af | 171 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 172 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 32:0a09505a656d | 173 | m_I2C_intf.write(ControlByte); // No more control bytes will be sent |
IanBenzMaxim | 32:0a09505a656d | 174 | // Function Set IS[2:1] = 0,0 (&h38 = Single height font, 0x3C = double height font) |
IanBenzMaxim | 32:0a09505a656d | 175 | m_I2C_intf.write(0x38); |
IanBenzMaxim | 32:0a09505a656d | 176 | m_I2C_intf.write(0x39); //Function Set IS[2:1] = (0,1) |
IanBenzMaxim | 32:0a09505a656d | 177 | // When IS[2:1]=(0,0): normal instruction be selected(refer instruction table 0) |
IanBenzMaxim | 32:0a09505a656d | 178 | // When IS[2:1]=(0,1): extension instruction be selected(refer instruction table 1) |
IanBenzMaxim | 32:0a09505a656d | 179 | // When IS[2:1]=(1,0): extension instruction be selected(refer instruction table 2) |
IanBenzMaxim | 32:0a09505a656d | 180 | m_I2C_intf.write(0x14); // BIAS SET |
IanBenzMaxim | 32:0a09505a656d | 181 | m_I2C_intf.write(0x70); // CONTRAST (was 0x78) |
IanBenzMaxim | 32:0a09505a656d | 182 | m_I2C_intf.write(0x5E); // POWER/ICON CONTROL/CONTRAST (upper two bits) |
IanBenzMaxim | 32:0a09505a656d | 183 | m_I2C_intf.write(0x6D); // FOLLOWER CONTROL |
IanBenzMaxim | 17:41be4896ed6d | 184 | m_I2C_intf.stop(); |
IanBenzMaxim | 32:0a09505a656d | 185 | wait_ms(200); // Wait for power stable |
IanBenzMaxim | 17:41be4896ed6d | 186 | m_I2C_intf.start(); |
IanBenzMaxim | 17:41be4896ed6d | 187 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 32:0a09505a656d | 188 | m_I2C_intf.write(ControlByte); // No more control bytes will be sent |
IanBenzMaxim | 32:0a09505a656d | 189 | m_I2C_intf.write(Display_ON); // Display on, cursor on, cursor position on |
IanBenzMaxim | 32:0a09505a656d | 190 | m_I2C_intf.write(ClearDisplay); // Clear Display |
IanBenzMaxim | 32:0a09505a656d | 191 | m_I2C_intf.write(0x06); // ENTRY MODE |
IanBenzMaxim | 1:e1c7c1c636af | 192 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 193 | } |
IanBenzMaxim | 1:e1c7c1c636af | 194 | |
IanBenzMaxim | 32:0a09505a656d | 195 | void Display::writeCharacter(uint8_t character) { |
IanBenzMaxim | 1:e1c7c1c636af | 196 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 197 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 32:0a09505a656d | 198 | m_I2C_intf.write(ControlByte_RS_Set); // No more control bytes will be sent |
IanBenzMaxim | 32:0a09505a656d | 199 | m_I2C_intf.write(character); // Display on, cursor on, cursor position on |
IanBenzMaxim | 32:0a09505a656d | 200 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 201 | } |
IanBenzMaxim | 1:e1c7c1c636af | 202 | |
IanBenzMaxim | 32:0a09505a656d | 203 | void Display::writeText(const std::string & text) { |
IanBenzMaxim | 1:e1c7c1c636af | 204 | const char RETURN_CHAR = 0x16; |
IanBenzMaxim | 32:0a09505a656d | 205 | |
IanBenzMaxim | 6:b6bafd0a7013 | 206 | size_t length = text.length(); |
IanBenzMaxim | 1:e1c7c1c636af | 207 | if (length > lineLength) |
IanBenzMaxim | 1:e1c7c1c636af | 208 | length = lineLength; |
IanBenzMaxim | 32:0a09505a656d | 209 | |
IanBenzMaxim | 1:e1c7c1c636af | 210 | //Write to LCD |
IanBenzMaxim | 1:e1c7c1c636af | 211 | m_I2C_intf.start(); |
IanBenzMaxim | 1:e1c7c1c636af | 212 | m_I2C_intf.write(m_LCD_I2C_addr); |
IanBenzMaxim | 1:e1c7c1c636af | 213 | m_I2C_intf.write(ControlByte_RS_Set); |
IanBenzMaxim | 32:0a09505a656d | 214 | |
IanBenzMaxim | 32:0a09505a656d | 215 | for (size_t i = 0; i < length; i++) { |
IanBenzMaxim | 32:0a09505a656d | 216 | if (text[i] != RETURN_CHAR) |
IanBenzMaxim | 1:e1c7c1c636af | 217 | m_I2C_intf.write(text[i]); |
IanBenzMaxim | 1:e1c7c1c636af | 218 | } |
IanBenzMaxim | 32:0a09505a656d | 219 | |
IanBenzMaxim | 1:e1c7c1c636af | 220 | m_I2C_intf.stop(); |
IanBenzMaxim | 1:e1c7c1c636af | 221 | } |
IanBenzMaxim | 1:e1c7c1c636af | 222 | |
IanBenzMaxim | 32:0a09505a656d | 223 | void Display::setCursorPosition(Line line, size_t position) { |
IanBenzMaxim | 32:0a09505a656d | 224 | // Set to last line character for values outside the upper bound |
IanBenzMaxim | 32:0a09505a656d | 225 | if (position > (lineLength - 1)) |
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 | 32:0a09505a656d | 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 | 32:0a09505a656d | 237 | void Display::writeLine(const std::string & text, Line line) { |
IanBenzMaxim | 1:e1c7c1c636af | 238 | setCursorPosition(line); |
IanBenzMaxim | 1:e1c7c1c636af | 239 | writeText(text); |
IanBenzMaxim | 1:e1c7c1c636af | 240 | } |
IanBenzMaxim | 1:e1c7c1c636af | 241 | |
IanBenzMaxim | 32:0a09505a656d | 242 | void Display::writeCompleteLine(const std::string & text, Line line) { |
IanBenzMaxim | 1:e1c7c1c636af | 243 | // Add padding to user's string |
IanBenzMaxim | 1:e1c7c1c636af | 244 | std::string writeText(text); |
IanBenzMaxim | 1:e1c7c1c636af | 245 | if (writeText.length() < lineLength) |
IanBenzMaxim | 1:e1c7c1c636af | 246 | writeText.append(lineLength - writeText.length(), ' '); |
IanBenzMaxim | 32:0a09505a656d | 247 | |
IanBenzMaxim | 1:e1c7c1c636af | 248 | writeLine(writeText, line); |
IanBenzMaxim | 1:e1c7c1c636af | 249 | } |
IanBenzMaxim | 1:e1c7c1c636af | 250 | |
IanBenzMaxim | 32:0a09505a656d | 251 | void Display::writeMessage(const std::string & message) { |
IanBenzMaxim | 32:0a09505a656d | 252 | if (message.length() > lineLength) { |
IanBenzMaxim | 1:e1c7c1c636af | 253 | // Find split point |
IanBenzMaxim | 1:e1c7c1c636af | 254 | std::istringstream messageStream(message); |
IanBenzMaxim | 1:e1c7c1c636af | 255 | std::string word; |
IanBenzMaxim | 6:b6bafd0a7013 | 256 | size_t splitIndex = 0; |
IanBenzMaxim | 32:0a09505a656d | 257 | do { |
IanBenzMaxim | 1:e1c7c1c636af | 258 | if (word.length() > 0) |
IanBenzMaxim | 1:e1c7c1c636af | 259 | splitIndex += (word.length() + 1); |
IanBenzMaxim | 1:e1c7c1c636af | 260 | std::getline(messageStream, word, ' '); |
IanBenzMaxim | 1:e1c7c1c636af | 261 | } while ((splitIndex + word.length()) <= lineLength); |
IanBenzMaxim | 1:e1c7c1c636af | 262 | if (splitIndex == 0) // First word is too long |
IanBenzMaxim | 1:e1c7c1c636af | 263 | { |
IanBenzMaxim | 1:e1c7c1c636af | 264 | writeCompleteLine(message.substr(0, lineLength), FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 265 | writeCompleteLine(message.substr(lineLength), SecondLine); |
IanBenzMaxim | 32:0a09505a656d | 266 | } else { |
IanBenzMaxim | 1:e1c7c1c636af | 267 | writeCompleteLine(message.substr(0, splitIndex - 1), FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 268 | writeCompleteLine(message.substr(splitIndex), SecondLine); |
IanBenzMaxim | 1:e1c7c1c636af | 269 | } |
IanBenzMaxim | 32:0a09505a656d | 270 | } else { |
IanBenzMaxim | 1:e1c7c1c636af | 271 | writeCompleteLine(message, FirstLine); |
IanBenzMaxim | 1:e1c7c1c636af | 272 | writeCompleteLine("", SecondLine); |
IanBenzMaxim | 1:e1c7c1c636af | 273 | } |
IanBenzMaxim | 1:e1c7c1c636af | 274 | } |