Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By sandfrog
#37443 Hello,

Here a Simple set of I2C-Tools: I2C-Scanner, I2C-Read, i2C-Write for Tests with I2C-Drives.

Done..

For ESPBASIC < 3.0
Code: Select all[clear]
cls
print "I2C-Tool:  "
button "Clear Screen" [clear]
button "Scan for Drives" [scan]
button "Read from Drive" [read]
button "Write to Drive" [write]
button "Programm Exit" [exit]
print ""
wait
'
[write]
wprint "Adresse (dec): "
let addr = 73
textbox addr
wprint " Write Register: "
let reg = 0
textbox reg
wprint "Bytes (48 4f):"
let bytes = "ff"
textbox bytes
button "Run" [wrun]
print ""
wait
'
[wrun]
i2c.begin(addr)
i2c.write(reg)
i2c.write(bytes)
i2c.end
wprint "Write "
wprint bytes
wprint " to "
wprint reg
print ""
wait
'
[read]
wprint "Adresse (dec): "
let addr = 73
textbox addr
wprint " Read Register: "
let reg = 0
textbox reg
wprint " Read Bytes: "
let byte = 1
textbox byte
button "Run" [rrun]
print ""
wait
'
[rrun]
byte = int(byte)
let data = ""
i2c.begin(addr)
i2c.write(reg)
i2c.end()
i2c.requestfrom(addr,byte)
for x = 1 to byte
data = data & "["
data = data & int(x)
data = data & "]"
data = data & i2c.read()
next x
wprint "Read:"
wprint data
print ""
wait
'
[scan]
wprint "Found: "
for addr = 1 to 119
i2c.begin(addr)
i2c.write(0)
i2c.end()
i2c.requestfrom(addr,1)
data = i2c.read()
if data <> "-1" then wprint hex(addr)
if data <> "-1" then wprint "/"
if data <> "-1" then wprint int(addr)
if data <> "-1" then wprint " "
next addr
print ""
wait
'
[exit]
end


For ESPBASIC > 3.0
Code: Select all[Setup]
i2c.setup(5, 4)
'
[clear]
cls
print "I2C-Tool:  "
button "Clear Screen", [clear]
button "Scan for Drives", [scan]
button "Read from Drive", [read]
button "Write to Drive", [write]
button "Programm Exit", [exit]
print ""
wait
'
[write]
wprint "Adresse (dec): "
let addr = 73
textbox addr
wprint " Write Register: "
let reg = 0
textbox reg
wprint "Bytes (48 4f):"
let bytes = "ff"
textbox bytes
button "Run", [wrun]
print ""
wait
'
[wrun]
i2c.begin(addr)
i2c.write(reg)
i2c.write(bytes)
i2c.end
wprint "Write "
wprint bytes
wprint " to "
wprint reg
print ""
wait
'
[read]
wprint "Adresse (dec): "
let addr = 72
textbox addr
wprint " Read Register: "
let reg = 0
textbox reg
wprint " Read Bytes: "
let byte = 1
textbox byte
button "Run", [rrun]
print ""
wait
'
[rrun]
byte = int(byte)
let data = ""
i2c.begin(addr)
i2c.write(reg)
i2c.end()
i2c.requestfrom(addr,byte)
for x = 1 to byte
data = data & "["
data = data & int(x)
data = data & "]"
data = data & i2c.read()
next x
wprint "Read:"
wprint data
print ""
wait
'
[scan]
wprint "Found: "
for addr = 1 to 119
i2c.begin(addr)
i2c.write(0)
i2c.end()
i2c.requestfrom(addr,1)
data = i2c.read()
if data <> "-1" then wprint hex(addr)
if data <> "-1" then wprint "/"
if data <> "-1" then wprint int(addr)
if data <> "-1" then wprint " "
next addr
print ""
wait
'
[exit]
end
Last edited by sandfrog on Thu Feb 02, 2017 4:07 pm, edited 3 times in total.
User avatar
By viscomjim
#37480 Hi Sandfrog, VERY NICE!!!! This will make a nice tool and shows some good programming reference. I have a question... why do you use byte = int(byte)? I was under the impression that espbasic did not need this. Did you have issues without using this line of code?

Thanks again, great work!
User avatar
By sandfrog
#37482 Its my Workaround to make a Number from 1.00 to 1, the I2C Rotines not work whit this String.

a = 1 + 1 is 2.00
int(a) is 2
User avatar
By viscomjim
#37488 Ahhh!!! That is excellent information for future programs. Good to know...

Thanks again, will be trying it out.