diff --git a/motor.ts b/motor.ts index 73428de..795b8c8 100644 --- a/motor.ts +++ b/motor.ts @@ -1,99 +1,89 @@ -enum Motor { +//% weight=13 color=#ffd43a icon="" block="Motor" +namespace cakLandMotor { + enum Motor { //% block="left" LEFT = 0, //% block="right" RIGHT = 1 -} - -enum MotorPower { - //% block="on" - ON = 1, - //% block="off" - OFF = 0 -} + } -//% weight=13 color=#ffd43a icon="" block="Motor" -namespace cakLandMotor { - const PWM_PERIOD = 1000; //totally arbitrary, testing showed no effect - let motorState: MotorPower = MotorPower.ON - - /** - *Turns the left motor at a specified speed - */ + /** + *Turns the left motor at a specified speed + */ //% block //% blockId=motion_turn_left block="turn left motor at |speed: %speed" //% speed.min=-100 speed.max=100 //% weight=60 export function turnLeft(speed: number): void { - drive(speed, 0); + drive(speed, 0); } - /** - *Turns the right motor at a specified speed - */ - //% block - //% blockId=motion_turn_right block="turn right motor at |speed: %speed" - //% speed.min=-100 speed.max=100 - //% weight=50 - export function turnRight(speed: number): void { - drive(0, speed); - } + /** + *Turns the right motor at a specified speed + */ + //% block + //% blockId=motion_turn_right block="turn right motor at |speed: %speed" + //% speed.min=-100 speed.max=100 + //% weight=50 + export function turnRight(speed: number): void { + drive(0, speed); + } - /** - *Stop the motors - */ - //% block - //% blockId=motion_stop block="stop motors" - //% weight=45 - export function stop(): void { - drive(0, 0); - } + /** + *Stop the motors + */ + //% block + //% blockId=motion_stop block="stop motors" + //% weight=45 + export function stop(): void { + drive(0, 0); + } - /** - * Control both wheels in one function. - * Speeds range from -100 to 100. - * Negative speeds go backwards, positive go forwards. - */ - //% block - //% blockId=motion_drive block="drive |left: %leftWheelSpeed|right: %rightWheelSpeed" - //% leftWheelSpeed.min=-100 leftWheelSpeed.max=100 - //% rightWheelSpeed.min=-100 rightWheelSpeed.max=100 - //% weight=40 - export function drive(leftWheelSpeed: number, rightWheelSpeed: number): void { - motorControl(Motor.LEFT, leftWheelSpeed) - motorControl(Motor.RIGHT, rightWheelSpeed) - } + /** + * Control both wheels in one function. + * Speeds range from -100 to 100. + * Negative speeds go backwards, positive go forwards. + */ + //% block + //% blockId=motion_drive block="drive |left: %leftWheelSpeed|right: %rightWheelSpeed" + //% leftWheelSpeed.min=-100 leftWheelSpeed.max=100 + //% rightWheelSpeed.min=-100 rightWheelSpeed.max=100 + //% weight=40 + export function drive(leftWheelSpeed: number, rightWheelSpeed: number): void { + motorControl(Motor.LEFT, leftWheelSpeed) + motorControl(Motor.RIGHT, rightWheelSpeed) + } - /** - * Advanced control of an individual motor. PWM is set to constant value. - */ - function motorControl(whichMotor: Motor, speed: number): void { - // Pick the motor using some magic values - let [pos, neg] = selectMotor(whichMotor); + /** + * Advanced control of an individual motor. PWM is set to constant value. + */ + function motorControl(whichMotor: Motor, speed: number): void { + // Pick the motor using some magic values + let [pos, neg] = selectMotor(whichMotor); - // drive motors - let remappedSpeed = speed * 10; - // forward: power to neg, reverse: power to pos - pins.analogWritePin(neg, 1023 - Math.abs(Math.max(remappedSpeed, 0))); - pins.analogWritePin(pos, 1023 - Math.abs(Math.min(0, remappedSpeed))); - } + // drive motors + let remappedSpeed = speed * 10; + // forward: power to neg, reverse: power to pos + pins.analogWritePin(neg, 1023 - Math.abs(Math.max(remappedSpeed, 0))); + pins.analogWritePin(pos, 1023 - Math.abs(Math.min(0, remappedSpeed))); + } - function selectMotor(whichMotor: Motor): AnalogPin[] { - let pos : AnalogPin, neg : AnalogPin; - switch (whichMotor) { - case Motor.LEFT: - pos = cakLand.M1_POS; - neg = cakLand.M1_NEG; - break; - case Motor.RIGHT: - pos = cakLand.M2_POS; - neg = cakLand.M2_NEG; - break; - default: - pos = cakLand.M1_POS; - neg = cakLand.M1_NEG; - break; - }; - return [pos, neg]; - } + function selectMotor(whichMotor: Motor): AnalogPin[] { + let pos : AnalogPin, neg : AnalogPin; + switch (whichMotor) { + case Motor.LEFT: + pos = cakLand.M1_POS; + neg = cakLand.M1_NEG; + break; + case Motor.RIGHT: + pos = cakLand.M2_POS; + neg = cakLand.M2_NEG; + break; + default: + pos = cakLand.M1_POS; + neg = cakLand.M1_NEG; + break; + }; + return [pos, neg]; + } } diff --git a/pump.ts b/pump.ts index 741d3cb..7c9a40e 100644 --- a/pump.ts +++ b/pump.ts @@ -1,75 +1,75 @@ -enum Pump { +//% weight=13 color=#01579b icon="" block="Pump" +namespace cakLandPump { + enum Pump { //% block="left" LEFT = 0, //% block="right" RIGHT = 1 -} + } -//% weight=13 color=#01579b icon="" block="Pump" -namespace cakLandPump { - /** - * Start the pump - */ - //% block - //% blockId=pump_start block="start %pump pump at speed %speed" - //% speed.min=0 speed.max=100 - //% weight=45 - export function start(pump: Pump, speed: number): void { - pumpControl(pump, speed) - } + /** + * Start the pump + */ + //% block + //% blockId=pump_start block="start %pump pump at speed %speed" + //% speed.min=0 speed.max=100 + //% weight=45 + export function start(pump: Pump, speed: number): void { + pumpControl(pump, speed) + } - /** - * Stop the pump - */ - //% block - //% blockId=pump_stop block="stop %pump pump" - //% weight=45 - export function stop(pump: Pump): void { - pumpControl(pump, 0) - } + /** + * Stop the pump + */ + //% block + //% blockId=pump_stop block="stop %pump pump" + //% weight=45 + export function stop(pump: Pump): void { + pumpControl(pump, 0) + } - /** - * Set a pump for a specified time at a specified speed. - */ - //% block - //% blockId=pump_duration block="run %pump pump at speed %speed for %duration seconds" - //% duration.min=0 duration.max=10 - //% speed.min=0 speed.max=100 - //% weight=45 - export function startDuration(pump: Pump, speed: number, duration: number): void { - start(pump, speed) - basic.pause(duration*1000) - stop(pump) - } + /** + * Set a pump for a specified time at a specified speed. + */ + //% block + //% blockId=pump_duration block="run %pump pump at speed %speed for %duration seconds" + //% duration.min=0 duration.max=10 + //% speed.min=0 speed.max=100 + //% weight=45 + export function startDuration(pump: Pump, speed: number, duration: number): void { + start(pump, speed) + basic.pause(duration*1000) + stop(pump) + } - /** - * Advanced control of an individual pump. PWM is set to constant value. - */ - function pumpControl(whichPump: Pump, speed: number): void { - let pumpSpeed: number + /** + * Advanced control of an individual pump. PWM is set to constant value. + */ + function pumpControl(whichPump: Pump, speed: number): void { + let pumpSpeed: number - pumpSpeed = remapSpeed(speed) + pumpSpeed = remapSpeed(speed) - if (whichPump == Pump.LEFT) { - pins.analogSetPeriod(cakLand.M1_NEG, 1024) - pins.analogWritePin(cakLand.M1_NEG, pumpSpeed) - } else { - pins.analogSetPeriod(cakLand.M2_NEG, 1024) - pins.analogWritePin(cakLand.M2_NEG, pumpSpeed) - } + if (whichPump == Pump.LEFT) { + pins.analogSetPeriod(cakLand.M1_NEG, 1024) + pins.analogWritePin(cakLand.M1_NEG, pumpSpeed) + } else { + pins.analogSetPeriod(cakLand.M2_NEG, 1024) + pins.analogWritePin(cakLand.M2_NEG, pumpSpeed) } + } - // Rescale values from 0 - 100 to 0 - 1023 - function remapSpeed(s: number): number { - let returnSpeed: number - if (s <= 0) { - returnSpeed = 0 - } else if (s >= 100) { - returnSpeed = 1023 - } else { - returnSpeed = (23200 + (s * 791)) / 100 - } - return returnSpeed; + // Rescale values from 0 - 100 to 0 - 1023 + function remapSpeed(s: number): number { + let returnSpeed: number + if (s <= 0) { + returnSpeed = 0 + } else if (s >= 100) { + returnSpeed = 1023 + } else { + returnSpeed = (23200 + (s * 791)) / 100 } + return returnSpeed; + } } diff --git a/servo.ts b/servo.ts index c5a876d..8033a95 100644 --- a/servo.ts +++ b/servo.ts @@ -1,4 +1,6 @@ -enum Position { +//% weight=10 color=#ab47bc icon="" block="Servos" +namespace cakLandServos { + enum Position { //% block="up" UP = 85, //% block="half up" @@ -9,32 +11,30 @@ enum Position { HALF_DOWN = -40, //% block="down" DOWN = -85 -} + } -//% weight=10 color=#ab47bc icon="" block="Servos" -namespace cakLandServos { - export enum ServoPin{ - P0, - P1, - P2, - P8, - P12 - } + export enum ServoPin{ + P0, + P1, + P2, + P8, + P12 + } - export enum MotorPin{ - //% block="left" - Left, - //% block="right" - Right - } + export enum MotorPin{ + //% block="left" + Left, + //% block="right" + Right + } - export enum Power { - //% block="off" - Off = 0, - //% block="on" - On = 1, - } + export enum Power { + //% block="off" + Off = 0, + //% block="on" + On = 1, + } /** * Enable a motor pin for access to 5v instead of the 3.3v on the rest of the breakout board @@ -43,32 +43,31 @@ namespace cakLandServos { //% blockId=servo_set_power block="turn %side motor + pin %state" //% weight=100 export function motorPinPower (side: MotorPin, state: Power): void { - let pin : DigitalPin; - // Pick a pin - switch (side) { - case MotorPin.Left: pin = cakLand.M1_POS; break; - case MotorPin.Right: pin = cakLand.M2_POS; break; - default: pin = null; break; - } - if (pin) { - pins.digitalWritePin(pin, state); - } + let pin : DigitalPin; + // Pick a pin + switch (side) { + case MotorPin.Left: pin = cakLand.M1_POS; break; + case MotorPin.Right: pin = cakLand.M2_POS; break; + default: pin = null; break; + } + if (pin) { + pins.digitalWritePin(pin, state); + } } /** * Function used to return actual AnalogPin from enum */ function getServoPin(pin: ServoPin): AnalogPin { - // Read from the pin specified from arg - switch (pin) { - case ServoPin.P0: return AnalogPin.P0 - case ServoPin.P1: return AnalogPin.P1 - case ServoPin.P2: return AnalogPin.P2 - case ServoPin.P8: return AnalogPin.P8 - case ServoPin.P12: return AnalogPin.P12 - } - // Default return if something goes wrong. - return AnalogPin.P0 + // Read from the pin specified from arg + switch (pin) { + case ServoPin.P0: return AnalogPin.P0; + case ServoPin.P1: return AnalogPin.P1; + case ServoPin.P2: return AnalogPin.P2; + case ServoPin.P8: return AnalogPin.P8; + case ServoPin.P12: return AnalogPin.P12; + default: AnalogPin.P0; + } } /** @@ -78,8 +77,8 @@ namespace cakLandServos { //% blockId=servo_set_pos block="set servo at %pin to |position: %position" //% weight=60 export function setServoPosition(pin: ServoPin, position: Position) { - let n: number = position - pins.servoWritePin(getServoPin(pin), -n + 90) + let n: number = position + pins.servoWritePin(getServoPin(pin), -n + 90) } /** @@ -89,7 +88,7 @@ namespace cakLandServos { //% blockId=servos_reset block="reset servo at %pin" //% weight=40 export function resetServos(pin: ServoPin) { - pins.servoWritePin(getServoPin(pin), 90) + pins.servoWritePin(getServoPin(pin), 90) } /** @@ -101,6 +100,6 @@ namespace cakLandServos { //% degrees.min=-90 degrees.max=90 //% weight=60 export function turnServo(pin: ServoPin, degrees: number) { - pins.servoWritePin(getServoPin(pin), -degrees + 90) + pins.servoWritePin(getServoPin(pin), -degrees + 90) } } diff --git a/soil.ts b/soil.ts index 4c27f1a..b1ee0a4 100644 --- a/soil.ts +++ b/soil.ts @@ -1,4 +1,6 @@ -enum Mlevel { +//% weight=13 color=#ff6700 icon="" block="Soil" +namespace cakLandSoil { + enum Mlevel { //% block="Very Wet" VERY_WET = 500, //% block="Wet" @@ -7,74 +9,67 @@ enum Mlevel { DRY = 100, //% block="Very Dry" VERY_DRY = 0 -} + } -//% weight=13 color=#ff6700 icon="" block="Soil" -namespace cakLandSoil { - export enum SoilPin { - P0, - P1, - P2 - } + export enum SoilPin { + P0, + P1, + P2 + } - /** - * Function used to return actual AnalogPin from enum - */ - function getSoilPin(pin: SoilPin): AnalogPin { - // Read from the pin specified from arg - switch (pin) { - case SoilPin.P0: return AnalogPin.P0 - case SoilPin.P1: return AnalogPin.P1 - case SoilPin.P2: return AnalogPin.P2 - } - // Default return if something goes wrong. - return AnalogPin.P0 + /** + * Function used to return actual AnalogPin from enum + */ + function getSoilPin(pin: SoilPin): AnalogPin { + // Read from the pin specified from arg + switch (pin) { + case SoilPin.P0: return AnalogPin.P0; + case SoilPin.P1: return AnalogPin.P1; + case SoilPin.P2: return AnalogPin.P2; + default: return AnalogPin.P0; } + } - /** - * Returns the value of the moisture sensor at a specific pin. - */ - //% block="moisture level at pin $pin" - export function getMoisture(pin: SoilPin): number { - return pins.analogReadPin(getSoilPin(pin)) - } + /** + * Returns the value of the moisture sensor at a specific pin. + */ + //% block="moisture level at pin $pin" + export function getMoisture(pin: SoilPin): number { + return pins.analogReadPin(getSoilPin(pin)) + } - /** - * Returns a boolean value based on if the moisture sensor at a specific pin is in a specified range of moisture. - */ - //% block="moisture level at pin $pin is %Mlevel" - export function ifMoisture(pin: SoilPin, level: Mlevel): boolean { - let moisture = getMoisture(pin); - switch (level) { - case Mlevel.VERY_DRY: { - return (moisture >= 0 && moisture <= 100); - break; - } - case Mlevel.DRY: { - return (moisture > 100 && moisture <= 200); - break; - } - case Mlevel.WET: { - return (moisture > 200 && moisture <= 500); - break; - } - case Mlevel.VERY_WET: { - return (moisture > 500); - } - } - return false; + /** + * Returns a boolean value based on if the moisture sensor at a specific pin is in a specified range of moisture. + */ + //% block="moisture level at pin $pin is %Mlevel" + export function ifMoisture(pin: SoilPin, level: Mlevel): boolean { + let moisture = getMoisture(pin); + switch (level) { + case Mlevel.VERY_DRY: { + return (moisture >= 0 && moisture <= 100); + } + case Mlevel.DRY: { + return (moisture > 100 && moisture <= 200); + } + case Mlevel.WET: { + return (moisture > 200 && moisture <= 500); + } + case Mlevel.VERY_WET: { + return (moisture > 500); + } } + } - /** - * Displays the read moisture level on a vertical bar graph up to full scale. - */ - //% block="display moisture level at %pin" - //% weight=40 - export function displayMoisture(pin: SoilPin): void { - led.plotBarGraph(getMoisture(pin), 800) - } + /** + * Displays the read moisture level on a vertical bar graph up to full scale. + */ + //% block="display moisture level at %pin" + //% weight=40 + export function displayMoisture(pin: SoilPin): void { + led.plotBarGraph(getMoisture(pin), 800) + } } diff --git a/touch.ts b/touch.ts index bd17622..383aa27 100644 --- a/touch.ts +++ b/touch.ts @@ -1,65 +1,63 @@ //% weight=10 color=#007A4B icon="" block="Touch" namespace cakLandTouch { - export enum TouchPin { - P0, - P1, - P2, - P8, - P12 - } + export enum TouchPin { + P0, + P1, + P2, + P8, + P12 + } - // array of currently latched pins. Push to latch, remove to unlatch - let latchClosed: TouchPin[] = []; + // array of currently latched pins. Push to latch, remove to unlatch + let latchClosed: TouchPin[] = []; - /** - * Function used to return actual DigitalPin from enum - */ - function getTouchPin(pin: TouchPin): DigitalPin { - // Read from the pin specified from arg - switch (pin) { - case TouchPin.P0: return DigitalPin.P0 - case TouchPin.P1: return DigitalPin.P1 - case TouchPin.P2: return DigitalPin.P2 - case TouchPin.P8: return DigitalPin.P8 - case TouchPin.P12: return DigitalPin.P12 - } - // Default return if something goes wrong. - return DigitalPin.P0 + /** + * Function used to return actual DigitalPin from enum + */ + function getTouchPin(pin: TouchPin): DigitalPin { + // Read from the pin specified from arg + switch (pin) { + case TouchPin.P0: return DigitalPin.P0; + case TouchPin.P1: return DigitalPin.P1; + case TouchPin.P2: return DigitalPin.P2; + case TouchPin.P8: return DigitalPin.P8; + case TouchPin.P12: return DigitalPin.P12; + default: return DigitalPin.P0; } + } - /** - * Returns the value of the touch sensor at a specific pin. - */ - //% block="on touch at $pin" - export function getTouch(pin: TouchPin): boolean { - // Return bool associated with the pin reading - if (pins.digitalReadPin(getTouchPin(pin)) > 0) { - return true - } - else { - return false - } + /** + * Returns the value of the touch sensor at a specific pin. + */ + //% block="on touch at $pin" + export function getTouch(pin: TouchPin): boolean { + // Return bool associated with the pin reading + if (pins.digitalReadPin(getTouchPin(pin)) > 0) { + return true + } else { + return false } + } - /** - * Debounced touch control, using flip-flop logic. - */ - //% block="tap at $pin" - export function getTap(pin: TouchPin): boolean { - let pinStatus = getTouch(pin); - let latchStatus = latchClosed.some(p => p == pin); - // Push to latch, remove to unlatch + /** + * Debounced touch control, using flip-flop logic. + */ + //% block="tap at $pin" + export function getTap(pin: TouchPin): boolean { + let pinStatus = getTouch(pin); + let latchStatus = latchClosed.some(p => p == pin); + // Push to latch, remove to unlatch - if (pinStatus && !latchStatus) { - // touch and unlatched: register touch, close latch - latchClosed.push(pin); - return true; - } else if (!pinStatus && latchStatus) { - // no touch and latched: register no touch, open latch - latchClosed = latchClosed.filter(p => p != pin); - } - return false; - // touch on closed latch, or no touch: register no touch + if (pinStatus && !latchStatus) { + // touch and unlatched: register touch, close latch + latchClosed.push(pin); + return true; + } else if (!pinStatus && latchStatus) { + // no touch and latched: register no touch, open latch + latchClosed = latchClosed.filter(p => p != pin); } + return false; + // touch on closed latch, or no touch: register no touch + } }