Posted: Fri 11 Nov 2011, 19:37
Hey big_bass,
Where's that dance?
r
Where's that dance?
r
Sorry, my baD. (mea culpa)Do they even allow dancing on this forum?
Hey VolhoutVolhout wrote:Big Bass,
Thanks for the links, but they are not very useful for me.
Your example (parallel port output) uses the SYSTEM command from BaCon to call a compiled C program. The other links show snippets of C functions that could be compiled to a library.
If I could program in C and compile it, I would probably do the whole project in C. This info is simply over my head (capacity)
Does make me feel stupid though...
Volhout
you used peek and poke and pure HEXBaCon driver for FTDI USB-SERIAL convertor chips. Uses compiled libraries
' from FTDI : libftdi-0.19. When compiled (./configure, make, make install)
' this library generates libftdi.so.1
Code: Select all
'-------------------------------------------------------------
'
' program to test the driver glue layer in ftdi_drv.bac
'
' 2011-09-02 Harm de Leeuw
'--------------------------------------------------------------
INCLUDE "ftdi_drv.bac"
LOCAL data TYPE unsigned char
LOCAL mask TYPE unsigned char
PRINT "connect to USB module with FT245BM"
'<D0,D3> output, <D4,D7> input
mask = 0x0F
ft245bm_open (mask)
PRINT "device connected, enabling bitbang mode"
PRINT "start banging"
FOR data = 0 TO 254
'write 0x00 .... 0xFE
ft245bm_poke (data)
'and read back, but <D4,D7> read actual input values
PRINT RIGHT$(HEX$(ft245bm_peek()),2)
NEXT data
PRINT "stop banging, disable bitbang mode"
ft245bm_close ()
END
Code: Select all
'---------------------------------------------------------------------------
' BaCon driver for FTDI USB-SERIAL convertor chips. Uses compiled libraries
' from FTDI : libftdi-0.19. When compiled (./configure, make, make install)
' this library generates libftdi.so.1
' 2011-09-02 Harm de Leeuw
'---------------------------------------------------------------------------
' Version
' 0.1 Supports open(mask), poke(byte), and close()
' 0.2 Added peek() function
'---------------------------------------------------------------------------
TRAP LOCAL
' Import some calls first
CONST library$ = "libftdi.so.1"
' Get the functions from the library
IMPORT ftdi_init(long) FROM library$ TYPE int
IMPORT ftdi_usb_open(long,int,int) FROM library$ TYPE int
IMPORT ftdi_get_error_string(long) FROM library$ TYPE char* ALIAS ftdi_get_error_string$
IMPORT ftdi_read_chipid(long,long) FROM library$ TYPE int
IMPORT ftdi_usb_close(long) FROM library$ TYPE int
IMPORT ftdi_deinit(long) FROM library$ TYPE void
IMPORT ftdi_write_data(long,long,int) FROM library$ TYPE int
IMPORT ftdi_set_bitmode(long,unsigned char,unsigned char) FROM library$ TYPE int
IMPORT ftdi_disable_bitbang(long) FROM library$ TYPE int
IMPORT ftdi_usb_get_strings(long,long,long,int,long,int,long,int) FROM library$ TYPE int
IMPORT ftdi_read_pins(long,long) FROM library$ TYPE int
CONST TYPE_AM = 0
CONST TYPE_BM = 1
CONST TYPE_2232C = 2
CONST TYPE_R = 3
CONST TYPE_2232H = 4
CONST TYPE_4232H = 5
CONST BITMODE = 0x01
' BaCon does not know types so we define
' a RECORD with the needed members
RECORD ftdic
' USB specific
LOCAL usb_dev TYPE long
LOCAL usb_read_timeout TYPE int
LOCAL usb_write_timeout TYPE int
' FTDI specific
LOCAL type TYPE int
LOCAL baudrate TYPE int
LOCAL bitbang_enabled TYPE unsigned char
LOCAL *readbuffer TYPE unsigned char
LOCAL readbuffer_offset TYPE unsigned int
LOCAL readbuffer_remaining TYPE unsigned int
LOCAL readbuffer_chunksize TYPE unsigned int
LOCAL writebuffer_chunksize TYPE unsigned int
LOCAL max_packet_size TYPE unsigned int
' FTDI FT2232C requirecments
LOCAL interface TYPE int
LOCAL index TYPE int
' Endpoints
LOCAL in_ep TYPE int
LOCAL out_ep TYPE int
' General
LOCAL bitbang_mode TYPE unsigned char
LOCAL eeprom_size TYPE int
LOCAL error_str TYPE char*
LOCAL async_usb_buffer TYPE char*
LOCAL async_usb_buffer_size TYPE unsigned int
END RECORD
'-----------------------------------------------------------------------
SUB ft245bm_open (unsigned char io_mask)
'-----------------------------------------------------------------------
' this function initializes the FTDI 245 BM chip attached to the USB bus
' of the PC in bitbang mode (8 bit wide IO port). The io_mask define the
' data direction ("1"=out, "0"=in).
' timing: every byte write takes 1 milisecond, regardless CPU speed.
IF ftdi_init(ADDRESS(ftdic)) < 0 THEN
PRINT "ftdi_init failed"
END 1
END IF
ret = ftdi_usb_open(ADDRESS(ftdic), 0x0403, 0x6001)
IF ret < 0 THEN
PRINT "unable to open ftdi device: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")"
END 1
END IF
ret = ftdi_set_bitmode(ADDRESS(ftdic), io_mask, BITMODE)
IF ret < 0 THEN
PRINT "unable to set bitmode: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")"
END 1
END IF
END SUB
'-----------------------------------------------------------------------
SUB ft245bm_poke (unsigned char data)
'-----------------------------------------------------------------------
' this function sends out a byte to the 8 bit wide io port of the ftdi
' 245bm chip. The actual data output is masked by the io_mask set in
' the init command.
ret = ftdi_write_data(ADDRESS(ftdic), ADDRESS(data), 1)
IF ret < 0 THEN
PRINT "unable to write: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")"
END 1
END IF
END SUB
'-----------------------------------------------------------------------
SUB ft245bm_close ()
'-----------------------------------------------------------------------
' this function disconnects the ftdi 245bm chip from the USB bus.
' first bitbang mode is disabled, putting the io pins in tristate.
ret = ftdi_disable_bitbang(ADDRESS(ftdic))
IF ret < 0 THEN
PRINT "unable to close ftdi device: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")"
END 1
END IF
ret = ftdi_usb_close(ADDRESS(ftdic))
IF ret < 0 THEN
PRINT "unable to close ftdi device: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")"
END 1
END IF
ftdi_deinit(ADDRESS(ftdic))
END SUB
'-----------------------------------------------------------------------
FUNCTION ft245bm_peek () TYPE unsigned char
'-----------------------------------------------------------------------
' this function reads the 8 io pins from the ftdi 245bm chip, and returns
' the value. The value read is depending on the mask set when opening the
' connection.
LOCAL data TYPE unsigned char
ret = ftdi_read_pins(ADDRESS(ftdic), ADDRESS(data))
IF ret < 0 THEN
PRINT "unable to read: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")"
END 1
END IF
RETURN data
END FUNCTION
'-----------------------------------------------------------------------
Hey VolhoutGood luck with it. You are free to post this anywhere on the BaCon website as a working solution. This location may not be the best place to have this posted.....
Code: Select all
INCLUDE "/usr/share/BaCon/hug_imports.bac"
'# File: /tmp/0 has the line: A = "aaa"
Set$ = "A"
Val$ = "AAA"
Cmd$ = CONCAT$("cat /tmp/0")
Cfg$ = CHOP$(EXEC$(Cmd$))
SPLIT Cfg$ BY NL$ TO Lines$ SIZE Lsize
OPEN /tmp/0 FOR WRITING AS file
FOR i = 0 TO Lsize - 1
SPLIT Lines$[i] BY " " TO Col$ SIZE size
IF Col$[0] = Set$ THEN
PRINT Col$[2], " ", Val$
IF Col$[2] != Val$ THEN
PRINT Lines$[i]
REPLACE$(Lines$[i], Col$[2], Val$)
END IF
END IF
' WRITELN Lines$[i] TO file
NEXT
CLOSE FILE file
Code: Select all
COLOR FG TO BLUE
PRINT
PRINT "================================="
PRINT " READ"
PRINT "/etc/rc.d/PUPSTATE"
PRINT "================================="
PRINT
COLOR RESET
OPEN "/etc/rc.d/PUPSTATE" FOR READING AS myfile
WHILE NOT(ENDFILE(myfile)) DO
READLN txt$ FROM myfile
IF NOT(ENDFILE(myfile)) THEN
'---remove all lines that are bash commented # ---'
SYSTEM ("sed /^#/d /etc/rc.d/PUPSTATE > /tmp/newPUPSTATE")
'---remove all lines that have bash single quotes ---'
SYSTEM ("sed \"s/'//g\" /tmp/newPUPSTATE > /tmp/newPUPSTATE2")
PRINT txt$,NL$;
ENDIF
WEND
CLOSE FILE myfile
PRINT "--------"
COLOR FG TO BLUE
PRINT
PRINT "================================="
PRINT " WROTE"
PRINT "/tmp/newPUPSTATE2"
PRINT "================================="
PRINT
COLOR RESET
'--- cat the file into an array using x$ --'
x$ = EXEC$("cat /tmp/newPUPSTATE2")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize
'--- this prints all arrays --'
FOR x = 0 TO mysize - 1
PRINT myarray$[x]
NEXT
COLOR FG TO GREEN
PRINT
PRINT "================================="
PRINT " READ arrays"
PRINT "from /tmp/newPUPSTATE2"
PRINT "================================="
PRINT
COLOR RESET
'--- this prints only the first array --'
PRINT myarray$[0]
'--- this prints only the scecond array --'
PRINT myarray$[1]
'--- this prints only the third array --'
PRINT myarray$[2]
Code: Select all
' read config. file setting
SUB READCFG(STRING Set$)
IF FILEEXISTS(CfgFile$) = TRUE THEN
Cmd$ = CONCAT$("grep ", Set$, " ", CfgFile$, "|cut -d' ' -f3")
Val$ = CHOP$(EXEC$(Cmd$))
END IF
END SUB
' write config. file setting
SUB WRITECFG(STRING Set$, STRING Val$)
Cmd$ = CONCAT$("echo \"$(<", CfgFile, ")\" |sed \"s/^", Set$, ".*$/", Set$, " = ", Val$, "/\"")
New$ = EXEC$(Cmd$)
SYSTEM CONCAT$("echo \"", New$, "\" > ", CfgFile$)
' PRINT New$
END SUB
Code: Select all
SUB OUTARRAY(STRING Txt$[Size], int Size)
OPEN File$ FOR WRITING AS file
FOR i = 0 TO Size-1
WRITELN Txt$[i] TO file
NEXT
CLOSE FILE file
END SUB