Skip to content

Commit 74c486a

Browse files
committed
change LiquidCrystal to use a polymorph 'class' declaration
1 parent 4f931b5 commit 74c486a

12 files changed

Lines changed: 170 additions & 126 deletions

File tree

docs/api/LiquidCrystal.md

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,67 @@ Derived from the Arduino LiquidCrystal library v1.8.0.
55

66

77

8+
## API
9+
10+
This library is a singleton library, it is not possible to use more than one
11+
instance per sketch.
12+
13+
The API syntax is very similar to the original C++ syntax, thanks to some
14+
[c preprocessor macro magic](macro.html).
15+
16+
Apart from the usual name mangeling for polymorph functions (mostly the
17+
different variants of the Print::print method) moving the opening bracket at
18+
the class declarator line and replacing the dots in the method names for
19+
underscores is all it needs.
20+
21+
22+
Arduino syntax |sduino syntax
23+
-------------------- |---------------------
24+
`LiquidCrystal lcd(rs,en,d0,d1,d2,d3)` |`LiquidCrystal (lcd,rs,en,d0,d1,d2,d3)`
25+
`LiquidCrystal lcd(rs,rw,en,d0,d1,d2,d3)` |`LiquidCrystal (lcd,rs,rw,en,d0,d1,d2,d3)`
26+
`LiquidCrystal lcd(rs,en,d0,d1,d2,d3,d4,d5,d6,d7)` |`LiquidCrystal (lcd,rs,en,d0,d1,d2,d3,d4,d5,d6,d7)`
27+
`LiquidCrystal lcd(rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)` |`LiquidCrystal (lcd,rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)`
28+
`lcd.init(rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)` |`lcd_init(mode,rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)`
29+
`lcd.begin(cols,lines)` |`lcd_begin(cols,lines)`
30+
`lcd.begin(cols,lines,charsize)` |`lcd_begin_charsize(cols,lines,charsize)`
31+
`lcd.clear()` |`lcd_clear()`
32+
`lcd.home()` |`lcd_home()`
33+
`lcd.noDisplay()` |`lcd_noDisplay()`
34+
`lcd.display()` |`lcd_display()`
35+
`lcd.noBlink()` |`lcd_noBlink()`
36+
`lcd.blink()` |`lcd_blink()`
37+
`lcd.noCursor()` |`lcd_noCursor()`
38+
`lcd.cursor()` |`lcd_cursor()`
39+
`lcd.scrollDisplayLeft()` |`lcd_scrollDisplayLeft()`
40+
`lcd.scrollDisplayRight()` |`lcd_scrollDisplayRight()`
41+
`lcd.leftToRight()` |`lcd_leftToRight()`
42+
`lcd.rightToLeft()` |`lcd_rightToLeft()`
43+
`lcd.noAutoscroll()` |`lcd_noAutoscroll()`
44+
`lcd.autoscroll()` |`lcd_autoscroll()`
45+
`lcd.setRowOffsets(row0,row1,row2,row3)`|`lcd_setRowOffsets(row0,row1,row2,row3)`
46+
`lcd.createChar(number, data[])` |`lcd_createChar(number, data[])`
47+
`lcd.setCursor(col,row)` |`lcd_setCursor(col,row)`
48+
`result = lcd.write(value)` |`result = lcd_write(value)`
49+
`lcd.command(value)` |`lcd_command(value)`
50+
51+
The LCD interface mode can be changed at run time by calling the
52+
`lcd_init(...)` function.
53+
54+
55+
856
## Example
957

1058
Output some Text and count the time since the last reset. Mind the
1159
slightly different position of the opening parenthesis at the "class
12-
constructor" function LiquidCrystal_4bit_r compared to the C++ instatiation.
60+
constructor" function LiquidCrystal compared to the C++ instatiation.
1361

1462
```c
1563
#include <Arduino.h>
1664
#include <LiquidCrystal.h>
1765

1866
// initialize the library with the numbers of the interface pins
19-
// The instance name "lcd" is _within_ the brackets
20-
LiquidCrystal_4bit_r (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
67+
// The instance name "lcd" is *within* the brackets
68+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
2169

2270
void setup() {
2371
lcd_begin(16, 2);
@@ -32,12 +80,12 @@ void loop() {
3280
```
3381
3482
35-
Original Arduino C++-Sytax:
83+
Compare it to the original Arduino C++-Sytax:
3684
```c
3785
#include <LiquidCrystal.h>
3886
3987
// initialize the library with the numbers of the interface pins
40-
// The instance name "lcd" is _before_ the brackets
88+
// The instance name "lcd" is *before* the brackets
4189
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
4290
4391
void setup() {
@@ -53,56 +101,10 @@ void loop() {
53101

54102

55103

56-
## API
57-
58-
This library is a singleton library, it is not possible to use more than one
59-
instance per sketch.
60-
61-
The API syntax is very similar to the original C++ syntax, thanks to some
62-
[c preprocessor macro magic](macro.html).
63-
Apart from the usual name mangeling for polymorph functions replacing the
64-
dots in the method names for underscores and a small modification of the
65-
initializer code should be enough.
66-
67-
The polymorph instantiation method is split into for different macros. Only
68-
one can be used per sketch. The choice for one of them has to be made at
69-
compile time. Changing the LCD mode at run time can be done by calling the
70-
`lcd_init(...)` function.
71-
72-
73-
Arduino syntax |sduino syntax
74-
-------------------- |---------------------
75-
`LiquidCrystal lcd(rs,en,d0,d1,d2,d3)` |`LiquidCrystal_4bit_r (lcd,rs,en,d0,d1,d2,d3)`
76-
`LiquidCrystal lcd(rs,rw,en,d0,d1,d2,d3)` |`LiquidCrystal_4bit_rw (lcd,rs,rw,en,d0,d1,d2,d3)`
77-
`LiquidCrystal lcd(rs,en,d0,d1,d2,d3,d4,d5,d6,d7)` |`LiquidCrystal_8bit_r (lcd,rs,en,d0,d1,d2,d3,d4,d5,d6,d7)`
78-
`LiquidCrystal lcd(rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)` |`LiquidCrystal_8bit_rw (lcd,rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)`
79-
`lcd.init(rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)`|`lcd_init(mode,rs,rw,en,d0,d1,d2,d3,d4,d5,d6,d7)`
80-
`lcd.begin(cols,lines)` |`lcd_begin(cols,lines)`
81-
`lcd.begin(cols,lines,charsize)` |`lcd_begin_charsize(cols,lines,charsize)`
82-
`lcd.clear()` |`lcd_clear()`
83-
`lcd.home()` |`lcd_home()`
84-
`lcd.noDisplay()` |`lcd_noDisplay()`
85-
`lcd.display()` |`lcd_display()`
86-
`lcd.noBlink()` |`lcd_noBlink()`
87-
`lcd.blink()` |`lcd_blink()`
88-
`lcd.noCursor()` |`lcd_noCursor()`
89-
`lcd.cursor()` |`lcd_cursor()`
90-
`lcd.scrollDisplayLeft()` |`lcd_scrollDisplayLeft()`
91-
`lcd.scrollDisplayRight()` |`lcd_scrollDisplayRight()`
92-
`lcd.leftToRight()` |`lcd_leftToRight()`
93-
`lcd.rightToLeft()` |`lcd_rightToLeft()`
94-
`lcd.noAutoscroll()` |`lcd_noAutoscroll()`
95-
`lcd.autoscroll()` |`lcd_autoscroll()`
96-
`lcd.setRowOffsets(row0,row1,row2,row3)`|`lcd_setRowOffsets(row0,row1,row2,row3)`
97-
`lcd.createChar(number, data[])` |`lcd_createChar(number, data[])`
98-
`lcd.setCursor(col,row)` |`lcd_setCursor(col,row)`
99-
`result = lcd.write(value)` |`result = lcd_write(value)`
100-
`lcd.command(value)` |`lcd_command(value)`
101-
102-
103-
104104
## Possible improvements
105105

106+
This is not a to-do-list, just a collection of random thoughts.
107+
106108
### Direct port access
107109
As long as the pin numberings are known at compile time it would be possible
108110
use direct port register access and the access pattern could be optimized if

sduino/hardware/sduino/stm8/cores/sduino/xmacro.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@
2626
19,18,17,16,15,14,13,12,11,10, \
2727
9,8,7,6,5,4,3,2,1,0
2828

29+
// a macro to generate a function call depending on the number of arguments.
30+
// Format of the function call: PREFIXn(...)
31+
// PREFIX is the first parameter to VARFUNC()
32+
// n is the number of arguments after PREFIX
33+
// example: 'VARFUNC(init,a,b,c)' expands to 'init3(a,b,c)'
34+
//
35+
// used for polymorph instantiation calls.
36+
#define VARFUNC(PREFIX,...) \
37+
PRE_NARG_(PREFIX,__VA_ARGS__,PP_RSEQ_N())(__VA_ARGS__)
38+
#define PRE_NARG_(PREFIX,...) \
39+
PRE_ARG_N(PREFIX,__VA_ARGS__)
40+
#define PRE_ARG_N(PREFIX, \
41+
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
42+
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
43+
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
44+
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
45+
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
46+
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
47+
_61,_62,_63,N,...) PREFIX##N
48+
49+
2950

3051

3152
/////// X-Macro like magic

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/Autoscroll/Autoscroll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include <LiquidCrystal.h>
4646

4747
// initialize the library with the numbers of the interface pins
48-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
48+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
4949

5050
void setup() {
5151
// set up the LCD's number of columns and rows:

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/Blink/Blink.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include <LiquidCrystal.h>
4646

4747
// initialize the library with the numbers of the interface pins
48-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
48+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
4949

5050
void setup() {
5151
// set up the LCD's number of columns and rows:

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/Cursor/Cursor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#include <LiquidCrystal.h>
4747

4848
// initialize the library with the numbers of the interface pins
49-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
49+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
5050

5151
void setup() {
5252
// set up the LCD's number of columns and rows:

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/CustomCharacter/CustomCharacter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
#include <LiquidCrystal.h>
4848

4949
// initialize the library with the numbers of the interface pins
50-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
50+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
5151

5252
// make some custom characters:
5353
byte heart[8] = {

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include <LiquidCrystal.h>
4545

4646
// initialize the library with the numbers of the interface pins
47-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
47+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
4848

4949
void setup() {
5050
// set up the LCD's number of columns and rows:

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/Scroll/Scroll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848

4949
// initialize the library with the numbers of the interface pins
50-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
50+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
5151

5252
void setup() {
5353
// set up the LCD's number of columns and rows:

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include <Serial.h>
4646

4747
// initialize the library with the numbers of the interface pins
48-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
48+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
4949

5050
void setup() {
5151
// set up the LCD's number of columns and rows:

sduino/hardware/sduino/stm8/libraries/LiquidCrystal/examples/TextDirection/TextDirection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This example code is in the public domain.
4545
#include <LiquidCrystal.h>
4646

4747
// initialize the library with the numbers of the interface pins
48-
LiquidCrystal_4bit_r(lcd,PA1,PA2, PA3,PD2,PD3,PD4);
48+
LiquidCrystal (lcd,PA1,PA2, PA3,PD2,PD3,PD4);
4949

5050
int thisChar = 'a';
5151

0 commit comments

Comments
 (0)