Using strings to store sequences

Help and questions on programming the codebug
Post Reply
pigraham
Posts: 65
Joined: Sat Aug 15, 2015 1:41 pm

Using strings to store sequences

Post by pigraham » Thu Aug 27, 2015 8:05 am

Can CodeBug Blockly handle sequence data? Yes it can, but with limitations.

There are no string handling blocks. The only string is a string sprite, but there are no blocks to address the content of the string.
Here is a hack that allows character by character addressing of characters in a string sprite. It can be used to store an arbitrary sequence of characters and take action depending on the next character in sequence.
Restrictions:
  • Constant (read only) data. You can't address or write to a string sprite
  • Slow. Writing to the LED array is quite slow, which could be an issue for any time critical loops
  • It has to write the display, so any sequence addressing is visible and destroys other display content
  • The programmer has to work out the decoding for the characters used. No one read will decode all possible characters.
  • Maximum string length 80 characters
A bare script showing the idea is here http://www.codebug.co.uk/explore/codebu ... gsequence/ [mod edit to correct url]

It defines an arbitrary string consisting of UDLR. It steps along this string one character at a time. It decodes the chracter by reading column 3 of the display. You could sample any pixels on the display that discriminate the characters you are using.
In this case U = 15, D = 14, R = 18, L = 16.
To show some result for the demo the corresponding direction word is then scrolled in the specified direction.

Get width of sprite returns the number of characters in the string * 5 (each char is 5*5 pixels).
draw sprite at x position a multiple of 5 displays a single character from the string as if at 0,0.
The column value is binary where b0 is on row 4, b1 row 3 etc.
So U is
*__*
*__*
*__*
*__*
_**_
So reading col 3 from the top is ****_
As binary, with the top pixel as b0: 01111 = 15 decimal

mikerr
Posts: 83
Joined: Fri Jul 31, 2015 10:37 am
Contact:

Re: Using strings to store sequences

Post by mikerr » Fri Aug 28, 2015 6:51 pm

A very inventive way of doing sequence/array !
It defines an arbitrary string consisting of UDLR. It steps along this string one character at a time. It decodes the character by reading column 3 of the display. You could sample any pixels on the display that discriminate the characters you are using.
In this case U = 15, D = 14, R = 18, L = 16.
I've worked out a clearer way of reading the character from the display using sprite comparisons (now they work!)
instead of having to work out L = 16 etc

changing
Image

to

Image
etc

the full remixed version is at
http://www.codebug.co.uk/explore/codebu ... nce-remix/
Twitter @mikered codebug profile mikerr

pigraham
Posts: 65
Joined: Sat Aug 15, 2015 1:41 pm

Re: Using strings to store sequences

Post by pigraham » Tue Sep 01, 2015 8:52 am

That is much easier.

pigraham
Posts: 65
Joined: Sat Aug 15, 2015 1:41 pm

Re: Using strings to store sequences

Post by pigraham » Sat Sep 05, 2015 3:36 pm

Although the sprite comparison work a treat in the web emulator it fails on the bug!
Just loading http://www.codebug.co.uk/explore/codebu ... nce-remix/ on the bug I see only the RULD appear on the LEDs. No scrolling text.

Any ideas?

I was wondering, why is the get sprite from block using a width of 4? It doesn't work with 5, in emulator.
Where width=4 fails on the bug Width=5 Works, sort of.
some matches work. A command string "DRLUDR" the first D and R fails to match but L,U,D and the second R all work.

http://www.codebug.co.uk/explore/codebu ... mix-remix/

Any ideas?

mikerr
Posts: 83
Joined: Fri Jul 31, 2015 10:37 am
Contact:

Re: Using strings to store sequences

Post by mikerr » Sat Sep 05, 2015 5:59 pm

The "grabbed sprite" from screen is width of 4 because character generated sprites are 4 wide by 5 high,
and here's a sketch to prove it :

http://www.codebug.co.uk/explore/codebu ... imensions/ ;)

I'm assuming for the sprite comparison to work you have to compare a 4x5 sprite with another 4x5 sprite.

Your version is comparing a 5x5 sprite (screen grabbed) to a 4x5 sprite (character sprite) - which is probably "undefined".
Twitter @mikered codebug profile mikerr

pigraham
Posts: 65
Joined: Sat Aug 15, 2015 1:41 pm

Re: Using strings to store sequences

Post by pigraham » Sat Sep 05, 2015 6:56 pm

Hmmm, that doesn't compile Mike.

Code: Select all

ERROR: Compile error (we have been notified about this)
This remix puts the character sprite into a variable first and that compiles.
http://www.codebug.co.uk/explore/codebu ... ons-remix/

Running in the emulator shows 4 x 5.
Running on a bug shows 5 x 5.

Given the inconsistency perhaps sprite dimensions are not properly clipped and can include extraneous data that can make the test fail depending on, say, what the next character is (outside the LED array).

Get col and get row seem to work.

http://www.codebug.co.uk/explore/codebu ... nce-debug/

Drawing the character sprites and using get sprite to read into a variable didn't work.

If only there were some string indexing functions!

Post Reply