[Tutor] Alphabet lookup table

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jan 28 05:00:11 EST 2022


On 28/01/2022 09:02, Phil wrote:

>>   You might find it easier
>> to use binary rather than hex though.
> I thought about that. The LED scroller includes a function that converts 
> the hex value to a byte array so it's probably an unnecessary step.

The representation of the literal strings has no relevance to the
conversion to bytes, only how it looks on screen. The binary
representation makes it easier to see which pixels are on/off
and thus easier to debug. Although, with this representation
you do have to tilt your head sideways to see it! :-)

But thinking about this you don't even need to use strings,
just use the numbers:

      P = [0b01111111,
           0b00001001,
           0b00001001,
           0b00001001,
           0b00000110]

You could then use the struct module to convert to bytes:

p_bytes = struct.pack('hhhhh',P)

That might be easier? Just a thought.

>> definitions into a separate module - that way it's easy to
>> change "fonts" by renaming the files.
>>
>>
>> alpha_table = {
>>    'P' : ['0b01111111',
>>           '0b00001001',
>>           '0b00001001',
>>           '0b00001001',
>>           '0b00000110'],
> 
> Do you mean a listing like this, rather than a class with only one 
> method that I already have?

NO. Just put the dicts or class(es) into a separate file from the
code that uses them. Thus you can just rename the module that
defines the font you want to use to active.font or whatever.
Your code then reads active.font to activate the LED. Thus you
can easily have a range f different fonts and allow the user
(or programmer) to select the currently active one.

BTW A class with only one method is a code smell. It means you
don't really need a class, you only need a data structure and
a function.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list