Menu

Put strings in a table?

Help
mkstevo
2019-03-14
2021-04-06
  • mkstevo

    mkstevo - 2019-03-14

    Is there an easy way of writing a (fixed) string into a table rather than using up RAM?

    I've written a number of programs that use a number of 74HC595 shift registers connected to seven segment LED displays that are then written to in order to display text and numerals.

    I've been using tables which have the numeric values required for the various segments to be lit in order to display the correct characters, and hence the messages, but this is rather hard work if a message needs to be changed.

    Here is an example of one of the 'message' tables I'm using.

    Table Message2 As Byte
        255 'Space
        249 '1
        164 '2
        142 'F
        130 '6
        248 '7
        146 '5
        255 'Space
        161 'd
        207 'I
        146 'S
        140 'P
        199 'L
        136 'A
        145 'Y
        255 'Space
        135 't
        134 'E
        146 'S
        135 't
        255 'Space
        255 'Space
        255 'Space
        255 'Space
    End Table
    

    I had an idea that I could improve on this by creating an 'Alphabet' table and then finding the ascii character of each letter of a string and then looking this up in the 'alphabet' in order to find the code to pass to the 74HC595 display.

    Similar to this:

    Table Alphabet As Byte
        255 'Space
        255 '!
        255 '"
        255 '#
        255 '$
        255 '%
        255 '&
        255 ' '
        198 '(
        240 ')
        255 '*
        255 '+
        255 ' '
        191 '-
        127 '.
        255 '/
        192 '0
        249 '1
        164 '2
        176 '3
        153 '4
        146 '5
        130 '6
        248 '7
        128 '8
        144 '9
        255 ':
        255 ';
        255 '<
        183 '=
        255 '>
        255 '?
        255 '@
        136 'A
        128 'B
        198 'C
        192 'D
        134 'E
        142 'F
        194 'G
        137 'H
        207 'I
        241 'J
        137 'K
        199 'L
        200 'M
        200 'N
        192 'O
        140 'P
        152 'Q
        206 'R
        146 'S
        135 'T
        193 'U
        193 'V
        193 'W
        137 'X
        145 'Y
        164 'Z
        198 '(
        255 '\
        240 ')
        255 '^
        247 '_
        255 ''
        163 'a
        131 'b
        167 'c
        161 'd
        134 'E
        142 'f
        144 'g
        139 'h
        239 'i
        241 'j
        139 'k
        207 'l
        171 'm
        171 'n
        163 'o
        140 'p
        152 'q
        175 'r
        146 's
        135 't
        227 'u
        227 'v
        227 'w
        137 'x
        145 'y
        164 'z
        198 '{
        255 '|
        240 '}
        255 '~
    End Table
    
    Dim MyFirstMessage  As String * 20
    Let MyFirstMessage  = "12F1840 Display test"
    
            Let StringLen = Len (MyFirstMessage)
    
            For StringPos = 1 to StringLen
    
              Let PrintChr = Asc(MyFirstMessage, StringPos)
    
              If PrintChr > 31 Then
                If PrintChr < 127 Then
                  Let PrintChr = PrintChr - 31
                Else
                  Let PrintChr = 255
                End If
              Else
                Let PrintChr = 255
              End If
    
              If EndMessage=1 Then
                 Exit For
              End If
    
              ReadTable Alphabet, PrintChr, Digit
              Let D_Lat  = 0                'Start output data latch
              ShiftData(Digit)
              Let D_Lat  = 1                'End  output data latch
    
              WaitLoop(CharDelay)
    
            Next StringPos
    

    All this works, and it is much much easier to update the 'messages', but the string arrays take up some ram and I'd rather put them into a table if possible.

    Is there some method of doing so that I'm not aware of?

    I tried:

    Table MyFirstMessage As Byte
    "H","e","l","l","o"
    End Table
    

    But that didn't work.

     
  • Anobium

    Anobium - 2019-03-14

    Use the Converter(s). This (the converter) can take am external file of strings it would create the tables for automatically.

     
  • Anobium

    Anobium - 2019-03-14

    Use the Converter(s). This (the converter) can take am external file of strings it would create the tables for automatically.

     
    • mkstevo

      mkstevo - 2019-03-14

      Hmm... I'm guessing that the converters are included with SynWrite? Or Windows only?

      Still, you got me thinking.

      If anyone else is wanting to do such a thing on a Mac, I've written a very poor AppleScript which will produce a Table from a text input.

      I've saved it as an AppleScript Application which should run on a Mac once downloaded. I've placed the Application here to share it.

      Edit: If you are intending to use this on a Mac, you might want to add:
      tell me to activate
      to the top of the code (I've added it below).

      This makes the dialog box jump to the frontmost when started so it doesn't get hidden if started by another Application.

      Here is the code:

      tell me to activate
      set theResponse to display dialog "Convert text to GCB table" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
      
      set stuff to (text returned of theResponse)
      set stuff_ascii_html to "Table TableName As Byte" & return
      
      repeat with c from 1 to the count of stuff
      
          set stuff_ascii_html to stuff_ascii_html & ((ASCII number of (character c of stuff)) as text)
          if ((ASCII number of (character c of stuff)) = 32) then
              set stuff_ascii_html to stuff_ascii_html & " 'Space" & return
          else
              set stuff_ascii_html to stuff_ascii_html & " '" & (character c of stuff) & return
          end if
      
      end repeat
      
      set stuff_ascii_html to stuff_ascii_html & "End Table"
      
      display dialog (stuff as text) & return & return & "Copy this:" default answer stuff_ascii_html as string
      

      Here is the output generated:

      Table TableName As Byte
      71 'G
      67 'C
      66 'B
      32 'Space
      65 'A
      112 'p
      112 'p
      108 'l
      101 'e
      115 's
      99 'c
      114 'r
      105 'i
      112 'p
      116 't
      32 'Space
      99 'c
      111 'o
      100 'd
      101 'e
      32 'Space
      99 'c
      111 'o
      110 'n
      118 'v
      101 'e
      114 'r
      116 't
      101 'e
      114 'r
      32 'Space
      116 't
      101 'e
      115 's
      116 't
      46 '.
      End Table
      

      Certainly a lot faster than I was doing things!

       

      Last edit: mkstevo 2019-03-14
  • mkstevo

    mkstevo - 2019-03-14

    Many thanks for your suggestion there. I hadn't thought of doing it that way until you mentioned it.

    I've now gone from 58.2% RAM used to just 14%. The ROM usage is of course relatively unchanged as the strings have to be stored in ROM before being loaded into RAM so a net gain in my book.

    Much better, thanks again.

     
    • Anobium

      Anobium - 2019-03-14

      Converters use the native operating system capabilities. You can use AppleScript or an OS Specific version of AWK or GAWK.

      So, you could take your little app and integrate. :-)

      But, you have a good method already. :-)

       
  • mkstevo

    mkstevo - 2019-03-14

    I can't find the converters in the MacOS installation?

    Still, I have to say I'm sat here feeling a little smug now as I've managed to add my script to the menu in Geany so I can invoke it from within the IDE.

    The command for opening the Application from Geany (and presumably other Mac editors too) is rather convoluted as if you are not careful, it attempts to open a directory as an executeable file. This is the command I'm using:

    /Users/Mark/GreatCowBasic/GCB_Text_To_Table.app/Contents/MacOS/applet

    Clearly the "/Users/Mark/" section requires editing to reflect the location you stored the Application on your hard drive, but you will hopefully get the idea.

    It is saving me some work and the 'two-table-tango' to convert from human readable table to the numeric code required to display it on a seven segment display driven by cascaded 74HC595s is working a treat too.

    Chuffed to bits!

     

    Last edit: mkstevo 2019-03-14
    • Anobium

      Anobium - 2019-03-14

      create a folder called converters below greatcowbasi folder. Then, either use the Windows files or have a look in the help to create the ini file etc. Also, see https://sourceforge.net/p/gcbasic/code/HEAD/tree/GCBASIC/trunk/converters/

      I bet you are chuffed!

       
  • mkstevo

    mkstevo - 2021-04-05

    I had difficulty making sense of the converters. I think they might initiate the "Awk" command to perform the text to table transformations, but I couldn't see how.

    Having written my script for macOS, I needed something similar for Windows as I really missed having access to it. I've had time today to create something similar using AutoHotKey. This can certainly be added to the Geany "Build" commands, and I don't doubt it could also be included into SynWrite too.

    It is an AutoHotKey compiled executable, so should run without any need to install AutoHotKey.

     
  • Anobium

    Anobium - 2021-04-05

    With the new compiler. Just put the strings in the table between quotes.

     
  • mkstevo

    mkstevo - 2021-04-05

    So I can write:

    Table HelloTable As Byte
    "Hello Gcb from text to table!"
    End Table
    

    Which would be converted to:

    Table HelloTable As Byte
    72 'H
    101 'e
    108 'l
    108 'l
    111 'o
    32 'Spc
    71 'G
    99 'c
    98 'b
    32 'Spc
    102 'f
    114 'r
    111 'o
    109 'm
    32 'Spc
    116 't
    101 'e
    120 'x
    116 't
    32 'Spc
    116 't
    111 'o
    32 'Spc
    116 't
    97 'a
    98 'b
    108 'l
    101 'e
    33 '!
    End Table
    

    Giving the same table functionality?

    I've seen the thread on tables, but thought it was concentrated on multi-level menus and so dismissed it as not relevant.

     

    Last edit: mkstevo 2021-04-05
  • Anobium

    Anobium - 2021-04-05

    yep...

    ReadTable Test,1 , mylen
    
    for readtext = 1 to mylen
      ReadTable Test, readtext, mychar
    next
    
    Table Test
    "Hello Gcb from text to table!"
    End Table
    

    to...

    TABLETEST
    
    retlw   29
    retlw   72
    retlw   101
    retlw   108
    retlw   108
    retlw   111
    retlw   32
    retlw   71
    retlw   99
    retlw   98
    retlw   32
    retlw   102
    retlw   114
    retlw   111
    retlw   109
    retlw   32
    retlw   116
    retlw   101
    retlw   120
    retlw   116
    retlw   32
    retlw   116
    retlw   111
    retlw   32
    retlw   116
    retlw   97
    retlw   98
    retlw   108
    retlw   101
    retlw   33
    
     
  • mkstevo

    mkstevo - 2021-04-05

    Thanks.
    That would have saved me an afternoon of work had I understood what was going on!

    Ah well, it's a learning curve isn't it...

     
    • Anobium

      Anobium - 2021-04-05

      Things are moving all the time. RC releases have lots and lots of new capabilities.

       
      • mkstevo

        mkstevo - 2021-04-05

        Sadly, it never became clear to me that the functionality did that. I had interpreted it from the thread as being for adding menus with sub menus, not for 'plain' text tables as well.

         
  • Anobium

    Anobium - 2021-04-05

    I posted a lot of examples today.

    You are correct that the tables can now be easily made into a menu system, but, we added escape chararcters and much more. 😃

     
  • stan cartwright

    stan cartwright - 2021-04-05

    It's over my head. I never had problems really with tables.
    Though I never used tables for strings I never saw it a problem to use the table to add to a string un til complete. I would have used markers or length of string.
    Anyway it's all got too complicated with all the rc releases.
    it's more often than windows

     
    • Anobium

      Anobium - 2021-04-06

      It called Development. No RC releases means a static tool chain. No development means no new chips. no new capabilities and no fixes.

      And, hopefully, the software gets better. :-)

       
    • mkstevo

      mkstevo - 2021-04-06

      As you might tell, I use a lot of tables. My main reason is that I can do a sort of reverse lookup from the value (character) in one table and cross reference it to the value in another table (value required to illuminate the elements of a seven segment display to show that character). Holding these in a table allows me to scroll a long sentence along a four 'digit' seven segment display easily by increasing the index. I can also perform animation in a similar way. Being able to update the character values in that table simply means I can quickly change the messages shown on the LED display.

      Although it took me most of Monday afternoon to get my little script working, and I don't really need it now thanks to the table 'string' functions added in the latest releases, I probably will still use my script(s) for creating tables. It formats the data into a table like structure, with the byte values represented individually - that helps me visualise that they are stored as individual bytes within the memory of the device. Daft I know, but it helps me to think!

       
  • stan cartwright

    stan cartwright - 2021-04-06

    What would be nice would be an easy way to define unused chrs to a few lines of binary.
    more understanding of glcd charsets and how to predefine...in a table?

    I don't think I explained that well.
    Is there an option to use a table for special definable charsor would they need plotting-pset?

     

    Last edit: stan cartwright 2021-04-06
    • Anobium

      Anobium - 2021-04-06

      @Stan. Create a new thread to discuss GLCD character creation.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.