Page 4 of 12

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Wed Mar 11, 2015 6:54 pm
by dpwhittaker
You have a few places that are missing "mod." in your init functions. spi, oled_sda, and oled_scl all need it. You also need it on spi and dc in several other functions. Btw, they can only see it because it is global, I would declare it local, switch to colon syntax, and use self instead of mod, so the mod is passed as its implicit self parameter.

I only get a couple nights a week to play with this stuff, but will take a closercloser look at the next opportunity.

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Thu Mar 12, 2015 12:03 am
by dpwhittaker
I added oledv2.zip to the first post. This exemplifies most of what I talked about in the previous post.

I added i2c to it, though it may not work at all as I have no way to test it. That pushed me over the maximum limit of what the interpreter can handle in a single file, so I broke it up into 3:

1. oled_init.lua - holds the initialization and i2c/spi specific code. Notice the init functions load the flash functions into memory and keep them there (while also choosing whether to load the spi or i2c version). That is what
Code: Select allself.data = self.data_spi
and similar is accomplishing. This is one way to create an in-memory function, but realize it has the same access limitations as flash functions, which is basically that no variables local to the file are available. Its scope includes its parameters, its own local variables, and global.

2. oled_func.lua - holds the flash functions that actually define the interface of the module. The division of these two files was rather arbitrary, you can have as little as one function in each file, or as many as you can fit and still load the file.

3. oled.lua - this is the actual entry point to the module. This is the other way to define in-memory functions and data (other than the init method mentioned above).

So, to use this module, you would execute oled_init.lua and oled_func.lua once, then you can delete them until you change them. The persisted version of the functions will remain in flash until they are needed. Then the
Code: Select alllocal oled = flashMod("oled")
in oled.lua will create the proxy module that will load those functions as needed, and add the additional functions and data that need to stay in memory.

Finally, as in test.lua, you call
Code: Select alllocal oled = dofile("oled.lc")
to load the complete package from a compiled oled.lua.

Notice that I switched to the format
Code: Select allfunction oled:funcName(...)
to define the function. In case you didn't know, this is just syntactic sugar
Code: Select allfunction oled.funcName(self, ...)
. Likewise, when I call functions on oled, I call them like
Code: Select alloled:funcName(...)
, which is syntactic sugar for
Code: Select alloled.funcName(oled, ...)
. This is what makes oled available on the self parameter within methods.

Hopefully you can get the i2c part working from this base.

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Thu Mar 12, 2015 1:11 am
by alon24
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)

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Thu Mar 12, 2015 2:59 am
by Patriko

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
     if change then
          ws2812.write(8, string.char(mod.values[2], mod.values[1], mod.values[3]):rep(10))

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

When I call:

Code: Select alll = flashMod("led")

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?