JM 9/7/2017

What is firmata?

tfirmata is a Tcl implementation of Arduino Firmata:

The original tfirmata did not work for me on Windows7, I suspect there is an endless loop on the tfirmata::open cmd.

The enhanced version (by chw) worked just fine, with the following code as a minimum example:

Example #1 (Blinking LED on Arduino UNO)

 console show
 package require tfirmata
 set bd [tfirmata::open COM8]

 $bd mode 13 out

 for {set i 1} {$i <20} {incr i} {
    $bd dset 13 1
    tfirmata::sleep 250
    $bd dset 13 0
    tfirmata::sleep 250
    puts $i
 puts done...

Example #2

Writing to LCD with I2c module:
with something like this:

 # twi_LCD.tcl
 # Enables TWI pins, and writes a message
 # to LCD with I2C module
 console show
 package require tfirmata

 proc send_data {byte} {
 global bd addr
 set byte [string range $byte end-1 end]
 puts "byte: $byte"
 foreach {n1 n2} [split $byte ""] break
 $bd twiset $addr 0x${n1}5 0x${n1}1 0x${n2}5 0x${n2}1

 set bd [tfirmata::open COM8]
 $bd twiconfig

 set addr 0x27

 # LCD Initialization for 4-BIT bus
 $bd twiset $addr 0x34 0x30
 tfirmata::sleep 5
 # LCD Initialization for 4-BIT bus
 $bd twiset $addr 0x34 0x30 0x34 0x30 0x24 0x20 0x24 0x20
 # 2 Line LCD, 5x10 character
 $bd twiset $addr 0xc4 0xc0

 # 01 = Clear Display
 $bd twiset $addr 0x04 0x00 0x14 0x10
 # 0C = Display Control,Display ON
 $bd twiset $addr 0x04 0x00 0xc4 0xc0
 # 06 = Entry Mode Set, Auto Increment of cursor position
 $bd twiset $addr 0x04 0x00 0x64 0x60

 foreach letter [split "Tcl/Tk & Arduino" ""] {
 binary scan $letter "H2" valor
 send_data $valor

 $bd close

Notice how 2 bytes are required to write a single 4-bit value to the LCD.