@@ -132,27 +132,32 @@ namespace cyberbot {
132
132
133
133
let isConnected = false ;
134
134
135
- // Make sure the propeller chips is properly connected
135
+ // Establish I2C communication between the micro:bit and cyber:bot boards
136
136
function connect ( ) {
137
-
138
137
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
+ }
148
150
break ;
149
151
}
150
152
}
153
+ pause ( 500 )
154
+ sendCommand ( null , null , HANDSHAKE , 0 , null , null ) ;
151
155
}
152
156
153
157
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 ) ;
156
161
control . reset ( ) ;
157
162
}
158
163
@@ -178,7 +183,11 @@ namespace cyberbot {
178
183
args = Buffer . concat ( [ args , freq ] ) ;
179
184
}
180
185
// 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
+ }
182
191
183
192
// build command and write
184
193
pins . i2cWriteBuffer ( ADDRESS , Buffer . fromArray ( [ 0 , cmd ] ) ) ;
0 commit comments