Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 20:56 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Picomite to Picomite via I2C

     Page 2 of 2    
Author Message
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 12:30am 09 Apr 2024
Copy link to clipboard 
Print this post

  circuit said  What I am looking for is the simplest method to remotely switch on/off individually the pins on a second or third picomite.


Untested, but using the serial multidrop circuit linked above, with GP1 as Rx and GP0 as Tx, for 16 digital I/O pins it could be as simple as:

setpin GP1,GP0,COM1
open "com1:115200" As #1

print #1,"A0101010111001100" ' picomite "A" to set pins
print #1,"B1010101011001100" ' picomite "B" to set pins


and on picomite "A"
setpin GP1,GP0,COM1
open "com1:115200" As #1
for i=2 to 17: cmd$="SETPIN GP"+str$(i)+",output": EXECUTE cmd$: next i
do
 if loc(1) then
   pause 50 ' long enough for full message to arrive
   line input #1,s$
   if mid$(s$,1,1)="A" then
     for i=2 to 17
       cmd$="pin(GP"+str$(i)+")="
       if mid$(s%,i,1)="1" then cmd$=cmd$+"1" else cmd$=cmd$+"0" ' 1 is on, else off
       EXECUTE cmd$
     next i
   endif
 endif
loop


(Or perhaps something similar if I have syntax or logic wrong. Error checking would probably also be useful. Alternatively, you could set a single pin at a time.)

~
Edited 2024-04-09 10:36 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 857
Posted: 06:14am 09 Apr 2024
Copy link to clipboard 
Print this post

For speed, I recommend a binary protocol (str2bin/bin2str) and the PORT command.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 04:45pm 09 Apr 2024
Copy link to clipboard 
Print this post

It looked like a variation on this might provide entertainment for an idle morning.

This shows a MMBasic DOS program controlling a PicoMite over a serial connection. Every second, colors are randomly changed on an 8-LED WS2812, and a green/yellow/red traffic light module is cycled through its colors.

The picomite client program looks for a serial input line which begins with "X" (for EXECUTE). It then checks to see if the second character in the string matches its id (in this instance, "A"). If it does, it serially breaks out multiple commands terminated with ":" (if more than one command), and runs the EXECUTE command with the broken out command.

Here's the MMBasic DOS program:
Dim integer i,j,k,l,m,n=2,stoplightpins(2)=(25,26,27)
Open "com9:115200" As #1
'cmd$="XADo:For i=0 To 7:j=Rnd()*15:d%(i)=e%(j):Next:k=e%(j):Bitbang WS2812 b,11,8,d%():Pause 1000:Loop"
cmd$="XAsetpin 25,dout:setpin 26,dout:setpin 27,dout"
Print #1,cmd$

Do
 cmd$="XAd%(0)=e%(Rnd()*15):d%(1)=e%(Rnd()*15):d%(2)=e%(Rnd()*15):d%(3)=e%(Rnd()*15):d%(4)=e%(Rnd()*15):d%(5)=e%(Rnd()*15):d%(6)=e%(Rnd()*15):d%(7)=e%(Rnd()*15)"
 Print "Sending: ";cmd$
 Print #1,cmd$
 cmd$="XABitbang WS2812 b,11,8,d%()"
 Print "Sending: ";cmd$
 Print #1,cmd$
 Pause 1000
 cmd$="XApin("+Str$(stoplightpins(n))+")=0:"
 n=n+1:n=n Mod 3
 cmd$=cmd$+"pin("+Str$(stoplightpins(n))+")=1"
 Print "Sending: ";cmd$
 Print #1,cmd$
Loop


And the PicoMite program:
' ser-client.bas
Dim integer d%(15),e%(15)=(RGB(red), RGB(green), RGB(blue),0,0,0,0,0,0,0,0,0,0,0,0,0) ' colors for WS2812
e%(5)=RGB(gold): e%(6)=RGB(magenta): e%(7)=RGB(brown): e%(8)=RGB(pink)
e%(9)=RGB(salmon): e%(10)=RGB(orange):e%(11)=RGB(grey):e%(12)=RGB(white)
e%(9)=RGB(salmon): e%(10)=RGB(orange):e%(11)=RGB(grey):e%(12)=RGB(white)
e%(13)=RGB(lightgrey): e%(14)=RGB(beige): e%(15)=0 ' black
Dim integer i,j,k,l,m,n

SetPin GP1,GP0,COM1
Open "com1:115200" As #1
client$="A"
' for i=2 to 17: cmd$="SETPIN GP"+str$(i)+",output": EXECUTE cmd$: next i
Do
If Loc(1) Then
  Pause 50 ' long enough for full message to arrive
  Line Input #1,s$
  Print "Received: ";s$
  token$=Mid$(s$,1,1)
  If token$="X" Then ' execute commands (if for us)
    id$=Mid$(s$,2,1)
    If id$=client$ Then ' execute command (possibly multiple)
      j=3
      k=Instr(j,s$,":")
      Do While k>0
        cmd$=Mid$(s$,j,k-j)
        Execute cmd$
        j=k+1
        k=Instr(j,s$,":")
      Loop
      cmd$=Mid$(s$,j)
      Execute cmd$
    EndIf
  ElseIf token$=client$ Then
'     for i=2 to 17
'       cmd$="pin(GP"+str$(i)+")="
'       if mid$(s%,i,1)="1" then cmd$=cmd$+"1" else cmd$=cmd$+"0" ' 1 is on, else off
'       EXECUTE cmd$
'     next i
  EndIf
EndIf
Loop


A youtube video.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 357
Posted: 06:50pm 09 Apr 2024
Copy link to clipboard 
Print this post

@Iizby
this is really interesting.  Opens a lot of opportunities for both wired and wireless projects. You can make the slave to do almost anything without the need to have all possible tasks pre-programmed in the slave.

Will certainly test it in the coming days.

Please have some more morning entertainments!

Pluto
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 02:15pm 10 Apr 2024
Copy link to clipboard 
Print this post

Thanks for the encouragement. Slight changes here. I upped the speed to 230400, added a space after the "XA" token and id characters to better visually differentiate the command, and added control of a red/green LED, an RGB LED, and a buzzer. I also added ON ERROR SKIP before the EXECUTE command, so the client PicoMite doesn't halt for a syntax error.

On the MMBasic DOS side:
Dim integer i,j,k,l,m,tick,n=2,stoplightpins(2)=(25,26,27)
Open "com9:230400" As #1
'cmd$="XA Do:For i=0 To 7:j=Rnd()*15:d%(i)=e%(j):Next:k=e%(j):Bitbang WS2812 b,11,8,d%():Pause 1000:Loop"
cmd$="XA setpin 25,dout:setpin 26,dout:setpin 27,dout" ' traffic light module
Print #1,cmd$
cmd$="XA setpin 7,dout:setpin 9,dout:setpin 10,dout:setpin 14,dout:setpin 15,dout" ' RGB & RG LEDs
Print #1,cmd$
cmd$="XA setpin 5,dout" ' buzzer
Print #1,cmd$

Do
 cmd$="XA d%(0)=e%(Rnd()*15):d%(1)=e%(Rnd()*15):d%(2)=e%(Rnd()*15):d%(3)=e%(Rnd()*15):d%(4)=e%(Rnd()*15):d%(5)=e%(Rnd()*15):d%(6)=e%(Rnd()*15):d%(7)=e%(Rnd()*15)"
 Print "Sending: ";cmd$
 Print #1,cmd$
 cmd$="XA Bitbang WS2812 b,11,8,d%()"
 Print "Sending: ";cmd$
 Print #1,cmd$
 cmd$="XA pin("+Str$(stoplightpins(n))+")=0:"
 n=n+1:n=n Mod 3
 cmd$=cmd$+"pin("+Str$(stoplightpins(n))+")=1"
 Print "Sending: ";cmd$
 Print #1,cmd$
 a$=Str$((tick Mod 4) And 1):b$=Str$((tick Mod 4) >> 1 And 1)
 cmd$="XA pin(14)="+a$+":pin(15)="+b$ ' R/G LED
 Print "Sending: ";cmd$
 Print #1,cmd$
 a$=Str$((tick Mod 8) And 1):b$=Str$((tick Mod 8) >> 1 And 1):c$=Str$((tick Mod 8) >> 1 And 1)
 cmd$="XA pin(7)="+a$+":pin(9)="+b$+":pin(10)="+c$ ' RGB LED
 Print "Sending: ";cmd$
 Print #1,cmd$
 If (tick Mod 8) = 7 Then cmd$="XA pin(5)=1" Else cmd$="XA pin(5)=0"
 Print #1,cmd$
 tick=tick+1
 Pause 1500
Loop


And the PicoMite side:
' ser-client.bas
Dim integer d%(16),e%(16)=(RGB(red), RGB(green), RGB(blue),0,0,0,0,0,0,0,0,0,0,0,0,0,0) ' colors for WS2812
e%(5)=RGB(gold): e%(6)=RGB(magenta): e%(7)=RGB(brown): e%(8)=RGB(pink)
e%(9)=RGB(salmon): e%(10)=RGB(orange):e%(11)=RGB(grey):e%(12)=RGB(white)
e%(9)=RGB(salmon): e%(10)=RGB(orange):e%(11)=RGB(grey):e%(12)=RGB(white)
e%(13)=RGB(lightgrey): e%(14)=RGB(beige): e%(15)=0 ' black
Dim integer i,j,k,l,m,n

SetPin GP1,GP0,COM1
Open "com1:230400" As #1
client$="A"
' for i=2 to 17: cmd$="SETPIN GP"+str$(i)+",output": EXECUTE cmd$: next i
Do
If Loc(1) Then
  Pause 100 ' long enough for full message to arrive
  Line Input #1,s$
  Print "Received: ";s$
  token$=Mid$(s$,1,1)
  If token$="X" Then ' execute commands (if for us)
    id$=Mid$(s$,2,1)
    If id$=client$ Then ' execute command (possibly multiple)
      j=4
      k=Instr(j,s$,":")
      Do While k>0
        cmd$=Mid$(s$,j,k-j)
        Execute cmd$
        j=k+1
        k=Instr(j,s$,":")
      Loop
      cmd$=Mid$(s$,j)
      On Error Skip
      Execute cmd$
    EndIf
  EndIf
EndIf
Loop


Youtube short

~
Edited 2024-04-11 05:10 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 245
Posted: 09:55am 11 Apr 2024
Copy link to clipboard 
Print this post

Clearly our minds have been running in parallel; I simply need to switch the pins on the remote pico high/low.  Therefore I find that this has done the trick:

'Picomite Simple Serial Master

Option AUTORUN ON
SetPin GP1,GP0,com1 '
Open "com1:115200, 256" As #1
'program the commands here onwards...in the form PRINT #1,"COMMAND+PARAMETERS"




And on the remote picomite



'Picomite Simple Serial Client

Option AUTORUN ON
Dim string cmd

SetPin GP1,GP0,com1 'Set up comms channel 1
Open "com1:115200, 256, ActionCommand, 3" As #1

Do
WatchDog 500        'This recovers from errors not ignored but resets the chip
Loop

Sub ActionCommand   'Message receipt detected
ON ERROR IGNORE     'This avoids system reset if duff command arrives
 Pause 30          'ensure all characters in command have arrived
 Line Input #1,cmd 'Get the command
 Execute cmd       'Action the received command

End Sub             'Return from the interrupt


What and excellent addition the command EXECUTE has turned out to be.  Works a treat.
Thanks to all for pointing me in the right direction.

@Lizby; your extra coding to allow for multiple modules will be most helpful in the future.  Many thanks for taking the time on this one.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 03:09pm 11 Apr 2024
Copy link to clipboard 
Print this post

  circuit said  running in parallel
(as it were)

Excellent. Short and sweet client code (and robust).

Returning of values (e.g., DS18B20) adds a level of complexity--with timing issues if there are multiple clients.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 357
Posted: 12:23pm 16 Apr 2024
Copy link to clipboard 
Print this post

Iizby (or Lizby?), finally had time to test your code above last night. I am pleased with the outcome!
Initially I had some problems...tried to send a short for-next program to be excecuted by the slave. (I think you had something similar commented out in your program.) Recognized however quite rapidly that it could not work. Is this what you call a senior moment?
FOR-NEXT and DO-LOOP implemented in the master works of course excellently.

Thanks
Pluto
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 03:47pm 16 Apr 2024
Copy link to clipboard 
Print this post

  Pluto said  Iizby (or Lizby?), finally had time to test your code above last night. I am pleased with the outcome!
Initially I had some problems...tried to send a short for-next program to be excecuted by the slave. (I think you had something similar commented out in your program.) Recognized however quite rapidly that it could not work. Is this what you call a senior moment?
FOR-NEXT and DO-LOOP implemented in the master works of course excellently.

Thanks
Pluto


Right. DO LOOP, FOR NEXT, IF ENDIF won't work with EXECUTE because a single command at a time is executed and it can't know where the termination is (or how to get back to the start). I just tried IF ... THEN ... ELSE ... with no colons and no ENDIF, and that worked.

I think I left the commented-out FOR NEXT to remind myself what I was trying to implement.

Glad it otherwise worked for you.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 245
Posted: 07:41am 17 Apr 2024
Copy link to clipboard 
Print this post

In the documentation for the EXECUTE command, it says;

The command sets an internal watchdog before executing the requested
command and if control does not return to the command, like in a GOTO
statement, the timer will expire. In this case you will get the message
"Command timeout".
RUN is a special case and will cancel the timer allowing you to use the
command to chain programs if required.


There is no indication of a duration for this watchdog timer which may affect some of the more complex command constructions that are now being mentioned.  Perhaps the authors of the firmware might indicate a little more detail about this aspect of the command and its impact.
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 896
Posted: 08:06am 17 Apr 2024
Copy link to clipboard 
Print this post

A 1 second timer is started when a command is executed via EXECUTE "command".
i.e.
ScrewUpTimer = 1000; i.e 1 second.
If the command completes within 1 second all is good. If not then MMBasic is restart.
EXECUTE "PAUSE 950" should work.
EXECUTE "PAUSE 1050" will cause a restart.

It looks like the "Command timeout" message is no longer given. May have gone when the hardware timeout was introduced.
Latest F4 Latest H7
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024