Menu

Passing Arrays to Subroutines

Help
2007-05-24
2013-05-30
  • Nobody/Anonymous

    I am trying to write a routine to display custom characters to the LCD. For this the function is usually passed an array 8 bytes which are successively written to CGRam. My code so far is as follows:

    sub LCDDefineChar(CBYTE, USERCHAR*) #NR
    REPEAT 3
    Rotate CBYTE LEFT SIMPLE
    END REPEAT
    LCDWriteByte(0x70 | CBYTE)
    FOR i = 1 to 8
    LCDWriteByte(USERCHAR(i))
    NEXT       
    end sub

    I get USERCHAR not defined. Is it possible to pass arrays in this manner? I'm used to being able to do this in C.

     
    • Nobody/Anonymous

      To add what I wrote above here is some working code to display an Up arrow.

      LCDDefineChar(0x00,0x04,0x0e,0x15,0x04,0x04,0x04,0x04,0x04)

      sub LCDDefineChar(CBYTE, USERCHAR1, USERCHAR2, USERCHAR3, USERCHAR4, USERCHAR5, USERCHAR6, USERCHAR7, USERCHAR8)
      REPEAT 3
        Rotate CBYTE LEFT SIMPLE
      END REPEAT
         
      SET LCD_RS OFF
      LCDWriteByte(0x40 | CBYTE)
      SET LCD_RS ON
      LCDWriteByte(USERCHAR1)
      LCDWriteByte(USERCHAR2)
      LCDWriteByte(USERCHAR3)
      LCDWriteByte(USERCHAR4)
      LCDWriteByte(USERCHAR5)
      LCDWriteByte(USERCHAR6)
      LCDWriteByte(USERCHAR7)
      LCDWriteByte(USERCHAR8)   
      end sub       

       
    • Hugh Considine

      Hugh Considine - 2007-05-24

      To pass arrays, add () after the name of the array. For example:

      Dim SomeArray(10)

      TestSub (SomeArray(), 10)

      sub TestSub(InArray(), Count)
        for Temp = 1 to Count
          InArray(Temp) = Temp
        next
      end sub

       
    • Nobody/Anonymous

      Is it possible to make the array declarations use square brackets instead of () to be standard with C and other languages? Also I have written my routine to add user defined characters to the LCD but there is one quirk I want to deal with, that is the initialization of arrays on declaration.

      For example the following two should be equivalent:

      Dim UP_ARROW(8) = {0x04, 0x0e, 0x15, 0x04, 0x04, 0x04, 0x04, 0x04}

      Dim UP_ARROW(8)
      UP_ARROW(1) = 0x04
      UP_ARROW(2) = 0x0e
      UP_ARROW(3) = 0x15
      UP_ARROW(4) = 0x04
      UP_ARROW(5) = 0x04
      UP_ARROW(6) = 0x04
      UP_ARROW(7) = 0x04
      UP_ARROW(8) = 0x04

      But the latter gives the correct result while the first doesn't throw an error but gives an invalid result.

      The code for the LCD user defined character is as follows:

      sub LCDDefineChar(CBYTE, USERCHAR()) #NR
          SET LCD_RSTemp LCD_RS
          REPEAT 3
              Rotate CBYTE LEFT SIMPLE
          END REPEAT
          SET LCD_RS OFF   
          LCDWriteByte(0x40 | CBYTE)   
          SET LCD_RS ON
          FOR i = 1 to 8
              LCDWriteByte(USERCHAR(i))
          NEXT   
          SET LCD_RS LCD_RSTemp
      end sub

      Example Usage:

      LCDDefineChar(1,UP_ARROW())

      Put(0,0,1)

      Also, what compiler are you using to make GCBasic? I would really like to help but I don't know which compiler you are using. Thanks!

       
    • Hugh Considine

      Hugh Considine - 2007-05-25

      GCBASIC uses () to be as similar as possible to FreeBASIC and VB/VB.NET.

      Currently the only way to initialise an array is to set each element on a single line. It would be easy to implement the first syntax - I've been thinking of something like this for a while, and will implement it soon (sometime in the next few days).

      GCBASIC is compiled with FreeBASIC. I've successfully compiled and run GCBASIC both on Windows and Ubuntu Linux. Be warned though, the GCBASIC source is quite a mess!

       
    • Nobody/Anonymous

      "the GCBASIC source is quite a mess!" Indeed, do you need some help cleaning it up??

       
    • Hugh Considine

      Hugh Considine - 2007-06-16

      Okay, I've finally added the ability to set multiple array elements with one line. Here is an example program which creates custom LCD characters:

      #include "mytest.h"

      Dim TempArray(10)

      TempArray() = 0, b'00010101', b'00001010', b'00010101', b'00001010', b'00010101', 0, 0
      LCDCreateChar 0, TempArray()

      print "Char: "
      LCDWriteChar 0

      Do
      Wait 500 ms
      TempArray() = 0, 1, 1, 1, 1, 1, 1, 0
      LCDCreateChar 0, TempArray()

      Wait 500 ms
      TempArray() = 0, 0, 0, 0, 0, 0, 255, 0
      LCDCreateChar 0, TempArray()

      Wait 500 ms
      TempArray() = 0, 16, 16, 16, 16, 16, 16, 0
      LCDCreateChar 0, TempArray()

      Wait 500 ms
      TempArray() = 0, 255, 0, 0, 0, 0, 0, 0
      LCDCreateChar 0, TempArray()

      Loop

      The LCDCreateChar sub is new, as is the ability to use numbers greater than 255 in the us and ms delays.

      http://gcbasic.sourceforge.net/newfiles/update.zip

       

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.