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.

Committer:
IanBenzMaxim
Date:
Thu Jul 21 11:06:13 2016 -0500
Revision:
17:41be4896ed6d
Parent:
6:b6bafd0a7013
Child:
32:0a09505a656d
Fixed issue with LCD controller initialization on some boards due to power instability. Updated following downstream restructuring in OneWire library.

Who changed what in which revision?

UserRevisionLine numberNew 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 6:b6bafd0a7013 36 #include "I2C.h"
IanBenzMaxim 17:41be4896ed6d 37 #include "wait_api.h"
IanBenzMaxim 1:e1c7c1c636af 38
IanBenzMaxim 1:e1c7c1c636af 39 //LCD Commands
IanBenzMaxim 1:e1c7c1c636af 40 //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 41 //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 42 //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 43 enum LCD_Commands
IanBenzMaxim 1:e1c7c1c636af 44 {
IanBenzMaxim 1:e1c7c1c636af 45 ControlByte = 0x00, //Only one control byte will be sent. Only a stream of data bytes is allowed to follow.
IanBenzMaxim 1:e1c7c1c636af 46 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 47 ControlBytes = 0x80, //Another control byte will follow, unless an I2C Stop condition is received.
IanBenzMaxim 1:e1c7c1c636af 48 ControlBytes_RS_Set = 0xC0, //RS Set and another control byte will follow, unless an I2C Stop condition is received.
IanBenzMaxim 1:e1c7c1c636af 49 };
IanBenzMaxim 1:e1c7c1c636af 50
IanBenzMaxim 1:e1c7c1c636af 51 //LCD Instructions
IanBenzMaxim 1:e1c7c1c636af 52 enum LCD_Instructions
IanBenzMaxim 1:e1c7c1c636af 53 {
IanBenzMaxim 1:e1c7c1c636af 54 ClearDisplay = 0x01,
IanBenzMaxim 1:e1c7c1c636af 55 Display_OFF = 0x08, //Display off
IanBenzMaxim 1:e1c7c1c636af 56 Display_ON = 0x0C, //Display on, cursor off, cursor position off
IanBenzMaxim 1:e1c7c1c636af 57 ReturnHome = 0x02,
IanBenzMaxim 1:e1c7c1c636af 58 SetDdramAddress = 0x80
IanBenzMaxim 1:e1c7c1c636af 59 };
IanBenzMaxim 1:e1c7c1c636af 60
IanBenzMaxim 1:e1c7c1c636af 61 // LED Driver Port Registers
IanBenzMaxim 1:e1c7c1c636af 62 // Initial port state 0x80
IanBenzMaxim 1:e1c7c1c636af 63 enum LED_Driver_Ports
IanBenzMaxim 1:e1c7c1c636af 64 {
IanBenzMaxim 1:e1c7c1c636af 65 P1 = 0x01,
IanBenzMaxim 1:e1c7c1c636af 66 P2 = 0x02, // Blue LED
IanBenzMaxim 1:e1c7c1c636af 67 P3 = 0x03, // Green LED
IanBenzMaxim 1:e1c7c1c636af 68 P4 = 0x04 // Red LED
IanBenzMaxim 1:e1c7c1c636af 69 };
IanBenzMaxim 1:e1c7c1c636af 70
IanBenzMaxim 1:e1c7c1c636af 71 // Convert a byte color value into the representation used by the MAX7306 PWM registers
IanBenzMaxim 6:b6bafd0a7013 72 static uint8_t convertColorToPwmRegVal(uint8_t color)
IanBenzMaxim 1:e1c7c1c636af 73 {
IanBenzMaxim 6:b6bafd0a7013 74 const uint8_t staticOffRegVal = 0x80; // LED is static off by setting to input
IanBenzMaxim 6:b6bafd0a7013 75 const uint8_t staticOnRegVal = 0x00; // LED is static on
IanBenzMaxim 6:b6bafd0a7013 76 const uint8_t minOnRegVal = 0x01; // LED on for minimum duty cycle
IanBenzMaxim 1:e1c7c1c636af 77
IanBenzMaxim 6:b6bafd0a7013 78 uint8_t regVal;
IanBenzMaxim 1:e1c7c1c636af 79 if (color == 0x00) // Use static off for no color
IanBenzMaxim 1:e1c7c1c636af 80 {
IanBenzMaxim 1:e1c7c1c636af 81 regVal = staticOffRegVal;
IanBenzMaxim 1:e1c7c1c636af 82 }
IanBenzMaxim 1:e1c7c1c636af 83 else if (color == 0xFF) // Use static on for full color
IanBenzMaxim 1:e1c7c1c636af 84 {
IanBenzMaxim 1:e1c7c1c636af 85 regVal = staticOnRegVal;
IanBenzMaxim 1:e1c7c1c636af 86 }
IanBenzMaxim 1:e1c7c1c636af 87 else // Use standard PWN for all other values
IanBenzMaxim 1:e1c7c1c636af 88 {
IanBenzMaxim 1:e1c7c1c636af 89 // The 3 least significant bits cannot be rendered with the MAX7306
IanBenzMaxim 1:e1c7c1c636af 90 regVal = color >> 3;
IanBenzMaxim 1:e1c7c1c636af 91 if (regVal == staticOnRegVal)
IanBenzMaxim 1:e1c7c1c636af 92 regVal = minOnRegVal;
IanBenzMaxim 1:e1c7c1c636af 93 }
IanBenzMaxim 1:e1c7c1c636af 94 return regVal;
IanBenzMaxim 1:e1c7c1c636af 95 }
IanBenzMaxim 1:e1c7c1c636af 96
IanBenzMaxim 6:b6bafd0a7013 97 Display::Display(mbed::I2C & I2C_intf, uint8_t LCD_I2C_addr, uint8_t LED_driver_I2C_addr)
IanBenzMaxim 1:e1c7c1c636af 98 : m_I2C_intf(I2C_intf), m_LCD_I2C_addr(LCD_I2C_addr), m_LED_driver_I2C_addr(LED_driver_I2C_addr)
IanBenzMaxim 1:e1c7c1c636af 99 {
IanBenzMaxim 1:e1c7c1c636af 100
IanBenzMaxim 1:e1c7c1c636af 101 }
IanBenzMaxim 1:e1c7c1c636af 102
IanBenzMaxim 1:e1c7c1c636af 103 void Display::initialize(void)
IanBenzMaxim 1:e1c7c1c636af 104 {
IanBenzMaxim 1:e1c7c1c636af 105 initializeLCD();
IanBenzMaxim 1:e1c7c1c636af 106 initializeLED_Driver();
IanBenzMaxim 1:e1c7c1c636af 107 }
IanBenzMaxim 1:e1c7c1c636af 108
IanBenzMaxim 1:e1c7c1c636af 109 void Display::initializeLED_Driver(void)
IanBenzMaxim 1:e1c7c1c636af 110 {
IanBenzMaxim 6:b6bafd0a7013 111 const uint8_t Configuration26 = 0x26; //intial port state 0xEC
IanBenzMaxim 6:b6bafd0a7013 112 const uint8_t Configuration27 = 0x27; //intial port state 0x8F
IanBenzMaxim 1:e1c7c1c636af 113
IanBenzMaxim 1:e1c7c1c636af 114 //Intial mode
IanBenzMaxim 1:e1c7c1c636af 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 1:e1c7c1c636af 119 //RST does reset PWM/blink counters, RST resets registers to power-on-reset state
IanBenzMaxim 1:e1c7c1c636af 120 m_I2C_intf.write(0x1F);
IanBenzMaxim 1:e1c7c1c636af 121 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 122
IanBenzMaxim 1:e1c7c1c636af 123 //Write to Configuration Register 0x27
IanBenzMaxim 1:e1c7c1c636af 124 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 125 m_I2C_intf.write(m_LED_driver_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 126 m_I2C_intf.write(Configuration27);
IanBenzMaxim 1:e1c7c1c636af 127 //Enable bus time out, set P1,P2,P3 to be controlled by their registers (0x01,0x02,0x03)
IanBenzMaxim 1:e1c7c1c636af 128 m_I2C_intf.write(0x0E);
IanBenzMaxim 1:e1c7c1c636af 129 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 130 }
IanBenzMaxim 1:e1c7c1c636af 131
IanBenzMaxim 1:e1c7c1c636af 132 void Display::setBackLightColor(const Color & color)
IanBenzMaxim 1:e1c7c1c636af 133 {
IanBenzMaxim 1:e1c7c1c636af 134 // Red
IanBenzMaxim 1:e1c7c1c636af 135 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 136 m_I2C_intf.write(m_LED_driver_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 137 m_I2C_intf.write(P4);
IanBenzMaxim 1:e1c7c1c636af 138 m_I2C_intf.write(convertColorToPwmRegVal(color.R));
IanBenzMaxim 1:e1c7c1c636af 139 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 140
IanBenzMaxim 1:e1c7c1c636af 141 // Green
IanBenzMaxim 1:e1c7c1c636af 142 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 143 m_I2C_intf.write(m_LED_driver_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 144 m_I2C_intf.write(P3);
IanBenzMaxim 1:e1c7c1c636af 145 m_I2C_intf.write(convertColorToPwmRegVal(color.G));
IanBenzMaxim 1:e1c7c1c636af 146 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 147
IanBenzMaxim 1:e1c7c1c636af 148 // Blue
IanBenzMaxim 1:e1c7c1c636af 149 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 150 m_I2C_intf.write(m_LED_driver_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 151 m_I2C_intf.write(P2);
IanBenzMaxim 1:e1c7c1c636af 152 m_I2C_intf.write(convertColorToPwmRegVal(color.B));
IanBenzMaxim 1:e1c7c1c636af 153 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 154 }
IanBenzMaxim 1:e1c7c1c636af 155
IanBenzMaxim 1:e1c7c1c636af 156 void Display::clearLine(Line line)
IanBenzMaxim 1:e1c7c1c636af 157 {
IanBenzMaxim 1:e1c7c1c636af 158 writeCompleteLine("", line);
IanBenzMaxim 1:e1c7c1c636af 159 setCursorPosition(line);
IanBenzMaxim 1:e1c7c1c636af 160 }
IanBenzMaxim 1:e1c7c1c636af 161
IanBenzMaxim 1:e1c7c1c636af 162 void Display::clearDisplay(void)
IanBenzMaxim 1:e1c7c1c636af 163 {
IanBenzMaxim 1:e1c7c1c636af 164 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 165 m_I2C_intf.write(m_LCD_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 166 m_I2C_intf.write(ControlByte); //No more control bytes will be sent
IanBenzMaxim 1:e1c7c1c636af 167 m_I2C_intf.write(ClearDisplay);
IanBenzMaxim 1:e1c7c1c636af 168 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 169 }
IanBenzMaxim 1:e1c7c1c636af 170
IanBenzMaxim 1:e1c7c1c636af 171 void Display::initializeLCD(void)
IanBenzMaxim 1:e1c7c1c636af 172 {
IanBenzMaxim 1:e1c7c1c636af 173 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 174 m_I2C_intf.write(m_LCD_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 175 m_I2C_intf.write(ControlByte); //No more control bytes will be sent
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 17:41be4896ed6d 185 m_I2C_intf.stop();
IanBenzMaxim 17:41be4896ed6d 186 wait_ms(200); //Wait for power stable
IanBenzMaxim 17:41be4896ed6d 187 m_I2C_intf.start();
IanBenzMaxim 17:41be4896ed6d 188 m_I2C_intf.write(m_LCD_I2C_addr);
IanBenzMaxim 17:41be4896ed6d 189 m_I2C_intf.write(ControlByte); //No more control bytes will be sent
IanBenzMaxim 17:41be4896ed6d 190 m_I2C_intf.write(Display_ON); //Display on, cursor on, cursor position on
IanBenzMaxim 17:41be4896ed6d 191 m_I2C_intf.write(ClearDisplay); //Clear Display
IanBenzMaxim 17:41be4896ed6d 192 m_I2C_intf.write(0x06); //ENTRY MODE
IanBenzMaxim 1:e1c7c1c636af 193 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 194 }
IanBenzMaxim 1:e1c7c1c636af 195
IanBenzMaxim 6:b6bafd0a7013 196 void Display::writeCharacter(uint8_t character)
IanBenzMaxim 1:e1c7c1c636af 197 {
IanBenzMaxim 1:e1c7c1c636af 198 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 199 m_I2C_intf.write(m_LCD_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 200 m_I2C_intf.write(ControlByte_RS_Set); //No more control bytes will be sent
IanBenzMaxim 1:e1c7c1c636af 201 m_I2C_intf.write(character); //Display on, cursor on, cursor position on
IanBenzMaxim 1:e1c7c1c636af 202 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 203 }
IanBenzMaxim 1:e1c7c1c636af 204
IanBenzMaxim 1:e1c7c1c636af 205 void Display::writeText(const std::string & text)
IanBenzMaxim 1:e1c7c1c636af 206 {
IanBenzMaxim 1:e1c7c1c636af 207 const char RETURN_CHAR = 0x16;
IanBenzMaxim 1:e1c7c1c636af 208
IanBenzMaxim 6:b6bafd0a7013 209 size_t length = text.length();
IanBenzMaxim 1:e1c7c1c636af 210 if (length > lineLength)
IanBenzMaxim 1:e1c7c1c636af 211 length = lineLength;
IanBenzMaxim 1:e1c7c1c636af 212
IanBenzMaxim 1:e1c7c1c636af 213 //Write to LCD
IanBenzMaxim 1:e1c7c1c636af 214 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 215 m_I2C_intf.write(m_LCD_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 216 m_I2C_intf.write(ControlByte_RS_Set);
IanBenzMaxim 1:e1c7c1c636af 217
IanBenzMaxim 6:b6bafd0a7013 218 for(size_t i = 0; i < length; i++)
IanBenzMaxim 1:e1c7c1c636af 219 {
IanBenzMaxim 1:e1c7c1c636af 220 if(text[i] != RETURN_CHAR)
IanBenzMaxim 1:e1c7c1c636af 221 m_I2C_intf.write(text[i]);
IanBenzMaxim 1:e1c7c1c636af 222 }
IanBenzMaxim 1:e1c7c1c636af 223
IanBenzMaxim 1:e1c7c1c636af 224 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 225 }
IanBenzMaxim 1:e1c7c1c636af 226
IanBenzMaxim 6:b6bafd0a7013 227 void Display::setCursorPosition(Line line, size_t position)
IanBenzMaxim 1:e1c7c1c636af 228 {
IanBenzMaxim 1:e1c7c1c636af 229 if (position > (lineLength - 1)) // Set to last line character for values outside the upper bound
IanBenzMaxim 1:e1c7c1c636af 230 position = (lineLength - 1);
IanBenzMaxim 1:e1c7c1c636af 231
IanBenzMaxim 1:e1c7c1c636af 232 m_I2C_intf.start();
IanBenzMaxim 1:e1c7c1c636af 233 m_I2C_intf.write(m_LCD_I2C_addr);
IanBenzMaxim 1:e1c7c1c636af 234 m_I2C_intf.write(ControlByte); // No more control bytes will be sent
IanBenzMaxim 1:e1c7c1c636af 235 if(line == SecondLine) // Offset for second line
IanBenzMaxim 1:e1c7c1c636af 236 position += 0x40;
IanBenzMaxim 1:e1c7c1c636af 237 m_I2C_intf.write(SetDdramAddress | position);
IanBenzMaxim 1:e1c7c1c636af 238 m_I2C_intf.stop();
IanBenzMaxim 1:e1c7c1c636af 239 }
IanBenzMaxim 1:e1c7c1c636af 240
IanBenzMaxim 1:e1c7c1c636af 241 void Display::writeLine(const std::string & text, Line line)
IanBenzMaxim 1:e1c7c1c636af 242 {
IanBenzMaxim 1:e1c7c1c636af 243 setCursorPosition(line);
IanBenzMaxim 1:e1c7c1c636af 244 writeText(text);
IanBenzMaxim 1:e1c7c1c636af 245 }
IanBenzMaxim 1:e1c7c1c636af 246
IanBenzMaxim 1:e1c7c1c636af 247 void Display::writeCompleteLine(const std::string & text, Line line)
IanBenzMaxim 1:e1c7c1c636af 248 {
IanBenzMaxim 1:e1c7c1c636af 249 // Add padding to user's string
IanBenzMaxim 1:e1c7c1c636af 250 std::string writeText(text);
IanBenzMaxim 1:e1c7c1c636af 251 if (writeText.length() < lineLength)
IanBenzMaxim 1:e1c7c1c636af 252 writeText.append(lineLength - writeText.length(), ' ');
IanBenzMaxim 1:e1c7c1c636af 253
IanBenzMaxim 1:e1c7c1c636af 254 writeLine(writeText, line);
IanBenzMaxim 1:e1c7c1c636af 255 }
IanBenzMaxim 1:e1c7c1c636af 256
IanBenzMaxim 1:e1c7c1c636af 257 void Display::writeMessage(const std::string & message)
IanBenzMaxim 1:e1c7c1c636af 258 {
IanBenzMaxim 1:e1c7c1c636af 259 if (message.length() > lineLength)
IanBenzMaxim 1:e1c7c1c636af 260 {
IanBenzMaxim 1:e1c7c1c636af 261 // Find split point
IanBenzMaxim 1:e1c7c1c636af 262 std::istringstream messageStream(message);
IanBenzMaxim 1:e1c7c1c636af 263 std::string word;
IanBenzMaxim 6:b6bafd0a7013 264 size_t splitIndex = 0;
IanBenzMaxim 1:e1c7c1c636af 265 do
IanBenzMaxim 1:e1c7c1c636af 266 {
IanBenzMaxim 1:e1c7c1c636af 267 if (word.length() > 0)
IanBenzMaxim 1:e1c7c1c636af 268 splitIndex += (word.length() + 1);
IanBenzMaxim 1:e1c7c1c636af 269 std::getline(messageStream, word, ' ');
IanBenzMaxim 1:e1c7c1c636af 270 } while ((splitIndex + word.length()) <= lineLength);
IanBenzMaxim 1:e1c7c1c636af 271 if (splitIndex == 0) // First word is too long
IanBenzMaxim 1:e1c7c1c636af 272 {
IanBenzMaxim 1:e1c7c1c636af 273 writeCompleteLine(message.substr(0, lineLength), FirstLine);
IanBenzMaxim 1:e1c7c1c636af 274 writeCompleteLine(message.substr(lineLength), SecondLine);
IanBenzMaxim 1:e1c7c1c636af 275 }
IanBenzMaxim 1:e1c7c1c636af 276 else
IanBenzMaxim 1:e1c7c1c636af 277 {
IanBenzMaxim 1:e1c7c1c636af 278 writeCompleteLine(message.substr(0, splitIndex - 1), FirstLine);
IanBenzMaxim 1:e1c7c1c636af 279 writeCompleteLine(message.substr(splitIndex), SecondLine);
IanBenzMaxim 1:e1c7c1c636af 280 }
IanBenzMaxim 1:e1c7c1c636af 281 }
IanBenzMaxim 1:e1c7c1c636af 282 else
IanBenzMaxim 1:e1c7c1c636af 283 {
IanBenzMaxim 1:e1c7c1c636af 284 writeCompleteLine(message, FirstLine);
IanBenzMaxim 1:e1c7c1c636af 285 writeCompleteLine("", SecondLine);
IanBenzMaxim 1:e1c7c1c636af 286 }
IanBenzMaxim 1:e1c7c1c636af 287 }