Guest User

1.2" 7-segment LED display, nodeMCU ESP8266, MAX7219 HT16K33

a guest
May 15th, 2016
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1. displays = { main = 0x70 }
  2.  
  3. function init( addresses, sda, scl )
  4.     i2c.setup( 0, sda, scl, i2c.SLOW )
  5.     for name, address in pairs( addresses ) do
  6.         write( address, 0x21 ) -- enable screen
  7.         setBrightness( address, 1 ) -- min brightness
  8.         output( address, "    " ) -- clear display
  9.     end
  10. end
  11.  
  12. function setBrightness( address, val ) -- 0 - off, 1 - 16 brightness level
  13.     if val < 1 then
  14.         write( address, 0x80 ) -- display "off"
  15.     else    
  16.         if val > 16 then val = 16 end
  17.         write( address, 0x81 ) -- display "on"
  18.         write( address, 0xE0 + val - 1 ) -- brightness 0 - 15
  19.     end
  20. end
  21.  
  22. function setBlink( address, yes )
  23.     local val = yes == true and 0x85 or 0x81
  24.     write( address, val ) -- blink or display "on"
  25. end
  26.  
  27. function output( address, txt ) -- pass exactly 4 char string
  28.     if txt:len() ~= 4 then return nil end
  29.     write( address, { 0, chr( txt:sub(1,1) ), 0, chr( txt:sub(2,2) ), 0, 0, 0, chr( txt:sub(3,3) ), 0, chr( txt:sub(4,4) ) } )
  30. end
  31.  
  32. function chr( x )
  33.     local c = {[" "]=0,["0"]=63,["1"]=6,["2"]=91,["3"]=79,["4"]=102,["5"]=109,["6"]=125,["7"]=7,["8"]=127,["9"]=111,b=124,o=92,t=120,n=84,c=88,Y=110,E=121,S=109,N=55,O=63,A=119,D=63,d=94,l=6,L=56,P=115,B=127,C=57,F=113,G=61,H=118,I=6,J=14,R=119,U=62,Z=91}
  34.     if c[x] == nil then return c[" "] end
  35.     return c[x]
  36. end
  37.  
  38. function write( address, data )
  39.     i2c.start( 0 ) -- it is always zero
  40.     i2c.address( 0, address, i2c.TRANSMITTER )
  41.     i2c.write( 0, data )
  42.     i2c.stop( 0 )
  43. end
  44.  
  45. init( displays, 1, 2 )
  46. output( displays["main"], "BLAH" )
Advertisement
Add Comment
Please, Sign In to add comment