Post your best Lua script examples here

User avatar
By dpwhittaker
#11863
alon24 wrote:thanks, now I have some work for tonight.

do I need to load the OLED files every time, in init, because before, if I did not load the OLED in init nothing would work(in the code I put here before)


flashMod(string) only works if everything on the module is a function. You were probably getting errors because some of your variables were missing. Notice I used dofile("oled.lc") to load the file that defines the variables that are not populated in init. So it does not need to be in init.lua.
User avatar
By dpwhittaker
#11870
Patriko wrote:Hi!

Thanks, I think that it'll solve the problem with ap scan, but it another hand other functions stopped working correctly, maybe I'm doing something wrong or there is a little bug with variables. Please take a look:

Code: Select allmod = { MOD_NAME = "led" }

mod.values   = {0,0,0}
mod.desired =  {0,0,0}

function mod.adjust()
     local change = false
     for i = 1, 3 do
          if mod.values[i] < mod.desired[i] then
               mod.values[i] = mod.values[i] + 1
               change = true
          elseif mod.values[i] > mod.desired[i] then
               mod.values[i] = mod.values[i] - 1
               change = true
          end
     end
     if change then
         
          ws2812.write(8, string.char(mod.values[2], mod.values[1], mod.values[3]):rep(10))
     end
end


function led.DesiredHex()
     return string.format("%02X%02X%02X", mod.desired[1], mod.desired[2], mod.desired[3])
end
function led.CurrentHex()
     return string.format("%02X%02X%02X", mod.values[1], mod.values[2], mod.values[3])
end
flashMod(mod)
return mod




First, you have CurrentHex and DesiredHex on led instead of mod, so when you execute them below, you are executing whatever the last version of them is that was on mod and got serialized to flash. You might want to delete led_*.lc to clean up old versions. Either way, put these functions on mod.

Patriko wrote:When I call:

Code: Select alll = flashMod("led")
print(l.CurrentHex())


I get an error:

Code: Select allled.lua:28: attempt to index field 'values' (a nil value)


It's looks like the flashMod is trying to put the variables into flash functions, isn't it?

Thanks,
Patriko


l = flashMod("led") creates an empty table with no variables that looks for files named led_CurrentHex.lc to execute when you call l.CurrentHex().

flashMod(mod) removes the functions from the mod table and stires them in flash, and adds the lookup mentioned above. So the variables are still there in this variant.

So, to fix your code, you have a few options:

1. Add an init function that you call first that populates the variables.

2. Change l = flashMod("led") to l = dofile("led.lua") to let the other file populate the variables... this will rewrite the functions to flash every time it is called, shortening the lifespan of your esp.

3. Split into flash and memory files, execute the flash file once, and dofile the memory file, similar to the oledv2 example.