- Wed Mar 18, 2015 2:38 pm
#12208
Hi, thanks for your reply.
Ive found other way to save a lot of heap, not using mod flash but, I learned usefull information about heap/flash thanks to your post's
Here are use results:
tcs_low.lc 2132 bytes
> node.heap()
> =node.heap()
22824
> require("tcs_low")
> =node.heap()
18592
As I see compared to other way that I used first I've saved about 5KB of heap space.
The secret is simple, only replaced static variables with respective value
This discussion post's lead me to learn that nodemcu lua uses a lot of heap memory for declaring variables, for example
> = node.heap()
18088
> t=1
> = node.heap()
18024
heap consumed per variable is about 64Bytes
but if i do
> = node.heap()
18024
> t=12345678901234567890
> = node.heap()
18024
This means that variable t allocates the same 64Bytes, suppose it can handle 64characters..
And about strings:
> = node.heap()
18024
> t="12345678901234567890"
> = node.heap()
18008
>
> t=12345678901234567890
> = node.heap()
18024
Ok, this means that the same string is eating more 16Bytes of heap for the same content.
So when programming on microcontrolers with limited resources using high level language lua you should declare variables only if really necessary, otherwise they are wasting a lot of space in heap.
If varible is dynamic so make sure u clean it after use, variable equal to nil.
So, if my thoughts are correct mod flash will put this variables into flash memory, releasing that heap space.
So need to have one precaution when using this mod flash, if there are some variables that are updated often this means writting to flash many times, and as we know flash memory have self life of some few thousands write operations.For exaple if u update this variable once a second means 86 400 writes/a day. Maybe enough to kill flash..
So this mod flash technik should be used only for static variables or the called #define
Other solution could be creating a file with static variables and read line index defined in a table to correspondent "variable" and insert that value directly into code dynamicaly.
The code for tcs34725 from Craig Scott is great and functional but has 5Kbytes allocated just for variables declaration. I just simplified the code with almost no declarations:
function initialise()
i2c.setup(0,3,4,i2c.SLOW)
if (string.byte(read_reg(0x29,0x12))==0x44) then
print("Found TCS34725")
end
end
function read_reg(dev_addr, reg_addr)
i2c.start(0)
i2c.address(0, dev_addr ,i2c.TRANSMITTER)
i2c.write(0,bit.bor(0x80,reg_addr))
i2c.stop(0)
i2c.start(0)
i2c.address(0, dev_addr,i2c.RECEIVER)
c=i2c.read(0,1)
i2c.stop(0)
return c
end
function write_reg(dev_addr, reg_addr, reg_val)
i2c.start(0)
i2c.address(0, dev_addr, i2c.TRANSMITTER)
i2c.write(0, bit.bor(0x80,reg_addr))
i2c.write(0, reg_val)
i2c.stop(0)
end
function getRawData ()
clear=bit.bor(bit.lshift(string.byte(read_reg(0x29,0x15)),8),string.byte(read_reg(0x29,0x14)))
red=bit.bor(bit.lshift(string.byte(read_reg(0x29,0x17)),8),string.byte(read_reg(0x29,0x16)))
green=bit.bor(bit.lshift(string.byte(read_reg(0x29,0x19)),8),string.byte(read_reg(0x29,0x18)))
blue=bit.bor(bit.lshift(string.byte(read_reg(0x29,0x1B)),8),string.byte(read_reg(0x29,0x1A)))
tmr.delay(100)
print(red,green,blue,clear)
red,green,blue,clear = nil,nil,nil,nil
end
function enable()
write_reg(0x29,0x00,0x01)
tmr.delay(30)
write_reg(0x29, 0x00, bit.bor(0x01,0x02))
end
function disable()
tmr.delay(3)
write_reg(0x29,0x00, bit.band(read_reg(0x29,0x00), bit.bnot(bit.bor(0x01,0x02))))
end
--Interaction with module after require("tcs34725") is:
--initialise(0x29)
--write_reg(0x29,0x01, 0xEB)
--write_reg(0x29,0x0F, 0x00)
--enable()
--getRawData()
The code is now fully functional but less 5KBytes used by heap
This is a lesson for all, remember that u are developing in a low resouce machine, all code must be optimized to do less operations and save RAM/FLASH memory .
Regards,Vito