Commit 3e901ee011c115dd8c44c5b4270569e8a825a1df

Authored by Enrique Díaz Roque
1 parent 6593eb6e
Exists in master and in 1 other branch dev

LCD completed, some commands are not implemented yet but I don't think we will be using them

tests/LCD/lib/LCD/LCD.cpp
1 1 #include "LCD.h"
2 2  
3 3 LCD::LCD() {
4   - lcd_addr = DEFAULT_ADDRESS;
5   - lcd_setup();
  4 + lcd_addr = LCD_DEFAULT_ADDRESS >> 1;
  5 + Wire.begin();
6 6 }
7 7  
8 8 LCD::LCD(byte lcd_addr) {
9   - this->lcd_addr = lcd_addr;
10   - lcd_setup();
  9 + this->lcd_addr = lcd_addr >> 1;
  10 + Wire.begin();
11 11 }
12 12  
13   -void LCD::lcd_setup() {
14   - Wire.begin();
15   - // check if device exists
  13 +bool LCD::is_alive() {
  14 + Wire.beginTransmission(lcd_addr);
  15 + return Wire.endTransmission() == 0;
16 16 }
17 17  
18 18 void LCD::command(byte cmd) {
19   - Wire.write(COMMAND);
  19 + Wire.write(LCD_COMMAND);
20 20 Wire.write(cmd);
21 21 }
22 22  
23 23 void LCD::backlight(bool state) {
24 24 Wire.beginTransmission(lcd_addr);
25 25 if (state) {
26   - command(BACKLIGHT_ON);
  26 + command(LCD_BACKLIGHT_ON);
27 27 } else {
28   - command(BACKLIGHT_OFF);
  28 + command(LCD_BACKLIGHT_OFF);
  29 + }
  30 + Wire.endTransmission();
  31 +}
  32 +
  33 +void LCD::set_type(byte type) {
  34 + Wire.beginTransmission(lcd_addr);
  35 + command(LCD_SET_DISPLAY_TYPE);
  36 + Wire.write(type);
  37 + Wire.endTransmission();
  38 +}
  39 +
  40 +void LCD::change_address(byte addr) {
  41 + Wire.beginTransmission(lcd_addr);
  42 + command(LCD_CHANGE_ADDRESS);
  43 + command(LCD_CHANGE_ADDRESS_1);
  44 + command(LCD_CHANGE_ADDRESS_2);
  45 + command(LCD_CHANGE_ADDRESS_3);
  46 + Wire.write(addr);
  47 + Wire.endTransmission();
  48 +
  49 + lcd_addr = addr;
  50 +}
  51 +
  52 +void LCD::print(char *string) {
  53 + Wire.beginTransmission(lcd_addr);
  54 + Wire.write(LCD_COMMAND);
  55 + uint8_t length = strlen(string);
  56 + for (int i = 0; i < length; i++) {
  57 + Wire.write(*string++);
29 58 }
30 59 Wire.endTransmission();
31 60 }
32 61  
33 62 void LCD::clear_screen() {
34 63 Wire.beginTransmission(lcd_addr);
35   - command(CLEAR_SCREEN);
  64 + command(LCD_CLEAR_SCREEN);
  65 + Wire.endTransmission();
  66 +}
  67 +
  68 +void LCD::cursor_home() {
  69 + Wire.beginTransmission(lcd_addr);
  70 + command(LCD_CURSOR_HOME);
  71 + Wire.endTransmission();
  72 +}
  73 +
  74 +void LCD::set_cursor(byte x, byte y) {
  75 + Wire.beginTransmission(lcd_addr);
  76 + command(LCD_SET_CURSOR_COORDS);
  77 + Wire.write(y);
  78 + Wire.write(x);
36 79 Wire.endTransmission();
37 80 }
38 81  
  82 +void LCD::hide_cursor() {
  83 + Wire.beginTransmission(lcd_addr);
  84 + command(LCD_HIDE_CURSOR);
  85 + Wire.endTransmission();
  86 +}
  87 +
  88 +void LCD::underline_cursor() {
  89 + Wire.beginTransmission(lcd_addr);
  90 + command(LCD_SHOW_UNDERLINE_CURSOR);
  91 + Wire.endTransmission();
  92 +}
  93 +
  94 +void LCD::blink_cursor() {
  95 + Wire.beginTransmission(lcd_addr);
  96 + command(LCD_SHOW_BLINKING_CURSOR);
  97 + Wire.endTransmission();
  98 +}
  99 +
  100 +void LCD::backspace() {
  101 + Wire.beginTransmission(lcd_addr);
  102 + command(LCD_BACKSPACE);
  103 + Wire.endTransmission();
  104 +}
  105 +
... ...
tests/LCD/lib/LCD/LCD.h
... ... @@ -10,46 +10,67 @@
10 10  
11 11 // Commands LCD05
12 12  
13   -#define COMMAND byte(0)
14   -#define CURSOR_HOME byte(1)
15   -#define SET_CURSOR byte(2)
16   -#define SET_CURSOR_COORDS byte(3)
17   -#define HIDE_CURSOR byte(4)
18   -#define SHOW_UNDERLINE_CURSOR byte(5)
19   -#define SHOW_BLINKING_CURSOR byte(6)
20   -#define BACKSPACE byte(8)
21   -#define HORIZONTAL_TAB byte(9)
22   -#define SMART_LINE_FEED byte(10)
23   -#define VERTICAL_TAB byte(11)
24   -#define CLEAR_SCREEN byte(12)
25   -#define CARRIAGE_RETURN byte(13)
26   -#define CLEAR_COLUMN byte(17)
27   -#define TAB_SET byte(18)
28   -#define BACKLIGHT_ON byte(19)
29   -#define BACKLIGHT_OFF byte(20)
30   -#define DISABLE_STARTUP_MESSAGE byte(21)
31   -#define ENABLE_STARTUP_MESSAGE byte(22)
32   -#define SAVE_AS_STARTUP_SCREEN byte(23)
33   -#define SET_DISPLAY_TYPE byte(24)
34   -#define CHANGE_ADDRESS byte(25)
35   -#define CUSTOM_CHAR_GENERATOR byte(27)
36   -#define DOUBLE_KEYPAD_SCAN_RATE byte(28)
37   -#define NORMAL_KEYPAD_SCAN_RATE byte(29)
38   -#define CONTRAST_SET byte(30)
39   -#define BRIGHTNESS_SET byte(31)
40   -
41   -#define DEFAULT_ADDRESS byte(0xC6)
  13 +#define LCD_COMMAND byte(0)
  14 +#define LCD_CURSOR_HOME byte(1)
  15 +#define LCD_SET_CURSOR byte(2)
  16 +#define LCD_SET_CURSOR_COORDS byte(3)
  17 +#define LCD_HIDE_CURSOR byte(4)
  18 +#define LCD_SHOW_UNDERLINE_CURSOR byte(5)
  19 +#define LCD_SHOW_BLINKING_CURSOR byte(6)
  20 +#define LCD_BACKSPACE byte(8)
  21 +#define LCD_HORIZONTAL_TAB byte(9)
  22 +#define LCD_SMART_LINE_FEED byte(10)
  23 +#define LCD_VERTICAL_TAB byte(11)
  24 +#define LCD_CLEAR_SCREEN byte(12)
  25 +#define LCD_CARRIAGE_RETURN byte(13)
  26 +#define LCD_FIFO_STATUS byte(14)
  27 +#define LCD_CLEAR_COLUMN byte(17)
  28 +#define LCD_TAB_SET byte(18)
  29 +#define LCD_BACKLIGHT_ON byte(19)
  30 +#define LCD_BACKLIGHT_OFF byte(20)
  31 +#define LCD_DISABLE_STARTUP_MESSAGE byte(21)
  32 +#define LCD_ENABLE_STARTUP_MESSAGE byte(22)
  33 +#define LCD_SAVE_AS_STARTUP_SCREEN byte(23)
  34 +#define LCD_SET_DISPLAY_TYPE byte(24)
  35 +#define LCD_CHANGE_ADDRESS byte(25)
  36 +#define LCD_CUSTOM_CHAR_GENERATOR byte(27)
  37 +#define LCD_DOUBLE_KEYPAD_SCAN_RATE byte(28)
  38 +#define LCD_NORMAL_KEYPAD_SCAN_RATE byte(29)
  39 +#define LCD_CONTRAST_SET byte(30)
  40 +#define LCD_BRIGHTNESS_SET byte(31)
  41 +#define LCD_CHANGE_ADDRESS_1 byte(160)
  42 +#define LCD_CHANGE_ADDRESS_3 byte(165)
  43 +#define LCD_CHANGE_ADDRESS_2 byte(170)
  44 +
  45 +#define LCD_DEFAULT_ADDRESS byte(0xC6)
  46 +#define LCD_MAX_CHANCE 500
42 47  
43 48 class LCD {
44 49 public:
  50 + static const byte GLCD_20X4 = 3;
  51 + static const byte BLCD_20X4 = 4;
  52 + static const byte GLCD_16X2 = 5;
  53 + static const byte BLCD_16X2 = 6;
  54 +
45 55 LCD();
46   - LCD(byte lcd_addr);
  56 + LCD(uint8_t lcd_addr);
  57 +
  58 + bool is_alive();
  59 + void backlight(bool state);
  60 + void set_type(byte type);
  61 + void change_address(byte addr);
  62 + void print(char *string);
  63 + void clear_screen();
  64 + void cursor_home();
  65 + void set_cursor(byte x, byte y);
  66 + void hide_cursor();
  67 + void blink_cursor();
  68 + void underline_cursor();
  69 + void backspace();
47 70 private:
48   - byte lcd_addr;
  71 + uint8_t lcd_addr;
49 72  
50   - void lcd_setup();
51 73 void command(byte cmd);
52   - void backlight(bool state);
53 74 };
54 75  
55 76 #endif
... ...
tests/LCD/src/main.cpp
... ... @@ -2,6 +2,22 @@
2 2  
3 3 LCD lcd;
4 4  
5   -void setup() {}
  5 +void setup() {
  6 + Serial.begin(9600);
  7 + Serial.print("LCD alive = ");
  8 + Serial.println(lcd.is_alive());
  9 +
  10 + lcd.set_type(LCD::GLCD_16X2);
  11 + lcd.clear_screen();
  12 + lcd.hide_cursor();
  13 + lcd.set_cursor(4, 2);
  14 + lcd.print("Hello");
  15 + lcd.backspace();
  16 +}
6 17  
7   -void loop() {}
  18 +void loop() {
  19 + lcd.backlight(true);
  20 + delay(1000);
  21 + lcd.backlight(false);
  22 + delay(1000);
  23 +}
... ...