Skip to content

Instantly share code, notes, and snippets.

@asterix24
Created March 24, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asterix24/224ed5be8dd574e91eb147945722d8fd to your computer and use it in GitHub Desktop.
Save asterix24/224ed5be8dd574e91eb147945722d8fd to your computer and use it in GitHub Desktop.
Lua nodeMCU TM1640 Matrix LED 8x8 for wemos D1
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-- Copyright 2019 Daniele Basile <asterix24@gmail.com>
-- Author Daniele Basile <asterix24@gmail.com>
local Tm1640 = { }
local MOSI = 7
local SCLK = 5
local BITDELAY = 10
local TM1640_CMD1 = 0x40 -- data command
local TM1640_CMD2 = 0xC0 -- address command
local TM1640_CMD3 = 0x80 -- display control command
local TM1640_DSP_ON = 0x08 -- display on
function send_start()
gpio.write(MOSI, gpio.LOW)
tmr.delay(BITDELAY)
gpio.write(SCLK, gpio.LOW)
tmr.delay(BITDELAY)
end
function send_end()
gpio.write(MOSI, gpio.LOW)
tmr.delay(BITDELAY)
gpio.write(SCLK, gpio.HIGH)
tmr.delay(BITDELAY)
gpio.write(MOSI, gpio.HIGH)
end
function write_byte(data, len)
for i = 0, (len * 8) - 1, 1 do
if bit.isset(data, i) then
gpio.write(MOSI, gpio.HIGH)
else
gpio.write(MOSI, gpio.LOW)
end
tmr.delay(BITDELAY)
gpio.write(SCLK, gpio.HIGH)
tmr.delay(BITDELAY)
gpio.write(SCLK, gpio.LOW)
tmr.delay(BITDELAY)
end
end
function cmd()
send_start()
write_byte(TM1640_CMD1, 1)
send_end()
end
function dsp_ctrl(brightness)
send_start()
write_byte(bit.bor(TM1640_CMD3, TM1640_DSP_ON, brightness), 1)
send_end()
end
function Tm1640.off(brightness)
send_start()
write_byte(bit.bor(TM1640_CMD3), 1)
send_end()
end
function Tm1640.brightness(val)
cmd()
dsp_ctrl(val)
end
function Tm1640.write(rows)
cmd()
send_start()
write_byte(TM1640_CMD2, 1)
for i,v in ipairs(rows) do
write_byte(v, 1)
end
send_end()
end
function Tm1640.write_pos(rows, pos)
cmd()
send_start()
write_byte(bit.bor(TM1640_CMD2, pos), 1)
for i,v in ipairs(rows) do
write_byte(v, 1)
end
send_end()
end
function Tm1640.init(mosi, sclk)
MOSI = mosi
SCLK = sclk
gpio.mode(MOSI, gpio.OUTPUT)
gpio.mode(SCLK, gpio.OUTPUT)
gpio.write(MOSI, gpio.LOW)
gpio.write(SCLK, gpio.LOW)
tmr.delay(BITDELAY)
cmd()
dsp_ctrl(0)
end
return Tm1640
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment