Thursday, March 10, 2016

ESP12e oled "smartwatch" eLua source code


This code is from project: ESP12e oled "smartwatch", honestly it's a wrist display ;)

init.lua

node.setcpufreq(node.CPU80MHZ)

dofile("display_init.lc")
dofile("display_body.lc")
dofile("wireless.lc")
dofile("server.lc")
dofile("gpio.lc")

display_init.lua

spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
disp = u8g.ssd1306_128x64_hw_spi(8, 6, 0)

function prep()
   disp:setFont(u8g.font_6x10)
   disp:setFontRefHeightExtendedText()
   disp:setDefaultForegroundColor()
   disp:setFontPosTop()
end

ip = "none"
mac = "none"
ssid = "none"
l0= ""
l1= ""
l2= ""
l3= ""
l4= ""
l5= ""

stat_screen = 1;

-- range 123456789012345678901 21 chars

function status_toggle()
   if (stat_screen == 1) then
      stat_screen = 0
   else
      stat_screen = 1
   end
end

function lines()
   disp:drawStr(0, 0, l0)
   disp:drawStr(0, 10, l1)
   disp:drawStr(0, 20, l2)
   disp:drawStr(0, 30, l3)
   disp:drawStr(0, 40, l4)
   disp:drawStr(0, 50, l5)
end

function clear_lines()
   l0=""
   l1=""
   l2=""
   l3=""
   l4=""
   l5=""
end

tmr.register(0, 1000, tmr.ALARM_SEMI, function() image_loop() end)
tmr.start(0)

display_body.lua

function status()
   disp:drawStr(0, 0, "IP: "..ip)
   disp:drawStr(0, 10, "SSID: "..ssid)
   disp:drawStr(0, 20, "Bat/Uptime: "..adc.read(0).."/"..tmr.time().."s")
   disp:drawStr(0, 30, "Heap: "..node.heap())

   for mac,ip in pairs(wifi.ap.getclient()) do
      disp:drawStr(0, 40, "Cli IP: "..ip)
      disp:drawStr(0, 50, "MAC "..mac)
   end
end

prep()
function image_loop()
   disp:firstPage()

   repeat
      if (stat_screen == 1) then
         status()
      else
         lines()
      end
   until disp:nextPage() == false

   tmr.start(0)
end

wireless.lua

wifi.sleeptype(wifi.MODEM_SLEEP)

wifi.setmode(wifi.SOFTAP)

cfg={}
cfg.ssid="esp_watch"
cfg.pwd="esp123123"
cfg.auth=AUTH_WPA2_PSK
cfg.max=1
wifi.ap.config(cfg)

ip = wifi.ap.getip()
mac = wifi.ap.getmac()
ssid = cfg.ssid

server.lua

s = net.createServer(net.TCP, 1)
s:listen(80, function(c)
    c:on("receive", function(c, pl) 
       node.input(pl)
       c:close()
    end)
end)

gpio.lua

gpio.mode(2, gpio.OUTPUT)
gpio.mode(1, gpio.INT, gpio.PULLUP)
gpio.mode(3, gpio.INT, gpio.PULLUP)

function led_on(level)
   gpio.write(2, gpio.LOW)
end

function led_off(level)
   gpio.write(2, gpio.HIGH)
end

led_status = 0;

tmr.register(1, 250, tmr.ALARM_AUTO,
   function()
      if (led_status == 1) then
         led_off()
         led_status = 0
      else 
         led_on()
         led_status = 1
      end
   end)

function notify_on()
   tmr.start(1)
end

function notify_off()
   tmr.stop(1)
   led_off()
   led_status = 0
end



notify_off()
gpio.trig(1, "down", status_toggle)
gpio.trig(3, "down", notify_off)

No comments:

Post a Comment