|
@@ -22,27 +22,27 @@ return function(bus_args)
|
|
|
-- The onus is on us to maintain the backlight state
|
|
|
local backlight = true
|
|
|
|
|
|
- local function send4bitI2C(value, rs_en, rw_en, read)
|
|
|
- local function exchange(data, unset_read)
|
|
|
- local rv = data
|
|
|
+ local function exchange(data, read)
|
|
|
+ local rv = data
|
|
|
+ i2c.start(busid)
|
|
|
+ i2c.address(busid, busad, i2c.TRANSMITTER)
|
|
|
+ i2c.write(busid, bit.set(data, en)) -- set data with en
|
|
|
+ if read then
|
|
|
+ i2c.start(busid) -- read 1 byte and go back to tx mode
|
|
|
+ i2c.address(busid, busad, i2c.RECEIVER)
|
|
|
+ rv = i2c.read(busid, 1):byte(1)
|
|
|
i2c.start(busid)
|
|
|
i2c.address(busid, busad, i2c.TRANSMITTER)
|
|
|
- i2c.write(busid, bit.set(data, en))
|
|
|
- if read then
|
|
|
- i2c.start(busid)
|
|
|
- i2c.address(busid, busad, i2c.RECEIVER)
|
|
|
- rv = i2c.read(busid, 1):byte(1)
|
|
|
- i2c.start(busid)
|
|
|
- i2c.address(busid, busad, i2c.TRANSMITTER)
|
|
|
- if unset_read then data = bit.bor(bit.bit(rs),
|
|
|
- bit.bit(rw),
|
|
|
- backlight and bit.bit(bl) or 0) end
|
|
|
- i2c.write(busid, bit.set(data, en))
|
|
|
- end
|
|
|
- i2c.write(busid, bit.clear(data, en))
|
|
|
- i2c.stop(busid)
|
|
|
- return rv
|
|
|
end
|
|
|
+ i2c.write(busid, data) -- lower en
|
|
|
+ i2c.stop(busid)
|
|
|
+ return rv
|
|
|
+ end
|
|
|
+
|
|
|
+ local function send4bitI2C(value, rs_en, rw_en)
|
|
|
+ local meta = bit.bor(rs_en and bit.bit(rs) or 0,
|
|
|
+ rw_en and bit.bit(rw) or 0,
|
|
|
+ backlight and bit.bit(bl) or 0)
|
|
|
local lo = bit.bor(bit.isset(value, 0) and bit.bit(d4) or 0,
|
|
|
bit.isset(value, 1) and bit.bit(d5) or 0,
|
|
|
bit.isset(value, 2) and bit.bit(d6) or 0,
|
|
@@ -51,11 +51,8 @@ return function(bus_args)
|
|
|
bit.isset(value, 5) and bit.bit(d5) or 0,
|
|
|
bit.isset(value, 6) and bit.bit(d6) or 0,
|
|
|
bit.isset(value, 7) and bit.bit(d7) or 0)
|
|
|
- local cmd = bit.bor(rs_en and bit.bit(rs) or 0,
|
|
|
- rw_en and bit.bit(rw) or 0,
|
|
|
- backlight and bit.bit(bl) or 0)
|
|
|
- hi = exchange(bit.bor(cmd, hi), false)
|
|
|
- lo = exchange(bit.bor(cmd, lo), true)
|
|
|
+ hi = exchange(bit.bor(meta, hi), rw_en)
|
|
|
+ lo = exchange(bit.bor(meta, lo), rw_en)
|
|
|
return bit.bor(bit.lshift(bit.isset(lo, d4) and 1 or 0, 0),
|
|
|
bit.lshift(bit.isset(lo, d5) and 1 or 0, 1),
|
|
|
bit.lshift(bit.isset(lo, d6) and 1 or 0, 2),
|
|
@@ -66,36 +63,45 @@ return function(bus_args)
|
|
|
bit.lshift(bit.isset(hi, d7) and 1 or 0, 7))
|
|
|
end
|
|
|
|
|
|
- -- init sequence from datasheet
|
|
|
- send4bitI2C(0x33, false, false, false)
|
|
|
- send4bitI2C(0x32, false, false, false)
|
|
|
+ -- init sequence from datasheet (Figure 24)
|
|
|
+ local function justsend(what)
|
|
|
+ i2c.start(busid)
|
|
|
+ i2c.address(busid, busad, i2c.TRANSMITTER)
|
|
|
+ i2c.write(busid, bit.set(what, en))
|
|
|
+ i2c.write(busid, what)
|
|
|
+ i2c.stop(busid)
|
|
|
+ end
|
|
|
+ local three = bit.bor(bit.bit(d4), bit.bit(d5))
|
|
|
+ justsend(three)
|
|
|
+ tmr.delay(5)
|
|
|
+ justsend(three)
|
|
|
+ tmr.delay(1)
|
|
|
+ justsend(three)
|
|
|
+ tmr.delay(1)
|
|
|
+ justsend(bit.bit(d5))
|
|
|
+ -- we are now primed for the FUNCTIONSET command from the liquidcrystal ctor
|
|
|
|
|
|
-- Return backend object
|
|
|
return {
|
|
|
fourbits = true,
|
|
|
command = function (_, cmd)
|
|
|
- return send4bitI2C(cmd, false, false, false)
|
|
|
+ return send4bitI2C(cmd, false, false)
|
|
|
end,
|
|
|
busy = function(_)
|
|
|
- local rv = send4bitI2C(0xff, false, true, true)
|
|
|
- send4bitI2C(bit.bor(0x80, bit.clear(rv, 7)), false, false, false)
|
|
|
- return bit.isset(rv, 7)
|
|
|
+ return bit.isset(send4bitI2C(0xff, false, true), 7)
|
|
|
end,
|
|
|
position = function(_)
|
|
|
- local rv = bit.clear(send4bitI2C(0xff, false, true, true), 7)
|
|
|
- send4bitI2C(bit.bor(0x80, rv), false, false, false)
|
|
|
- return rv
|
|
|
+ return bit.clear(send4bitI2C(0xff, false, true), 7)
|
|
|
end,
|
|
|
write = function(_, value)
|
|
|
- return send4bitI2C(value, true, false, false)
|
|
|
+ return send4bitI2C(value, true, false)
|
|
|
end,
|
|
|
read = function(_)
|
|
|
- return send4bitI2C(0xff, true, true, true)
|
|
|
+ return send4bitI2C(0xff, true, true)
|
|
|
end,
|
|
|
backlight = function(_, on)
|
|
|
backlight = on
|
|
|
- local rv = bit.clear(send4bitI2C(0xff, false, true, true), 7)
|
|
|
- send4bitI2C(bit.bor(0x80, rv), false, false, false)
|
|
|
+ send4bitI2C(0, false, false) -- No-op
|
|
|
return on
|
|
|
end,
|
|
|
}
|