init

Dependencies:   mbed C12832 EthernetInterface MQTT mbed-rtos picojson

Committer:
co838_mgl6
Date:
Thu May 05 14:02:24 2016 +0000
Revision:
3:f809d8f8e572
Parent:
1:1e45dd2c91fb
final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_mgl6 1:1e45dd2c91fb 1 /*
co838_mgl6 1:1e45dd2c91fb 2 * Marc Le Labourier
co838_mgl6 1:1e45dd2c91fb 3 * 16/02/2016
co838_mgl6 1:1e45dd2c91fb 4 */
co838_mgl6 1:1e45dd2c91fb 5
co838_mgl6 1:1e45dd2c91fb 6 #include "LCD.h"
co838_mgl6 1:1e45dd2c91fb 7
co838_mgl6 1:1e45dd2c91fb 8 LCD::LCD() : _host(D11, D13, D12, D7, D10)
co838_mgl6 1:1e45dd2c91fb 9 {
co838_mgl6 1:1e45dd2c91fb 10 _line = 0;
co838_mgl6 1:1e45dd2c91fb 11 _pos = 0;
co838_mgl6 1:1e45dd2c91fb 12 _it = _buffer.begin();
co838_mgl6 1:1e45dd2c91fb 13 }
co838_mgl6 1:1e45dd2c91fb 14
co838_mgl6 1:1e45dd2c91fb 15 LCD::~LCD()
co838_mgl6 1:1e45dd2c91fb 16 {
co838_mgl6 1:1e45dd2c91fb 17 _buffer.clear();
co838_mgl6 1:1e45dd2c91fb 18 }
co838_mgl6 1:1e45dd2c91fb 19
co838_mgl6 1:1e45dd2c91fb 20 C12832& LCD::Host()
co838_mgl6 1:1e45dd2c91fb 21 {
co838_mgl6 1:1e45dd2c91fb 22 return _host;
co838_mgl6 1:1e45dd2c91fb 23 }
co838_mgl6 1:1e45dd2c91fb 24
co838_mgl6 1:1e45dd2c91fb 25 /* Clear process:
co838_mgl6 1:1e45dd2c91fb 26 * The screen buffer is full...
co838_mgl6 1:1e45dd2c91fb 27 * We need to extract one line and to redisplay the content from the start of the buffer.
co838_mgl6 1:1e45dd2c91fb 28 * If we do not wait during the process, the screen will not be readable.
co838_mgl6 1:1e45dd2c91fb 29 * The wait_ms is responsible of the 'scrolling effect'.
co838_mgl6 1:1e45dd2c91fb 30 */
co838_mgl6 1:1e45dd2c91fb 31 void LCD::clear()
co838_mgl6 1:1e45dd2c91fb 32 {
co838_mgl6 1:1e45dd2c91fb 33 wait_ms(1000);
co838_mgl6 1:1e45dd2c91fb 34 if (_buffer.size() > SCREEN_SIZE)
co838_mgl6 1:1e45dd2c91fb 35 _buffer.pop_front();
co838_mgl6 1:1e45dd2c91fb 36 _host.cls();
co838_mgl6 1:1e45dd2c91fb 37 }
co838_mgl6 1:1e45dd2c91fb 38
co838_mgl6 1:1e45dd2c91fb 39 /* Push a string to be rendered on the LCD screen:
co838_mgl6 1:1e45dd2c91fb 40 * Long string are split in many other and added to the buffer.
co838_mgl6 1:1e45dd2c91fb 41 */
co838_mgl6 1:1e45dd2c91fb 42 void LCD::print(const std::string& s)
co838_mgl6 1:1e45dd2c91fb 43 {
co838_mgl6 1:1e45dd2c91fb 44 if (s.size() > SCREEN_CHAR)
co838_mgl6 1:1e45dd2c91fb 45 {
co838_mgl6 1:1e45dd2c91fb 46 for (size_t i = 0; i < s.size(); i += SCREEN_CHAR)
co838_mgl6 1:1e45dd2c91fb 47 {
co838_mgl6 1:1e45dd2c91fb 48 std::string tmp = s.substr(i, SCREEN_CHAR);
co838_mgl6 1:1e45dd2c91fb 49 _buffer.push_back(tmp);
co838_mgl6 1:1e45dd2c91fb 50 }
co838_mgl6 1:1e45dd2c91fb 51 }
co838_mgl6 1:1e45dd2c91fb 52 else
co838_mgl6 1:1e45dd2c91fb 53 _buffer.push_back(s);
co838_mgl6 1:1e45dd2c91fb 54 printAll();
co838_mgl6 1:1e45dd2c91fb 55 }
co838_mgl6 1:1e45dd2c91fb 56
co838_mgl6 1:1e45dd2c91fb 57 /* Update the screen process
co838_mgl6 1:1e45dd2c91fb 58 * Update the screen with the new input from the print() method.
co838_mgl6 1:1e45dd2c91fb 59 * All the magic is done here.
co838_mgl6 1:1e45dd2c91fb 60 */
co838_mgl6 1:1e45dd2c91fb 61 void LCD::printAll()
co838_mgl6 1:1e45dd2c91fb 62 {
co838_mgl6 1:1e45dd2c91fb 63 if (_it == _buffer.end())
co838_mgl6 1:1e45dd2c91fb 64 {
co838_mgl6 1:1e45dd2c91fb 65 _it = _buffer.begin();
co838_mgl6 1:1e45dd2c91fb 66 for (int i = 0; i < _line; ++i)
co838_mgl6 1:1e45dd2c91fb 67 ++_it;
co838_mgl6 1:1e45dd2c91fb 68 }
co838_mgl6 1:1e45dd2c91fb 69 for (; _it != _buffer.end(); ++_it)
co838_mgl6 1:1e45dd2c91fb 70 {
co838_mgl6 1:1e45dd2c91fb 71 if (_line > SCREEN_SIZE)
co838_mgl6 1:1e45dd2c91fb 72 {
co838_mgl6 1:1e45dd2c91fb 73 _line = 0;
co838_mgl6 1:1e45dd2c91fb 74 _pos = 0;
co838_mgl6 1:1e45dd2c91fb 75 clear();
co838_mgl6 1:1e45dd2c91fb 76 _it = _buffer.begin();
co838_mgl6 1:1e45dd2c91fb 77 }
co838_mgl6 1:1e45dd2c91fb 78 std::string s = *_it;
co838_mgl6 1:1e45dd2c91fb 79 printOne(s, _pos);
co838_mgl6 1:1e45dd2c91fb 80 _pos += SCREEN_PADDING;
co838_mgl6 1:1e45dd2c91fb 81 ++_line;
co838_mgl6 1:1e45dd2c91fb 82 }
co838_mgl6 1:1e45dd2c91fb 83 }
co838_mgl6 1:1e45dd2c91fb 84
co838_mgl6 1:1e45dd2c91fb 85 /* Render a string on the screen */
co838_mgl6 1:1e45dd2c91fb 86 void LCD::printOne(const std::string& s, int index)
co838_mgl6 1:1e45dd2c91fb 87 {
co838_mgl6 1:1e45dd2c91fb 88 _host.locate(0, index);
co838_mgl6 1:1e45dd2c91fb 89 _host.printf("%s", s.c_str());
co838_mgl6 1:1e45dd2c91fb 90 }