SMOOTHIEPANEL LIVES! :D
[clinton/Smoothieware.git] / src / modules / utils / panel / panels / smoothiepanel / LCDBang.h
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7 #ifndef LCDBANG_H
8 #define LCDBANG_H
9 #include "mbed.h" // mbed.h lib
10 #include "I2CBang.h"
11
12 void lcdbang_writenibble(I2C i2c, char c, bool command = false){
13 const int addr = 0x40;
14 char cmd[2];
15 c <<= 4;
16 c &= 0xF0;
17 c |= 0x01;
18 if(!command) c |= 0x02;
19
20 cmd[0] = 0x0C;
21 cmd[1] = c;
22 i2c.write(addr, cmd, 2);
23 cmd[1] = c | 0x08;
24 i2c.write(addr, cmd, 2);
25 cmd[1] = c;
26 i2c.write(addr, cmd, 2);
27 // wait_ms(1);
28 }
29
30 void lcdbang_write(I2C i2c, char c, bool command = false){
31 if(command){
32 lcdbang_writenibble(i2c, c, command);
33 }else{
34 lcdbang_writenibble(i2c, c >> 4, command);
35 lcdbang_writenibble(i2c, c, command);
36 }
37 }
38
39 void lcdbang_init(I2C i2c){
40 const int addr = 0x40;
41 char cmd[2];
42 cmd[0] = 0x1C;
43 cmd[1] = 0x00;
44 i2c.write(addr, cmd, 2);
45
46 lcdbang_write(i2c, 0x3, true);
47 wait_ms(50);
48 lcdbang_write(i2c, 0x3, true);
49 wait_ms(10);
50 lcdbang_write(i2c, 0x3, true);
51 wait_ms(10);
52 lcdbang_write(i2c, 0x2, true);
53 wait_ms(1);
54
55 lcdbang_write(i2c, 0x2, true);
56 lcdbang_write(i2c, 0x8, true);
57 wait_ms(1);
58
59 lcdbang_write(i2c, 0x0, true);
60 lcdbang_write(i2c, 0x8, true);
61 wait_ms(1);
62
63 lcdbang_write(i2c, 0x0, true);
64 lcdbang_write(i2c, 0x1, true);
65 wait_ms(1);
66
67 lcdbang_write(i2c, 0x0, true);
68 lcdbang_write(i2c, 0x6, true);
69 wait_ms(1);
70
71 lcdbang_write(i2c, 0x0, true);
72 lcdbang_write(i2c, 0x2, true);
73 wait_ms(1);
74
75 lcdbang_write(i2c, 0x0, true);
76 lcdbang_write(i2c, 0xC, true);
77 wait_ms(1);
78 }
79
80 void lcdbang_print(I2C i2c, const char* msg){
81 for(int i=0;msg[i];i++){
82 lcdbang_write(i2c, msg[i]);
83 }
84 }
85
86 void lcdbang_contrast(I2C i2c, int contrast){
87 // set dac pins as output and set dac
88 i2cbang_init(i2c);
89 i2cbang_start(i2c);
90 i2cbang_write(i2c, 0xC0);
91 i2cbang_write(i2c, 0x60);
92 i2cbang_write(i2c, contrast >> 8);
93 i2cbang_write(i2c, (contrast << 8) & 0xF0 );
94 i2cbang_stop(i2c);
95 }
96
97 #endif // LCDBANG_H
98