Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
4e235d84
Commit
4e235d84
authored
Sep 24, 2015
by
devsaurus
Browse files
improve u8g lua examples
parent
f5ae0ed7
Changes
2
Hide whitespace changes
Inline
Side-by-side
lua_examples/u8glib/u8g_bitmaps.lua
View file @
4e235d84
...
@@ -61,35 +61,46 @@ function draw(draw_state)
...
@@ -61,35 +61,46 @@ function draw(draw_state)
end
end
function
bitmap_test
(
delay
)
function
bitmap_test
()
-- read XBM picture
dir
=
0
file
.
open
(
"u8glib_logo.xbm"
,
"r"
)
next_rotation
=
0
xbm_data
=
file
.
read
()
file
.
close
()
disp
:
firstPage
()
repeat
-- read Bitmap picture
draw
(
draw_state
)
file
.
open
(
"u8g_rook.bm"
,
"r"
)
until
disp
:
nextPage
()
==
false
bm_data
=
file
.
read
()
file
.
close
()
if
(
draw_state
<=
7
+
1
*
8
)
then
draw_state
=
draw_state
+
1
print
(
"--- Starting Bitmap Test ---"
)
else
dir
=
0
print
(
"--- Restarting Bitmap Test ---"
)
next_rotation
=
0
draw_state
=
1
end
local
draw_state
for
draw_state
=
1
,
7
+
1
*
8
,
1
do
print
(
"Heap: "
..
node
.
heap
())
disp
:
firstPage
()
-- retrigger timer to give room for system housekeeping
repeat
tmr
.
start
(
0
)
draw
(
draw_state
)
until
disp
:
nextPage
()
==
false
tmr
.
delay
(
delay
)
tmr
.
wdclr
()
end
print
(
"--- Bitmap Test done ---"
)
end
end
--init_i2c_display()
draw_state
=
1
init_spi_display
()
bitmap_test
(
50000
)
init_i2c_display
()
--init_spi_display()
-- read XBM picture
file
.
open
(
"u8glib_logo.xbm"
,
"r"
)
xbm_data
=
file
.
read
()
file
.
close
()
-- read Bitmap picture
file
.
open
(
"u8g_rook.bm"
,
"r"
)
bm_data
=
file
.
read
()
file
.
close
()
-- set up timer 0 with short interval, will be retriggered in graphics_test()
tmr
.
register
(
0
,
100
,
tmr
.
ALARM_SEMI
,
function
()
bitmap_test
()
end
)
print
(
"--- Starting Bitmap Test ---"
)
tmr
.
start
(
0
)
lua_examples/u8glib/u8g_graphics_test.lua
View file @
4e235d84
...
@@ -11,148 +11,155 @@
...
@@ -11,148 +11,155 @@
-- setup I2c and connect display
-- setup I2c and connect display
function
init_i2c_display
()
function
init_i2c_display
()
-- SDA and SCL can be assigned freely to available GPIOs
-- SDA and SCL can be assigned freely to available GPIOs
local
sda
=
5
-- GPIO14
local
sda
=
5
-- GPIO14
local
scl
=
6
-- GPIO12
local
scl
=
6
-- GPIO12
local
sla
=
0x3c
local
sla
=
0x3c
i2c
.
setup
(
0
,
sda
,
scl
,
i2c
.
SLOW
)
i2c
.
setup
(
0
,
sda
,
scl
,
i2c
.
SLOW
)
disp
=
u8g
.
ssd1306_128x64_i2c
(
sla
)
disp
=
u8g
.
ssd1306_128x64_i2c
(
sla
)
end
end
-- setup SPI and connect display
-- setup SPI and connect display
function
init_spi_display
()
function
init_spi_display
()
-- Hardware SPI CLK = GPIO14
-- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13
-- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used)
-- Hardware SPI MISO = GPIO12 (not used)
-- CS, D/C, and RES can be assigned freely to available GPIOs
-- CS, D/C, and RES can be assigned freely to available GPIOs
local
cs
=
8
-- GPIO15, pull-down 10k to GND
local
cs
=
8
-- GPIO15, pull-down 10k to GND
local
dc
=
4
-- GPIO2
local
dc
=
4
-- GPIO2
local
res
=
0
-- GPIO16
local
res
=
0
-- GPIO16
spi
.
setup
(
1
,
spi
.
MASTER
,
spi
.
CPOL_LOW
,
spi
.
CPHA_LOW
,
spi
.
DATABITS_8
,
0
)
spi
.
setup
(
1
,
spi
.
MASTER
,
spi
.
CPOL_LOW
,
spi
.
CPHA_LOW
,
spi
.
DATABITS_8
,
0
)
disp
=
u8g
.
ssd1306_128x64_hw_spi
(
cs
,
dc
,
res
)
disp
=
u8g
.
ssd1306_128x64_hw_spi
(
cs
,
dc
,
res
)
end
end
-- graphic test components
-- graphic test components
function
prepare
()
function
prepare
()
disp
:
setFont
(
u8g
.
font_6x10
)
disp
:
setFont
(
u8g
.
font_6x10
)
disp
:
setFontRefHeightExtendedText
()
disp
:
setFontRefHeightExtendedText
()
disp
:
setDefaultForegroundColor
()
disp
:
setDefaultForegroundColor
()
disp
:
setFontPosTop
()
disp
:
setFontPosTop
()
end
end
function
box_frame
(
a
)
function
box_frame
(
a
)
disp
:
drawStr
(
0
,
0
,
"drawBox"
)
disp
:
drawStr
(
0
,
0
,
"drawBox"
)
disp
:
drawBox
(
5
,
10
,
20
,
10
)
disp
:
drawBox
(
5
,
10
,
20
,
10
)
disp
:
drawBox
(
10
+
a
,
15
,
30
,
7
)
disp
:
drawBox
(
10
+
a
,
15
,
30
,
7
)
disp
:
drawStr
(
0
,
30
,
"drawFrame"
)
disp
:
drawStr
(
0
,
30
,
"drawFrame"
)
disp
:
drawFrame
(
5
,
10
+
30
,
20
,
10
)
disp
:
drawFrame
(
5
,
10
+
30
,
20
,
10
)
disp
:
drawFrame
(
10
+
a
,
15
+
30
,
30
,
7
)
disp
:
drawFrame
(
10
+
a
,
15
+
30
,
30
,
7
)
end
end
function
disc_circle
(
a
)
function
disc_circle
(
a
)
disp
:
drawStr
(
0
,
0
,
"drawDisc"
)
disp
:
drawStr
(
0
,
0
,
"drawDisc"
)
disp
:
drawDisc
(
10
,
18
,
9
)
disp
:
drawDisc
(
10
,
18
,
9
)
disp
:
drawDisc
(
24
+
a
,
16
,
7
)
disp
:
drawDisc
(
24
+
a
,
16
,
7
)
disp
:
drawStr
(
0
,
30
,
"drawCircle"
)
disp
:
drawStr
(
0
,
30
,
"drawCircle"
)
disp
:
drawCircle
(
10
,
18
+
30
,
9
)
disp
:
drawCircle
(
10
,
18
+
30
,
9
)
disp
:
drawCircle
(
24
+
a
,
16
+
30
,
7
)
disp
:
drawCircle
(
24
+
a
,
16
+
30
,
7
)
end
end
function
r_frame
(
a
)
function
r_frame
(
a
)
disp
:
drawStr
(
0
,
0
,
"drawRFrame/Box"
)
disp
:
drawStr
(
0
,
0
,
"drawRFrame/Box"
)
disp
:
drawRFrame
(
5
,
10
,
40
,
30
,
a
+
1
)
disp
:
drawRFrame
(
5
,
10
,
40
,
30
,
a
+
1
)
disp
:
drawRBox
(
50
,
10
,
25
,
40
,
a
+
1
)
disp
:
drawRBox
(
50
,
10
,
25
,
40
,
a
+
1
)
end
end
function
stringtest
(
a
)
function
stringtest
(
a
)
disp
:
drawStr
(
30
+
a
,
31
,
" 0"
)
disp
:
drawStr
(
30
+
a
,
31
,
" 0"
)
disp
:
drawStr90
(
30
,
31
+
a
,
" 90"
)
disp
:
drawStr90
(
30
,
31
+
a
,
" 90"
)
disp
:
drawStr180
(
30
-
a
,
31
,
" 180"
)
disp
:
drawStr180
(
30
-
a
,
31
,
" 180"
)
disp
:
drawStr270
(
30
,
31
-
a
,
" 270"
)
disp
:
drawStr270
(
30
,
31
-
a
,
" 270"
)
end
end
function
line
(
a
)
function
line
(
a
)
disp
:
drawStr
(
0
,
0
,
"drawLine"
)
disp
:
drawStr
(
0
,
0
,
"drawLine"
)
disp
:
drawLine
(
7
+
a
,
10
,
40
,
55
)
disp
:
drawLine
(
7
+
a
,
10
,
40
,
55
)
disp
:
drawLine
(
7
+
a
*
2
,
10
,
60
,
55
)
disp
:
drawLine
(
7
+
a
*
2
,
10
,
60
,
55
)
disp
:
drawLine
(
7
+
a
*
3
,
10
,
80
,
55
)
disp
:
drawLine
(
7
+
a
*
3
,
10
,
80
,
55
)
disp
:
drawLine
(
7
+
a
*
4
,
10
,
100
,
55
)
disp
:
drawLine
(
7
+
a
*
4
,
10
,
100
,
55
)
end
end
function
triangle
(
a
)
function
triangle
(
a
)
local
offset
=
a
local
offset
=
a
disp
:
drawStr
(
0
,
0
,
"drawTriangle"
)
disp
:
drawStr
(
0
,
0
,
"drawTriangle"
)
disp
:
drawTriangle
(
14
,
7
,
45
,
30
,
10
,
40
)
disp
:
drawTriangle
(
14
,
7
,
45
,
30
,
10
,
40
)
disp
:
drawTriangle
(
14
+
offset
,
7
-
offset
,
45
+
offset
,
30
-
offset
,
57
+
offset
,
10
-
offset
)
disp
:
drawTriangle
(
14
+
offset
,
7
-
offset
,
45
+
offset
,
30
-
offset
,
57
+
offset
,
10
-
offset
)
disp
:
drawTriangle
(
57
+
offset
*
2
,
10
,
45
+
offset
*
2
,
30
,
86
+
offset
*
2
,
53
)
disp
:
drawTriangle
(
57
+
offset
*
2
,
10
,
45
+
offset
*
2
,
30
,
86
+
offset
*
2
,
53
)
disp
:
drawTriangle
(
10
+
offset
,
40
+
offset
,
45
+
offset
,
30
+
offset
,
86
+
offset
,
53
+
offset
)
disp
:
drawTriangle
(
10
+
offset
,
40
+
offset
,
45
+
offset
,
30
+
offset
,
86
+
offset
,
53
+
offset
)
end
end
function
ascii_1
()
function
ascii_1
()
local
x
,
y
,
s
local
x
,
y
,
s
disp
:
drawStr
(
0
,
0
,
"ASCII page 1"
)
disp
:
drawStr
(
0
,
0
,
"ASCII page 1"
)
for
y
=
0
,
5
,
1
do
for
y
=
0
,
5
,
1
do
for
x
=
0
,
15
,
1
do
for
x
=
0
,
15
,
1
do
s
=
y
*
16
+
x
+
32
s
=
y
*
16
+
x
+
32
disp
:
drawStr
(
x
*
7
,
y
*
10
+
10
,
string.char
(
s
))
disp
:
drawStr
(
x
*
7
,
y
*
10
+
10
,
string.char
(
s
))
end
end
end
end
end
end
function
extra_page
(
a
)
function
extra_page
(
a
)
disp
:
drawStr
(
0
,
12
,
"setScale2x2"
)
disp
:
drawStr
(
0
,
12
,
"setScale2x2"
)
disp
:
setScale2x2
()
disp
:
setScale2x2
()
disp
:
drawStr
(
0
,
6
+
a
,
"setScale2x2"
)
disp
:
drawStr
(
0
,
6
+
a
,
"setScale2x2"
)
disp
:
undoScale
()
disp
:
undoScale
()
end
end
-- the draw() routine
-- the draw() routine
function
draw
(
draw_state
)
function
draw
(
draw_state
)
local
component
=
bit
.
rshift
(
draw_state
,
3
)
local
component
=
bit
.
rshift
(
draw_state
,
3
)
prepare
()
prepare
()
if
(
component
==
0
)
then
if
(
component
==
0
)
then
box_frame
(
bit
.
band
(
draw_state
,
7
))
box_frame
(
bit
.
band
(
draw_state
,
7
))
elseif
(
component
==
1
)
then
elseif
(
component
==
1
)
then
disc_circle
(
bit
.
band
(
draw_state
,
7
))
disc_circle
(
bit
.
band
(
draw_state
,
7
))
elseif
(
component
==
2
)
then
elseif
(
component
==
2
)
then
r_frame
(
bit
.
band
(
draw_state
,
7
))
r_frame
(
bit
.
band
(
draw_state
,
7
))
elseif
(
component
==
3
)
then
elseif
(
component
==
3
)
then
stringtest
(
bit
.
band
(
draw_state
,
7
))
stringtest
(
bit
.
band
(
draw_state
,
7
))
elseif
(
component
==
4
)
then
elseif
(
component
==
4
)
then
line
(
bit
.
band
(
draw_state
,
7
))
line
(
bit
.
band
(
draw_state
,
7
))
elseif
(
component
==
5
)
then
elseif
(
component
==
5
)
then
triangle
(
bit
.
band
(
draw_state
,
7
))
triangle
(
bit
.
band
(
draw_state
,
7
))
elseif
(
component
==
6
)
then
elseif
(
component
==
6
)
then
ascii_1
()
ascii_1
()
elseif
(
component
==
7
)
then
elseif
(
component
==
7
)
then
extra_page
(
bit
.
band
(
draw_state
,
7
))
extra_page
(
bit
.
band
(
draw_state
,
7
))
end
end
end
end
function
graphics_test
(
delay
)
function
graphics_test
()
print
(
"--- Starting Graphics Test ---"
)
disp
:
firstPage
()
-- cycle through all components
repeat
local
draw_state
draw
(
draw_state
)
for
draw_state
=
0
,
7
+
8
*
8
,
1
do
until
disp
:
nextPage
()
==
false
disp
:
firstPage
()
repeat
if
(
draw_state
<=
7
+
8
*
8
)
then
draw
(
draw_state
)
draw_state
=
draw_state
+
1
until
disp
:
nextPage
()
==
fa
lse
e
lse
--
print(
node.heap()
)
print
(
"--- Restarting Graphics Test ---"
)
tmr
.
delay
(
delay
)
draw_state
=
0
-- re-trigger Watchdog!
end
tmr
.
wdclr
()
end
print
(
"Heap: "
..
node
.
heap
())
-- retrigger timer to give room for system housekeeping
print
(
"--- Graphics Test done ---"
)
tmr
.
start
(
0
)
end
end
--init_i2c_display()
draw_state
=
0
init_spi_display
()
graphics_test
(
50000
)
init_i2c_display
()
--init_spi_display()
-- set up timer 0 with short interval, will be retriggered in graphics_test()
tmr
.
register
(
0
,
100
,
tmr
.
ALARM_SEMI
,
function
()
graphics_test
()
end
)
print
(
"--- Starting Graphics Test ---"
)
tmr
.
start
(
0
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment