# dimension arrays in Swordfish

Status
Not open for further replies.

#### MrDEB

##### Well-Known Member
wanting to do this in a For NEXT LOOP but Swordfish uses only 1 demension unless I am going in wrong direction.Is this even doable??
[QUOTE
dim Channel(21) as byte
dim Channel(1) as portD.1
dim Channel(2) as portD.0
dim Channel(3) as portC.3
dim Channel(4) as portC.2
dim Channel(5) as portC.1

For x = 0 to 10
Channel(x) = 1
delayms(1000)
toggle (Channel(x)
next
][/QUOTE]

#### Pommie

##### Well-Known Member
Not doable unless Swordfish has pointers to bits. Didn't you ask this same thing a month or two back?

Mike.

#### MrDEB

##### Well-Known Member
yes I thought I did but wasn't sure I did.
doing some research because I have seen this done. Graham Mitchel has simlar in his Tetris code.

Use Select case

#### JonSea

##### Well-Known Member
Use Select case
I use these whenever there are several options. Extremely useful when trying to break stuff into ranges when used intelligently.

#### MrDEB

##### Well-Known Member
Thanks Burt, I saw your post before going to bed last night.
Going to try and load into a CONST array the values from 0 to 3 then load the buffer with the values. Each port will have a value from 0 to 3 for trial purposes on the Tap-28
Hopefully on the right track.
A SELECT CASE is basically a short version of IF THEN statements so this might work??

#### be80be

##### Well-Known Member
You are using 2 ports that makes it hard to do what you want.
You can change a whole port easy you can change bits too but
this doesn't work
Code:
dim Channel(21) as byte
dim Channel(1) as portD.1
dim Channel(2) as portD.0
dim Channel(3) as portC.3
dim Channel(4) as portC.2
dim Channel(5) as portC.1
But you'd be better with case and then just point to what case you want to use.

#### MrDEB

##### Well-Known Member
I started using CASE SELECT but would still like to use Led(1) , Led(2) etc.
looking at an example code where a data buffer was used.
First dim the ports DIM Led_1 as portb.0
then load a buffer Led_1 = Buffer(1)
want to play with this but Swordfish does not use more that 1 dimension arrays.
My goal is to use similar to PORTB.BITS(x) but may be asking the impossible??

#### JonSea

##### Well-Known Member
No matter how many more times you say it, it's not going to work. Not even if you ask in 7 threads in 3 different forums. Oh, wait, you've already tried that.

Give C a try

#### MrDEB

##### Well-Known Member
I must have missed Mikes suggestion. Need to look at the << symbol.
will give it a try
Thanks Mike

#### MrDEB

##### Well-Known Member
I must have somethib wrong.
I tried Mikes suggestion and try to compile and keep getting EXPECTED on the sub route call
SUB light(led AS BYTE)
one minor issue I see is I am using 20 port pins on three different ports
Will come back to this later as I need to get some material cut for the town wide garage sale today.

#### JonSea

##### Well-Known Member
You have typo'd or missed something prior to the line where the error is indicated. Something is missing in the line above, until eventually the compiler can't make sense of something. It could be a missing parentheses, a missing END IF for an IF statement & etc, a malformed command, etc.

You really should understand that after you've written a page of code.

#### be80be

##### Well-Known Member
Swordfish doesn't support arrays of more than one dimension directly, but it's very easy to achieve the same thing using structures. The syntax is perhaps a little more convoluted, but the net result is the same. You will also find it to be more flexible, as structures can hold different data types. However, lets keep it simple and look at how we can create a 10 x 10 matrix of byte values.

Structure TArray
a(10) As Byte
End Structure
Dim Items(10) As TArray

The above code may look a little strange, but on closer examination it can be seen that Items() holds 10 structure elements, each with their own 10 byte array. To access an element, we simply use

Items(5).a(5) = 10

which is the same as writing something like

Items(5)(5) = 10

Here is a small code example which initialises a 10 x 10 matrix, then displays via the MCUs USART

// import modules...
include "usart.bas"
include "convert.bas"

// declare a 10 x 10 byte array...
Structure TArray
a(10) As Byte
End Structure
Dim Items(10) As TArray

// local variables...
Dim i,j, Value As Byte

// initialise the two dimensional array...
for i = 0 to 9
for j = 0 to 9
Items(i).a(j) = i + j
next
next

// display to the screen...
setbaudrate(br115200)
for i = 0 to 9
for j = 0 to 9
Value = Items(i).a(j)
Write(DecToStr(Value,2)," ")
next
Write(13,10)
next

That's all there is to it. Passing your array to a sub or function is really easy also. For example, given the subroutine declaration

sub MySub(byref pArray() as TArray)
...
end sub

we can pass the array like this

MySub(Items)

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Why don't you modify Mikes program slightly to look more like a two dimensional array..
Code:
sub toggleLED( led as byte, myport as byte)
if(myport = 1) then
porta = porta xor (1<<(led and 7))
elseif( myport = 2) then
portb = portb xor (1<<(led and 7))
elseif(myport = 3) then
portc = portc xor (1<<(led and 7))
endif
Now you can use it as an array toggleLED( 5 , 1 ) .. LED 5 on port 1 ( A )

#### tumbleweed

##### Active Member
Here's an example that doesn't use arrays (need to research that), doesn't use const arrays (need to research that too), doesn't use complex statements like 'select case' or operators like '<<' (currently researching those), and doesn't involve 'C' (God help us, please don't research THAT!)

Code:
dim LED1 as PORTD.1,
LED2 as PORTD.0,
LED3 as PORTC.3,
LED4 as PORTC.2,
LED5 as PORTC.1

public sub set_led(ledno as byte, setting as bit)
if (ledno = 1) then
LED1 = setting
elseif (ledno = 2) then
LED2 = setting
elseif (ledno = 3) then
LED3 = setting
elseif (ledno = 4) then
LED4 = setting
elseif (ledno = 5) then
LED5 = setting
endif
end sub

public sub toggle_led(ledno as byte)
if (ledno = 1) then
toggle(LED1)
elseif (ledno = 2) then
toggle(LED2)
elseif (ledno = 3) then
toggle(LED3)
elseif (ledno = 4) then
toggle(LED4)
elseif (ledno = 5) then
toggle(LED5)
endif
end sub

dim x as byte

// make all leds outputs
output(LED1)
output(LED2)
output(LED3)
output(LED4)
output(LED5)

for x = 1 to 5
set_led(x, 0)   // turn off led # x
next

for x = 1 to 5
set_led(x, 1)   // turn on led # x
next

for x = 1 to 5
toggle_led(x)   // toggle led # x
next

for x = 1 to 5
set_led(x, 1)
delayms(1000)
toggle_led(x)
next
It's likely MrDEB won't even read this post.

#### JonSea

##### Well-Known Member
Case Select is really not a "complex statement" and is in fact easier to use than a slew of If Then statements.

#### be80be

##### Well-Known Member
It's kind of how the world is today lets make it simple push button led comes on.
Na that to easy let's make leds light up and make a sign.
Na that worked but its like pushing a button.
lets write code that pull the letters out of a array and sets the pins sounds easy
Lets do it the way I want it to work forget that I didn't write swordfish and the help files with it and I'm sure it works my way.
See the problem is " dim Channel (1) as port anything is that when you do the next dim Channel (2)
the compiler see's Channel being used agin (X) is to hold bit , byte, or words value.

#### tumbleweed

##### Active Member
Case Select is really not a "complex statement" and is in fact easier to use than a slew of If Then statements
For most folks that's true, but for MrDEB it involves a lot of 'research' (ie search the internet until he finds code to copy that doesn't really do what he wants but contains one of the keywords he searched for so it's somehow close). Using anything past FOR NEXT, IF THEN, and simple assignments is dancing in dangerous territory.

After close to 10 years of watching his posts I don't think he's actually read or understood much of what's been told to him. Case in point is the first post in this thread. How could that possibly compile? What does it even mean?

Do you think there's any way a multi-dimensional array would solve his problem in a way that wouldn't eventually come back as gibberish?

Status
Not open for further replies.