Show the file size of the files in the tmp_dir on the LCD.
[ruwai.git] / software / c++ / ruwaicom / src / lcddisplay.cpp
blob952c68a3f13fd89d6dfd09aaf5fa57dcb176cf62
1 /*--------------------------------------------------------------------------*/
2 // LICENSE
3 //
4 // This file is part of ruwai.
5 //
6 // If you use ruwai_parser in any program or publication, please inform and
7 // acknowledge its author Stefan Mertl (stefan@mertl-research.at).
8 //
9 // ruwai is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /*--------------------------------------------------------------------------*/
23 #include "lcddisplay.h"
24 #include <unistd.h>
25 #include <iostream>
27 LCDDisplay::LCDDisplay(int rs_gpio, int e_gpio, int db0_gpio, int db1_gpio, int db2_gpio, int db3_gpio, int db4_gpio, int db5_gpio, int db6_gpio, int db7_gpio)
29 this->mode = 8;
30 this->rs_gpio = new GPIO(rs_gpio);
31 this->e_gpio = new GPIO(e_gpio);
32 this->db0_gpio = new GPIO(db0_gpio);
33 this->db1_gpio = new GPIO(db1_gpio);
34 this->db2_gpio = new GPIO(db2_gpio);
35 this->db3_gpio = new GPIO(db3_gpio);
36 this->db4_gpio = new GPIO(db4_gpio);
37 this->db5_gpio = new GPIO(db5_gpio);
38 this->db6_gpio = new GPIO(db6_gpio);
39 this->db7_gpio = new GPIO(db7_gpio);
41 // Initialize the gpios.
42 this->rs_gpio->set_direction(OUTPUT);
43 this->e_gpio->set_direction(OUTPUT);
44 this->db0_gpio->set_direction(OUTPUT);
45 this->db1_gpio->set_direction(OUTPUT);
46 this->db2_gpio->set_direction(OUTPUT);
47 this->db3_gpio->set_direction(OUTPUT);
48 this->db4_gpio->set_direction(OUTPUT);
49 this->db5_gpio->set_direction(OUTPUT);
50 this->db6_gpio->set_direction(OUTPUT);
51 this->db7_gpio->set_direction(OUTPUT);
55 LCDDisplay::LCDDisplay(int rs_gpio, int e_gpio, int db4_gpio, int db5_gpio, int db6_gpio, int db7_gpio)
57 this->mode = 4;
58 this->rs_gpio = new GPIO(rs_gpio);
59 this->e_gpio = new GPIO(e_gpio);
60 this->db4_gpio = new GPIO(db4_gpio);
61 this->db5_gpio = new GPIO(db5_gpio);
62 this->db6_gpio = new GPIO(db6_gpio);
63 this->db7_gpio = new GPIO(db7_gpio);
65 // Initialize the gpios.
66 this->rs_gpio->set_direction(OUTPUT);
67 this->e_gpio->set_direction(OUTPUT);
68 this->db4_gpio->set_direction(OUTPUT);
69 this->db5_gpio->set_direction(OUTPUT);
70 this->db6_gpio->set_direction(OUTPUT);
71 this->db7_gpio->set_direction(OUTPUT);
75 void
76 LCDDisplay::init(void)
78 switch(mode)
80 case 4:
81 init_4bit();
82 break;
83 case 8:
84 init_8bit();
85 break;
90 void
91 LCDDisplay::init_4bit(void)
93 usleep(100000);
95 write_4bit_high_only(0x30, true);
96 usleep(15000);
98 write_4bit_high_only(0x30, true);
99 usleep(2000);
101 write_4bit_high_only(0x30, true);
102 //write_4bit(LCD_CMD_FUNCTION_SET | LCD_FUNCTION_TWO_LINES, true);
103 usleep(2000);
105 write_4bit_high_only(0x20, true);
106 //write_4bit(LCD_CMD_FUNCTION_SET | LCD_FUNCTION_TWO_LINES, true);
107 usleep(2000);
109 write_4bit(LCD_CMD_FUNCTION_SET | LCD_FUNCTION_TWO_LINES, true);
110 usleep(LCD_SHORT_DELAY);
112 // display on
113 //write_command(0b00001110);
114 write_command(LCD_CMD_DISPLAY | LCD_DISPLAY_DISPLAY_ON |LCD_DISPLAY_CURSOR_ON);
115 usleep(LCD_SHORT_DELAY);
119 void
120 LCDDisplay::init_8bit(void)
122 usleep(100000);
124 write_8bit(LCD_CMD_FUNCTION_SET | LCD_FUNCTION_8BIT_MODE | LCD_FUNCTION_TWO_LINES, true);
125 usleep(15000);
127 write_8bit(LCD_CMD_FUNCTION_SET | LCD_FUNCTION_8BIT_MODE | LCD_FUNCTION_TWO_LINES, true);
128 usleep(2000);
130 switch (mode)
132 case 4:
133 // Select 4 bit mode.
134 write_8bit(LCD_CMD_FUNCTION_SET | LCD_FUNCTION_TWO_LINES, true);
135 break;
136 case 8:
137 write_8bit(LCD_CMD_FUNCTION_SET | LCD_FUNCTION_8BIT_MODE | LCD_FUNCTION_TWO_LINES, true);
138 break;
140 usleep(LCD_SHORT_DELAY);
142 // display on
143 //write_command(0b00001110);
144 write_command(LCD_CMD_DISPLAY | LCD_DISPLAY_DISPLAY_ON |LCD_DISPLAY_CURSOR_ON);
145 usleep(LCD_SHORT_DELAY);
148 void
149 LCDDisplay::write_8bit(std::uint8_t data, bool command)
151 std::cout << "Write data: " << data << std::endl;
152 if(command)
153 rs_gpio->set_value(LOW);
154 else
155 rs_gpio->set_value(HIGH);
157 // Write the high nibble.
158 db7_gpio->set_value((gpio_value_t)((data & 0x80) >> 7));
159 db6_gpio->set_value((gpio_value_t)((data & 0x40) >> 6));
160 db5_gpio->set_value((gpio_value_t)((data & 0x20) >> 5));
161 db4_gpio->set_value((gpio_value_t)((data & 0x10) >> 4));
162 db3_gpio->set_value((gpio_value_t)((data & 0x08) >> 3));
163 db2_gpio->set_value((gpio_value_t)((data & 0x04) >> 2));
164 db1_gpio->set_value((gpio_value_t)((data & 0x02) >> 1));
165 db0_gpio->set_value((gpio_value_t)(data & 0x01));
166 toggle_e();
170 void
171 LCDDisplay::write_4bit(std::uint8_t data, bool command)
173 std::cout << "Write data: " << data << std::endl;
174 if(command)
175 rs_gpio->set_value(LOW);
176 else
177 rs_gpio->set_value(HIGH);
179 // Write the high nibble.
180 db7_gpio->set_value((gpio_value_t)((data & 0x80) >> 7));
181 db6_gpio->set_value((gpio_value_t)((data & 0x40) >> 6));
182 db5_gpio->set_value((gpio_value_t)((data & 0x20) >> 5));
183 db4_gpio->set_value((gpio_value_t)((data & 0x10) >> 4));
184 toggle_e();
186 // Write the low nibble.
187 db7_gpio->set_value((gpio_value_t)((data & 0x08) >> 3));
188 db6_gpio->set_value((gpio_value_t)((data & 0x04) >> 2));
189 db5_gpio->set_value((gpio_value_t)((data & 0x02) >> 1));
190 db4_gpio->set_value((gpio_value_t)(data & 0x01));
191 toggle_e();
195 void
196 LCDDisplay::write_4bit_high_only(std::uint8_t data, bool command)
198 std::cout << "Write data: " << data << std::endl;
199 if(command)
200 rs_gpio->set_value(LOW);
201 else
202 rs_gpio->set_value(HIGH);
204 // Write the high nibble.
205 db7_gpio->set_value((gpio_value_t)((data & 0x80) >> 7));
206 db6_gpio->set_value((gpio_value_t)((data & 0x40) >> 6));
207 db5_gpio->set_value((gpio_value_t)((data & 0x20) >> 5));
208 db4_gpio->set_value((gpio_value_t)((data & 0x10) >> 4));
209 toggle_e();
212 void
213 LCDDisplay::write_command(std::uint8_t data)
215 switch(mode)
217 case 4:
218 write_4bit(data, true);
219 break;
220 case 8:
221 write_8bit(data, true);
222 break;
226 void
227 LCDDisplay::write_data(std::uint8_t data)
229 switch(mode)
231 case 4:
232 write_4bit(data, false);
233 break;
234 case 8:
235 write_8bit(data, false);
236 break;
241 void
242 LCDDisplay::toggle_e(void)
244 //e_gpio->set_value(LOW);
245 //usleep(10);
246 e_gpio->set_value(HIGH);
247 usleep(1000);
248 e_gpio->set_value(LOW);
251 void
252 LCDDisplay::clear(void)
254 write_command(0x01);
255 usleep(LCD_LONG_DELAY);
258 void
259 LCDDisplay::home(void)
261 write_command(0x02);
262 usleep(LCD_LONG_DELAY);
266 void
267 LCDDisplay::print(std::string msg_line1, std::string msg_line2)
269 home();
270 clear();
272 for (int k = 0; k < msg_line1.length(); k++)
274 write_data(msg_line1[k]);
277 // Go to the beginning of line 2.
278 write_command(LCD_CMD_SET_DDRAM_ADDR | 0x40);
280 for (int k = 0; k < msg_line2.length(); k++)
282 write_data(msg_line2[k]);