Skip to content

Commit eec0684

Browse files
author
Erin Peach
authored
Merge pull request code-dot-org#26715 from code-dot-org/cspx=pin-mapping
Circuit Playground Express Pin Mapping
2 parents 36212f0 + e2c8410 commit eec0684

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

apps/src/lib/kits/maker/CircuitPlaygroundBoard.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ process.hrtime = require('browser-process-hrtime');
2929
/** @const {number} serial port transfer rate */
3030
const SERIAL_BAUD = 57600;
3131

32+
/** Maps the Circuit Playground Express pins to Circuit Playground Classic*/
33+
const pinMapping = {"A0": 12, "A1": 6, "A2": 9, "A3": 10, "A4": 3, "A5": 2, "A6": 0, "A7": 1};
34+
35+
3236
/**
3337
* Controller interface for an Adafruit Circuit Playground board using
3438
* Circuit Playground Firmata firmware.
@@ -228,33 +232,39 @@ export default class CircuitPlaygroundBoard extends EventEmitter {
228232
.then(() => forEachLedInSequence(led => led.off(), 80));
229233
}
230234

235+
mappedPin(pin) {
236+
return pinMapping.hasOwnProperty(pin) ? pinMapping[pin] : pin;
237+
}
238+
231239
pinMode(pin, modeConstant) {
232-
this.fiveBoard_.pinMode(pin, modeConstant);
240+
this.fiveBoard_.pinMode(this.mappedPin(pin), modeConstant);
233241
}
234242

235243
digitalWrite(pin, value) {
236-
this.fiveBoard_.digitalWrite(pin, value);
244+
this.fiveBoard_.digitalWrite(this.mappedPin(pin), value);
237245
}
238246

239247
digitalRead(pin, callback) {
240-
this.fiveBoard_.digitalRead(pin, callback);
248+
this.fiveBoard_.digitalRead(this.mappedPin(pin), callback);
241249
}
242250

243251
analogWrite(pin, value) {
244-
this.fiveBoard_.analogWrite(pin, value);
252+
this.fiveBoard_.analogWrite(this.mappedPin(pin), value);
245253
}
246254

247255
analogRead(pin, callback) {
248-
this.fiveBoard_.analogRead(pin, callback);
256+
this.fiveBoard_.analogRead(this.mappedPin(pin), callback);
249257
}
250258

251259
createLed(pin) {
260+
pin = this.mappedPin(pin);
252261
const newLed = new Led({board: this.fiveBoard_, pin});
253262
this.dynamicComponents_.push(newLed);
254263
return newLed;
255264
}
256265

257266
createButton(pin) {
267+
pin = this.mappedPin(pin);
258268
const newButton = new Button({board: this.fiveBoard_, pin});
259269
this.dynamicComponents_.push(newButton);
260270
return newButton;

apps/test/unit/lib/kits/maker/CircuitPlaygroundBoardTest.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import experiments from '@cdo/apps/util/experiments';
1313
// Polyfill node process.hrtime for the browser, which gets used by johnny-five
1414
process.hrtime = require('browser-process-hrtime');
1515

16+
const xPins = ["A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7"];
17+
const classicPins = [12, 6, 9, 10, 3, 2, 0, 1];
18+
1619
describe('CircuitPlaygroundBoard', () => {
1720
let board, playground;
1821

@@ -275,6 +278,15 @@ describe('CircuitPlaygroundBoard', () => {
275278
expect(playground.pinMode).to.have.been.calledWith(pin, arg2);
276279
});
277280
});
281+
282+
it('forwards the call to firmata with the modified CPX value', () => {
283+
return board.connect().then(() => {
284+
const pin = xPins[0];
285+
const arg2 = 1023;
286+
board.pinMode(pin, arg2);
287+
expect(playground.pinMode).to.have.been.calledWith(classicPins[0], arg2);
288+
});
289+
});
278290
});
279291

280292
describe(`digitalWrite(pin, value)`, () => {
@@ -286,6 +298,15 @@ describe('CircuitPlaygroundBoard', () => {
286298
expect(playground.digitalWrite).to.have.been.calledWith(pin, arg2);
287299
});
288300
});
301+
302+
it('forwards the call to firmata with the modified CPX value', () => {
303+
return board.connect().then(() => {
304+
const pin = xPins[1];
305+
const arg2 = 1023;
306+
board.digitalWrite(pin, arg2);
307+
expect(playground.digitalWrite).to.have.been.calledWith(classicPins[1], arg2);
308+
});
309+
});
289310
});
290311

291312
describe(`digitalRead(pin, callback)`, () => {
@@ -297,6 +318,15 @@ describe('CircuitPlaygroundBoard', () => {
297318
expect(playground.digitalRead).to.have.been.calledWith(pin, arg2);
298319
});
299320
});
321+
322+
it('forwards the call to firmata with the modified CPX value', () => {
323+
return board.connect().then(() => {
324+
const pin = xPins[2];
325+
const arg2 = () => {};
326+
board.digitalRead(pin, arg2);
327+
expect(playground.digitalRead).to.have.been.calledWith(classicPins[2], arg2);
328+
});
329+
});
300330
});
301331

302332
describe(`analogWrite(pin, value)`, () => {
@@ -308,6 +338,15 @@ describe('CircuitPlaygroundBoard', () => {
308338
expect(playground.analogWrite).to.have.been.calledWith(pin, arg2);
309339
});
310340
});
341+
342+
it('forwards the call to firmata with the modified CPX value', () => {
343+
return board.connect().then(() => {
344+
const pin = xPins[3];
345+
const arg2 = 1023;
346+
board.analogWrite(pin, arg2);
347+
expect(playground.analogWrite).to.have.been.calledWith(classicPins[3], arg2);
348+
});
349+
});
311350
});
312351

313352
describe(`analogRead(pin, callback)`, () => {
@@ -319,6 +358,15 @@ describe('CircuitPlaygroundBoard', () => {
319358
expect(playground.analogRead).to.have.been.calledWith(pin, arg2);
320359
});
321360
});
361+
362+
it('forwards the call to firmata with the modified CPX value', () => {
363+
return board.connect().then(() => {
364+
const pin = xPins[4];
365+
const arg2 = () => {};
366+
board.analogRead(pin, arg2);
367+
expect(playground.analogRead).to.have.been.calledWith(classicPins[4], arg2);
368+
});
369+
});
322370
});
323371

324372
describe(`boardConnected()`, () => {
@@ -349,6 +397,14 @@ describe('CircuitPlaygroundBoard', () => {
349397
expect(newLed).to.be.an.instanceOf(Led);
350398
});
351399
});
400+
401+
it('uses the express pin value to make an LED controller with the classic pin value', () => {
402+
return board.connect().then(() => {
403+
const pin = xPins[5];
404+
const newLed = board.createLed(pin);
405+
expect(newLed.pin).to.equal(classicPins[5]);
406+
});
407+
});
352408
});
353409

354410
describe(`createButton(pin)`, () => {
@@ -360,6 +416,14 @@ describe('CircuitPlaygroundBoard', () => {
360416
});
361417
});
362418

419+
it('uses the express pin value to make a button controller with the classic pin value', () => {
420+
return board.connect().then(() => {
421+
const pin = xPins[6];
422+
const newLed = board.createButton(pin);
423+
expect(newLed.pin).to.equal(classicPins[6]);
424+
});
425+
});
426+
363427
it('configures the controller as a pullup if passed an external pin', () => {
364428
return board.connect().then(() => {
365429
EXTERNAL_PINS.forEach((pin) => {
@@ -380,4 +444,12 @@ describe('CircuitPlaygroundBoard', () => {
380444
});
381445
});
382446
});
447+
448+
describe(`mappedPin(pin)`, () => {
449+
it(`returns the Classic pin value of the provided Express pin value`, () =>{
450+
for (let i = 0; i < xPins.length; i++) {
451+
expect(board.mappedPin(xPins[i])).to.equal(classicPins[i]);
452+
}
453+
});
454+
});
383455
});

0 commit comments

Comments
 (0)