Skip to content

Commit

Permalink
Support Calliope-Mini and allow adjust of IR timing
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphenkel committed Feb 8, 2018
1 parent e9641be commit 266f819
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pxt-powerfunctions - Power Functions IR Sender

Control your LEGO® Power Functions motors using your micro:bit, an infrared LED and MakeCode.
This package turns your micro:bit into a remote control for your Power Functions devices.
Control your LEGO® Power Functions motors using your micro:bit or Calliope-Mini, an infrared LED and MakeCode.
This package turns your device into a remote control for your Power Functions devices.

## Installation
Open MakeCode and add this package via Add Package in the Advanced menu. You need to enter our project URL https://github.com/philipphenkel/pxt-powerfunctions, hit return and then select the power-functions package.
Expand All @@ -14,7 +14,7 @@ Open MakeCode and add this package via Add Package in the Advanced menu. You nee

```javascript
basic.showIcon(IconNames.Heart)
powerfunctions.useIrLedPin(AnalogPin.P0)
powerfunctions.useIrLedPin(AnalogPin.P1)
powerfunctions.setMotorDirection(PowerFunctionsMotor.Blue1, PowerFunctionsDirection.Backward)

input.onButtonPressed(Button.A, () => {
Expand All @@ -26,10 +26,8 @@ input.onButtonPressed(Button.B, () => {
})

basic.forever(() => {
led.plotBarGraph(
input.lightLevel(),
255
)
led.plotBarGraph(input.lightLevel(), 255)

if (input.lightLevel() > 200) {
powerfunctions.float(PowerFunctionsMotor.Blue1)
basic.pause(5000)
Expand All @@ -52,4 +50,5 @@ Licensed under the MIT License (MIT). See LICENSE file for more details.
## Supported targets

* for PXT/microbit
* for PXT/calliope

3 changes: 2 additions & 1 deletion _locales/de/powerfunctions-strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"powerfunctions.float|block": "lasse Motor %motor | ausrollen",
"powerfunctions.setSpeed|block": "schalte | Motor %motor | auf %speed",
"powerfunctions.useIrLedPin|block": "nutze IR LED an Pin %pin",
"powerfunctions.setMotorDirection|block": "setze Richtung |von Motor %motor | auf %direction",
"powerfunctions.setMotorDirection|block": "setze Richtung | von Motor %motor | auf %direction",
"powerfunctions.adjustIrTiming|block": "konfiguriere Zeiten | der IR Marke %markMicroSeconds | und Pause %pauseMicroSeconds",
"powerfunctions|block": "Power Functions",
"{id:category}Power Functions": "Power Functions"
}
36 changes: 29 additions & 7 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Power Functions IR Sender
* Control your Power Functions motors using your PXT device, an infrared LED and MakeCode.
* Control your Power Functions motors using your micro:bit or Calliope-Mini, an infrared LED and MakeCode.
*
* (c) 2017-2018, Philipp Henkel
*/

/* Board specific configuration */
namespace BoardConfig {
export const DefaultPin = AnalogPin.P0;
export const DefaultPin = AnalogPin.P1;
export const MarkTimingCorrectionMicroSeconds = -65;
export const PauseTimingCorrectionMicroSeconds = -150;
}
Expand Down Expand Up @@ -82,7 +82,9 @@ namespace powerfunctions {
]

let irLed = BoardConfig.DefaultPin;

let markTimingCorrectionMicroSeconds = BoardConfig.MarkTimingCorrectionMicroSeconds;
let pauseTimingCorrectionMicroSeconds = BoardConfig.PauseTimingCorrectionMicroSeconds;

function getChannel(motor: PowerFunctionsMotor): PowerFunctionsChannel {
const MOTOR_TO_CHANNEL = [
PowerFunctionsChannel.One, PowerFunctionsChannel.Two, PowerFunctionsChannel.Three, PowerFunctionsChannel.Four,
Expand Down Expand Up @@ -112,7 +114,7 @@ namespace powerfunctions {
*/
//% blockId=pf_use_ir_led_pin
//% block="use IR LED on pin %pin"
//% weight=20
//% weight=30
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4 pin.fieldOptions.tooltips="false"
//% advanced=true
export function useIrLedPin(pin: AnalogPin) {
Expand All @@ -124,13 +126,33 @@ namespace powerfunctions {
*/
//% blockId=pf_set_motor_direction
//% block="set direction | of motor %motor | to %direction"
//% weight=10
//% weight=20
//% motor.fieldEditor="gridpicker" motor.fieldOptions.columns=4 motor.fieldOptions.tooltips="false"
//% advanced=true
export function setMotorDirection(motor: PowerFunctionsMotor, direction: PowerFunctionsDirection) {
motorDirections[motor] = direction
}

/**
* Adjust timing configuration to reach the required IR precision.
* Due to the overhead of function calls the sleep intervals during transmission of IR commands need to be shortened.
* Timing depends on both the device and the MakeCode version.
* Recommended default values are -65 micro seconds for the IR mark and -150 micro seconds for the pause.
*/
//% blockId=pf_adjust_ir_timing
//% block="adjust timing | of IR mark %markMicroSeconds | and pause %pauseMicroSeconds"
//% weight=10
//% markMicroSeconds.min=-157 markMicroSeconds.max=0
//% pauseMicroSeconds.min=-263 pauseMicroSeconds.max=0
//% advanced=true
export function adjustIrTiming(
markMicroSeconds: number = BoardConfig.MarkTimingCorrectionMicroSeconds,
pauseMicroSeconds: number = BoardConfig.PauseTimingCorrectionMicroSeconds)
{
markTimingCorrectionMicroSeconds = markMicroSeconds;
pauseTimingCorrectionMicroSeconds = pauseMicroSeconds;
}

/**
* Move a motor forward.
*/
Expand Down Expand Up @@ -261,9 +283,9 @@ namespace powerfunctions {

transmitBit(markMicroSeconds: number, pauseMicroSeconds: number): void {
pins.analogWritePin(this.pin, 511)
control.waitMicros(Math.max(1, markMicroSeconds + BoardConfig.MarkTimingCorrectionMicroSeconds))
control.waitMicros(Math.max(1, markMicroSeconds + markTimingCorrectionMicroSeconds))
pins.analogWritePin(this.pin, 0)
control.waitMicros(Math.max(1, pauseMicroSeconds + BoardConfig.PauseTimingCorrectionMicroSeconds))
control.waitMicros(Math.max(1, pauseMicroSeconds + pauseTimingCorrectionMicroSeconds))
}
}

Expand Down
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
powerfunctions.runTests()

basic.showIcon(IconNames.Heart)
powerfunctions.useIrLedPin(AnalogPin.P0)
powerfunctions.useIrLedPin(AnalogPin.P1)
powerfunctions.setMotorDirection(PowerFunctionsMotor.Blue1, PowerFunctionsDirection.Backward)

input.onButtonPressed(Button.A, () => {
Expand Down

0 comments on commit 266f819

Please sign in to comment.