Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 15:47 09 May 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 : Sequencing Code

Author Message
marcwolf

Senior Member

Joined: 08/06/2009
Location: Australia
Posts: 119
Posted: 04:02pm 18 Jul 2013
Copy link to clipboard 
Print this post

Hi folks. I'd thought I's paste some code I am working on.
Basically the concept is to read through set's of servo sequences in different files with each file containing multiple sets, different record length, an a primitive command looping and jump system.
Please feel free to use it.
Also many thanks Geoff for the Random File Access for without that this sould not have been possible at all.
Dave


' Sequence Processing System


option base 1 ' sets arrays to start at 1

dim RecElem(5,7)
Dim FileElem(5,2)

' File Structure
' First 40 bytes contain Rec Len, Num Seq, and Comment
' Then 100,200,300 bytes are for first seq header.. seq num, start rec, num rec in sequence
' Byte 2000 is start of first sequence

'File Example
FileElem(1,1) = 64 ' Record Length
FileElem(1,2) = 6 ' Num of Sequences

' Record Example
RecElem(1,1) = 0 ' Seq Num
RecElem(1,2) = 0 ' Curr Rec
RecElem(1,3) = 0 ' Delay Count
RecElem(1,4) = 0 ' Byte Offset to first seq position
RecElem(1,5) = 0 ' Recs in current sequence
RecElem(1,6) = 0 ' Last Branch
RecElem(1,7) = 0 ' Last Pos before Branch

' Globals
DefFldLen = 10 ' default numberic field len
DefCmdOff = 21 ' Skip to Command

Function NumPad$(iNum, FldLen) ' Pad Numbers with specified Len
LOCAL a$
a$ = string$(FldLen,32)
NumPad$ = right$(a$ + str$(iNum),FldLen)
End Function

Function Pad$(iNum) ' Pad Numbers for Saving
LOCAL a$
a$ = string$( DefFldLen,32)
Pad$ = right$(a$ + str$(iNum), DefFldLen)
End Function

function Get$(FileNum, Recnum)
' Get offset record pos using Seq Details
LOCAL t
LOCAL q$
t = (FileElem(FileNum,1) * (RecNum-1)) + 1 ' Rec Number
t = t + RecElem(FileNum,4) ' Add Start Offset
If t < LOF(#FileNum) then
seek #FileNum, t
q$ = input$(FileElem(FileNum,1),#FileNum)
else
q$ = "EOF"
endif
Get$ = q$
End Function

function GetRec$(FileNum, RecLen, Recnum, OffSet)
' Get absolute record position
LOCAL t
LOCAL q$
t = (RecLen * (RecNum-1)) + 1 ' Rec Number
t = t + Offset
If t < LOF(#FileNum) then
seek #FileNum, t
q$ = input$(RecLen,#FileNum)
else
q$ = "EOF"
endif
GetRec$ = q$
End Function

SUB PutRec(FileNum, RecLen, Recnum, OffSet, Rec$)
' Get absolute record position
local t
t = (RecLen * (RecNum-1)) + 1 ' Rec Number
t = t + Offset ' Add Offset
seek #FileNum, t
Print #FileNum, Rec$;
End SUB


Sub LoadSeq(FileNum, SeqNum)
' Load the SEQ Header into the Array
SeqNum = 100 + ((SeqNum-1) * 100)
Seek #FileNum, SeqNum : RecElem(FileNum,1) = VAL(INPUT$(DefFldLen,FileNum)) ' Seq Num
Seek #FileNum, SeqNum + 11 : RecElem(FileNum,4) = VAL(INPUT$(DefFldLen,FileNum)) ' Byte Offset
Seek #FileNum, SeqNum + 22 : RecElem(FileNum,5) = VAL(INPUT$(DefFldLen,FileNum)) ' Recs in Sequence
RecElem(FileNum,2) =0: RecElem(FileNum,3) =0
End Sub

Sub LoadFile(FileNum, FileName$ )
' Load the File Header
open FileName$ for RANDOM as #Filenum
SEEK #FileNum, 1
FileElem(FileNum,1) = VAL(INPUT$(DefFldLen,#FileNum))
SEEK #FileNum, 12
FileElem(FileNum,2) = VAL(INPUT$(DefFldLen,#FileNum))
End Sub



Function ProcessRec$(FileNum)
Local Rec$, Cmd$, Q

' Are we doing a Delay Looping ?
If RecElem(FileNum,3) > 0 then
RecElem(FileNum,3) = RecElem(FileNum,3) -1
ProcessRec$ = ""
Exit Function
EndIf

' Are we at STOP ?
If RecElem(FileNum,2) = -99 then
' No Op
ProcessRec$ = ""
Exit Function
EndIf

ReEntryIncr:

' Incr Pointers
RecElem(FileNum,2) = RecElem(FileNum,2) + 1

ReEntry:

' Are we at End of Seq? Then Loop to Beginning
If (RecElem(FileNum,2) ) > RecElem(FileNum,5) then
RecElem(FileNum,2) = 1
ENDIF

' Get the Actual Record
Rec$ = get$(FileNum,RecElem(FileNum,2)) ' Get REAL Record

' If "EOF then treat as NOP
If Rec$ = "EOF" Then
RecElem(FileNum,2) = 1
ProcessRec$ = ""
EXIT FUNCTION
EndIf

Rec$ = MID$(Rec$,DefCmdOff) ' Remove Recs Seq Nums
Cmd$ = LEFT$(Rec$,2) ' Get the first 2 chars for command

'XX - Stop. Don't progess any more
'XJxxxx - Jump to Record xxxx back from Current
'XSxxxx - Jump to Sequence #xxxx
'XDxxxx - Delay for x number of interactions and then to next


If Left$(Cmd$,1) = "X" Then

If Left$(Cmd$,2) = "XX" then ' STOP
RecElem(FileNum,2) = -99
ProcessRec$=""
Exit Function
EndIf

If Left$(Cmd$,2) = "XJ" then ' JUMP To Record
Q = val(mid$(Rec$,3,10))
RecElem(FileNum,2) = Q
Goto ReEntry
EndIf

If Left$(Cmd$,2) = "XD" then ' Delay for X Reads
Q=val(mid$(Rec$,3,10))
RecElem(FileNum,3) = Q
ProcessRec$ = ""
EXIT FUNCTION
EndIf

If Left$(Cmd$,2) = "XS" then ' Sequence Change
Q = val(mid$(Rec$,3,10))
loadseq(FileNum, Q)
RecElem(FileNum,2) =1:RecElem(FileNum,6) =0: RecElem(FileNum,7) =0
Goto ReEntry
EndIf

EndIf


' Return Back Servo Command
ProcessRec$ = Rec$

End Function




Sub CreateTempFile(FileNum, FileName$, RecLen, Title$, NumSeq)
' Creates a Test File
LOCAL RecCont$, X,Q,T, StartPos
X=0:Q=0:T=0:StartPos =0
RecCont$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789"

open FileName$ for RANDOM as #Filenum
SEEK #Filenum,1
print #Filenum, pad$(RecLen), pad$(NumSeq), Title$ ' Reclen, num seq, file comment''
StartPos = 2000

For x = 1 to NumSeq
seek #Filenum,(x * 100)
Q = (INT(RND() * 20) +1)
print #Filenum, pad$(X),pad$(StartPos),pad$(Q) ' Seq Num, Start Rec,
print Filenum, pad$(X),pad$(StartPos),pad$(Q) ' Seq Num, Start Rec,
For t = 1 to Q
recInfo$ = left$(pad$(NumSeq) + pad$(t) + RecCont$,RecLen)
PutRec Filenum,RecLen,t,StartPos, recInfo$
next
StartPos = StartPos + (RecLen * q) + 100
Next
Close #FileNum
End Sub

main:


' Create Test Files
CreateTempFile 1, "Ears.dat", 64, "Ears", 4
Print
CreateTempFile 2, "Muzzle.dat", 76, "Muzzle", 6
'END

loadfile(1,"Ears.dat") ' Load File 1 Header
loadfile(2,"Muzzle.dat") ' Load File 2 Header
print FileElem(1,1),FileElem(1,2) ' Display File 1 Header
print FileElem(2,1),FileElem(2,2) ' Display File 2 Header



loadseq(1,1) ' load Seq 1 from File 1
loadseq(2,2) ' Load Seq 2 from File 2
print RecElem(1,1),RecElem(1,4),RecElem(1,5) ' Show Seq 1 Header
print RecElem(2,1),RecElem(2,4),RecElem(2,5) ' Show Seq 2 Header



' RecElem(2,2) = 10 ' Seed to start at Record 10 For Testing
For x = 1 to 10 ' Get 10 Records
' Print 1, RecElem(1,2), ProcessRec$(1)
Print RecElem(1,1), RecElem(1,2), ProcessRec$(1)

next

print RecElem(1,2), RecElem(2,2) ' Show Record Pointers

' Wait
input a$




Coding Coding Coding..
Keep those keyboards coding..
RAW CODE!!!!!
 
Print this page


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

© JAQ Software 2024