Skip to content

Commit af6d670

Browse files
authored
Merge pull request #7 from parallaxinc/fix-issue-6-i2c-start-unreliable
Fix issue 6 i2c start unreliable
2 parents 09ff2d9 + abd90be commit af6d670

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

cbcore.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,32 @@ namespace cyberbot {
132132

133133
let isConnected = false;
134134

135-
// Make sure the propeller chips is properly connected
135+
// Establish I2C communication between the micro:bit and cyber:bot boards
136136
function connect() {
137-
138137
while (true) {
139-
pins.digitalWritePin(DigitalPin.P8, 0)
140-
pins.digitalWritePin(DigitalPin.P8, 1)
141-
pause(10);
142-
if (pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE) !== 0) {
143-
//pins.digitalWritePin(DigitalPin.P8, 1)
144-
pause(10);
145-
pins.i2cWriteNumber(ADDRESS, 12, NumberFormat.UInt16LE);
146-
pause(10);
147-
isConnected = true;
138+
let a = pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE)
139+
if( a != 0){
140+
pins.setPull(DigitalPin.P8, PinPullMode.PullUp);
141+
pause(10)
142+
pins.i2cWriteNumber(ADDRESS, 12, NumberFormat.Int8BE)
143+
while(true){
144+
let b = pins.i2cReadNumber(ADDRESS, NumberFormat.UInt16LE)
145+
if(b != 0){
146+
isConnected = true
147+
break
148+
}
149+
}
148150
break;
149151
}
150152
}
153+
pause(500)
154+
sendCommand(null, null, HANDSHAKE, 0, null, null);
151155
}
152156

153157
function botDisable(): void {
154-
pins.setPull(DigitalPin.P8, PinPullMode.PullNone);
155-
basic.pause(200);
158+
pins.setPull(DigitalPin.P8, PinPullMode.PullDown);
159+
isConnected = false
160+
pause(200);
156161
control.reset();
157162
}
158163

@@ -178,7 +183,11 @@ namespace cyberbot {
178183
args = Buffer.concat([args, freq]);
179184
}
180185
// console.log(args.toHex())
181-
pins.i2cWriteBuffer(ADDRESS, args);
186+
let sca = pins.i2cWriteBuffer(ADDRESS, args);
187+
if(sca==-1010){
188+
isConnected = false
189+
botDisable()
190+
}
182191

183192
// build command and write
184193
pins.i2cWriteBuffer(ADDRESS, Buffer.fromArray([0, cmd]));

pxt.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cyberbot",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "Makecode extension to add blocks for use with the Cyberbot",
55
"license": "MIT",
66
"dependencies": {

0 commit comments

Comments
 (0)