Taiko Code Tips
On this page we will share tips and interesting ideas related to programming with TBASIC. The content of this page is not limited to our own, if you have any interesting ideas or experiences related to programming with TBASIC that you would like to share with others, please send us an email at support@tibbo.com
Efficient use of storage space - 12 November 2007
Coming from PC based programming background, sometimes our coding habits becomes less efficient when dealing with storage space. This creates a problem of running out of memory when large amount of data (in term of an embedded device) needs to be stored and every byte of storage space counts!
Take for example, when we read / write strings to the EEPROM memory space using the "stor" object, we would have something like the following:
dim x as string
stor.setdata("142",1)
x = stor.getdata(1,3)
The above will store the string 142 into the first position of the EEPROM and we will retrieve this value as a string.So, what if when we need to store it as a byte instead of string? Most of us would probably just do it like this:
dim x as byte stor.setdata(142,1) x = val(stor.getdata(1,3))That would work fine, but notice that it is still taking up 3 bytes of memory space; there is a more memory efficient way to do this:
dim x as byte stor.setdata(chr(142),1) x = asc(stor.getdata(1,1))Now, instead of taking but 3 bytes of storage space, we only use 1 byte by first converting 142 into the ASCII representation before we store it into the EEPROM. When we retrieve back the data, we just convert it back to its original number.
Using sock.redir with UDP - 16 OCTOBER 2007
On some of the platforms you can speed up the data transfer by using a technique called buffer redirection, which means that data bypasses the RX buffer and goes directly to the TX buffer of another object. This works great, but with one exception, which is when you are redirecting from a socket that is using UDP, which will generate what seems like "junk data".
The reason behind this is that since UDP packets has header information containing the size of the packet, without being processed by the BASIC program, the header is sent together with the data to the destination object's TX buffer. So, when working with UDP, please use the on_sock_data_arrival event handler.
Instead of using redirection like this:
sock.num = 0 sock.redir(PL_REDIR_SER0)Change it to this:
sock.num = 0 ser.num = 0 ser.setdata(sock.getdata(ser.txfree-ser.newtxlen))voilĂ ! Now, the socket is ready to handle UDP!
Smart Code That Won't Work - 29 AUGUST 2007
Here's a good example where what seemingly is a smart and short code that just will not work! Orignially, this was the code used during one of our projects:
dim f as byte
for f=0 to 3
ser.num=f
ser.setdata("test")
ser.send
next f
Ok, so this outputs the same string through all four serial port.
Suddenly, I thought... do I really need f? Here is my new way:
for ser.num=0 to 3
ser.setdata("test")
ser.send
next ser.num
Wonderful! Only, it doesn't work!
For the cycle to be over, the ser.num must increment beyond the limit. That is, the cycle will be over when ser.num becomes 4. But you can't set it to 4 - this property is internally limited (to available serial ports 0...3). So writing ser.num=4 will still leave it at 3. So, the cycle will NEVER end.



