939449a40f58d57cd4b2382c740b54c727c470fe
[contrib/upm.git] / src / lcd / ssd1308.cxx
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <string>
26 #include <unistd.h>
27
28 #include "ssd1308.h"
29
30 using namespace upm;
31
32 SSD1308::SSD1308 (int bus_in, int addr_in) : I2CLcd (bus_in, addr_in) {
33     i2Cmd (m_i2c_lcd_control, DISPLAY_CMD_OFF);  // display off
34     printf ("NO_GDB :: DISPLAY_CMD_OFF \n");
35     usleep (4500);
36     i2Cmd (m_i2c_lcd_control, DISPLAY_CMD_ON);   // display on
37     usleep (4500);
38     setNormalDisplay ();                         // set to normal display '1' is ON
39
40     clear ();
41     setAddressingMode (PAGE);
42 }
43
44 SSD1308::~SSD1308 () {
45
46 }
47
48 maa_result_t
49 SSD1308::draw (uint8_t *data, int bytes) {
50     maa_result_t error = MAA_SUCCESS;
51
52     setAddressingMode (HORIZONTAL);
53     for (int idx = 0; idx < bytes; idx++) {
54         i2cData (m_i2c_lcd_control, data[idx]);
55     }
56
57     return error;
58 }
59
60 /*
61  * **************
62  *  virtual area
63  * **************
64  */
65 maa_result_t
66 SSD1308::write (std::string msg) {
67     maa_result_t error = MAA_SUCCESS;
68     uint8_t data[2] = {0x40, 0};
69
70     setAddressingMode (PAGE);
71     for (std::string::size_type i = 0; i < msg.size(); ++i) {
72         writeChar (m_i2c_lcd_control, msg[i]);
73     }
74
75     return error;
76 }
77
78 maa_result_t
79 SSD1308::setCursor (int row, int column) {
80     maa_result_t error = MAA_SUCCESS;
81
82     error = i2Cmd (m_i2c_lcd_control, BASE_PAGE_START_ADDR + row);                           // set page address
83     error = i2Cmd (m_i2c_lcd_control, BASE_LOW_COLUMN_ADDR + (8 * column & 0x0F));           // set column lower address
84     error = i2Cmd (m_i2c_lcd_control, BASE_HIGH_COLUMN_ADDR + ((8 * column >> 4) & 0x0F));   // set column higher address
85
86     return error;
87 }
88
89 maa_result_t
90 SSD1308::clear () {
91     maa_result_t error = MAA_SUCCESS;
92     uint8_t columnIdx, rowIdx;
93
94     i2Cmd (m_i2c_lcd_control, DISPLAY_CMD_OFF);  // display off
95     for(rowIdx = 0; rowIdx < 8; rowIdx++) {
96         setCursor (rowIdx, 0);
97
98         // clear all columns
99         for(columnIdx = 0; columnIdx < 16; columnIdx++) {
100             writeChar (m_i2c_lcd_control, ' ');
101         }
102     }
103     i2Cmd (m_i2c_lcd_control, DISPLAY_CMD_ON);   // display on
104     home ();
105
106     return MAA_SUCCESS;
107 }
108
109 maa_result_t
110 SSD1308::home () {
111     return setCursor (0, 0);
112 }
113
114 /*
115  * **************
116  *  private area
117  * **************
118  */
119 maa_result_t
120 SSD1308::writeChar (maa_i2c_context ctx, uint8_t value) {
121     if (value < 0x20 || value > 0x7F) {
122         value = 0x20; // space
123     }
124
125     for (uint8_t idx = 0; idx < 8; idx++) {
126         i2cData (m_i2c_lcd_control, BasicFont[value - 32][idx]);
127     }
128 }
129
130 maa_result_t
131 SSD1308::setNormalDisplay () {
132     return i2Cmd (m_i2c_lcd_control, DISPLAY_CMD_SET_NORMAL);    // set to normal display '1' is ON
133 }
134
135 maa_result_t
136 SSD1308::setAddressingMode (displayAddressingMode mode) {
137     i2Cmd (m_i2c_lcd_control, DISPLAY_CMD_MEM_ADDR_MODE); //set addressing mode
138     i2Cmd (m_i2c_lcd_control, mode); //set page addressing mode
139 }