Skip to content

Commit

Permalink
Merge pull request #3 from pelikhan/master
Browse files Browse the repository at this point in the history
Jacdac Extension
  • Loading branch information
JackAtKitronik authored Aug 18, 2022
2 parents 2141e36 + ff2fab6 commit 8277f05
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions jacdac/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Jacdac blocks for the Kitronik motor driver
59 changes: 59 additions & 0 deletions jacdac/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//% deprecated
namespace kitronik_motor_driver { }

namespace modules {
/**
* Motor 1 client
*/
//% fixedInstance whenUsed block="kitronik motor1"
export const kitronikMotor1 = new MotorClient("kitronik motor1?dev=self&srvo=0")

/**
* Motor 2 client
*/
//% fixedInstance whenUsed block="kitronik motor2"
export const kitronikMotor2 = new MotorClient("kitronik motor2?dev=self&srvo=1")
}

namespace servers {
class MotorServer extends jacdac.Server {
motor: kitronik_motor_driver.Motors
speed: number
enabled: boolean

constructor(motor: kitronik_motor_driver.Motors) {
super(jacdac.SRV_MOTOR)
this.motor = motor
this.enabled = false

kitronik_motor_driver.motorOff(this.motor)
}

handlePacket(pkt: jacdac.JDPacket) {
this.handleRegValue(pkt, jacdac.MotorReg.Reversible, jacdac.MotorRegPack.Reversible, true)
this.speed = this.handleRegValue(pkt, jacdac.MotorReg.Speed, jacdac.MotorRegPack.Speed, this.speed)
this.enabled = this.handleRegBool(pkt, jacdac.MotorReg.Enabled, this.enabled)

this.sync()
}

sync() {
if (!this.enabled)
kitronik_motor_driver.motorOff(this.motor)
else {
const direction = this.speed < 0 ? kitronik_motor_driver.MotorDirection.Reverse
: kitronik_motor_driver.MotorDirection.Forward
kitronik_motor_driver.motorOn(this.motor, direction, Math.abs(this.speed))
}
}
}
function start() {
jacdac.productIdentifier = 0x31ee311d
jacdac.deviceDescription = "Kitronik Motor Driver"
jacdac.startSelfServers(() => [
new MotorServer(kitronik_motor_driver.Motors.Motor1),
new MotorServer(kitronik_motor_driver.Motors.Motor2)
])
}
start()
}
27 changes: 27 additions & 0 deletions jacdac/pxt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "kitronik-motor-driver-jacdac",
"version": "0.0.16",
"dependencies": {
"core": "*",
"radio": "*",
"microphone": "*",
"pxt-kitronik-motor-driver": "github:KitronikLtd/pxt-kitronik-motor-driver",
"jacdac": "github:microsoft/pxt-jacdac#v0.10.53",
"jacdac-motor": "github:microsoft/pxt-jacdac/motor#v0.10.53"
},
"files": [
"main.ts",
"README.md"
],
"testFiles": [
"test.ts"
],
"targetVersions": {
"target": "4.1.45",
"targetId": "microbit"
},
"supportedTargets": [
"microbit"
],
"preferredEditor": "tsprj"
}
14 changes: 14 additions & 0 deletions jacdac/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let speed = 0
let dv = 5
forever(() => {
modules.kitronikMotor1.run(speed)
modules.kitronikMotor2.run(- speed)

speed += dv
if (speed > 100) {
dv = -5
} else if (speed < -100) {
dv = 5
}
pause(250)
})

0 comments on commit 8277f05

Please sign in to comment.