Created
December 13, 2016 03:15
-
-
Save dotxnc/48643236912156014fd07a53d02d3e1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/luajit | |
local wpi = require ("wiringlua") | |
local ADDR = 0x27 | |
local BLEN = 1 | |
local fd = 0 | |
function write_word(data) | |
local temp = data | |
if BLEN == 1 then | |
temp = bit.bor(temp, 0x08) | |
else | |
temp = bit.band(temp, 0xF7) | |
end | |
wpi.i2c.write(fd, temp) | |
end | |
function send_command(comm) | |
local buf | |
buf = bit.band(comm, 0xF0) | |
buf = bit.bor(buf, 0x04) | |
write_word(buf) | |
wpi.timer.delay(2) | |
buf = bit.band(buf, 0xFB) | |
write_word(buf) | |
buf = bit.lshift(bit.band(comm, 0x0F), 4) | |
buf = bit.bor(buf, 0x04) | |
write_word(buf) | |
wpi.timer.delay(2) | |
buf = bit.band(buf, 0xFB) | |
write_word(buf) | |
end | |
function send_data(data) | |
local buf | |
buf = bit.band(data, 0xF0) | |
buf = bit.bor(buf, 0x05) | |
write_word(buf) | |
wpi.timer.delay(2) | |
buf = bit.band(buf, 0xFB) | |
write_word(buf) | |
buf = bit.lshift(bit.band(data, 0x0F), 4) | |
buf = bit.bor(buf, 0x05) | |
write_word(buf) | |
wpi.timer.delay(2) | |
buf = bit.band(buf, 0xFB) | |
write_word(buf) | |
end | |
function init() | |
send_command(0x33) | |
wpi.timer.delay(5) | |
send_command(0x32) | |
wpi.timer.delay(5) | |
send_command(0x28) | |
wpi.timer.delay(5) | |
send_command(0x0C) | |
wpi.timer.delay(5) | |
send_command(0x01) | |
wpi.i2c.write(fd, 0x08) | |
end | |
function clear() | |
send_command(0x01) | |
end | |
function write(x, y, string) | |
local addr, tmp | |
if x < 0 then x = 0 end | |
if x > 15 then x = 15 end | |
if y < 0 then y = 0 end | |
if y > 1 then y = 1 end | |
addr = 0x80 + 0x40 * y + x | |
send_command(addr) | |
tmp = #string | |
for i=1,#string do | |
send_data(string.byte(string:sub(i,i))) | |
end | |
end | |
fd = wpi.i2c.setup(ADDR) | |
init() | |
write(0, 0, "HELLO FROM") | |
write(9, 1, "LUAJIT!") | |
print("FD = " .. fd) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment