Skip to content

Commit

Permalink
enum instead of string for service class names
Browse files Browse the repository at this point in the history
  • Loading branch information
tballmsft committed Nov 4, 2023
1 parent dec73a6 commit 2bd24b0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 95 deletions.
2 changes: 1 addition & 1 deletion editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ namespace microcode {
this.diskBtn.draw()
const wasVisible = this.connectBtn.visible()
this.connectBtn.setVisible(
jdc.numServiceInstances(jacs.serviceClass("dotMatrix")) == 0
jdc.numServiceInstances(jacs.ServiceClass.DotMatrix) == 0
)
if (wasVisible !== this.connectBtn.visible()) this.changed()
if (this.connectBtn.visible()) this.connectBtn.draw()
Expand Down
123 changes: 56 additions & 67 deletions jacs_topwriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace jacs {
const enablers = needsEnable()
if (enablers.indexOf(this.classIdentifier) >= 0) {
this.parent.emitSetReg(this, JD_REG_INTENSITY, hex`01`)
if (this.classIdentifier == serviceClass("radio")) {
if (this.classIdentifier == ServiceClass.Radio) {
// set group to 1
this.parent.emitSetReg(this, 0x80, hex`01`)
}
Expand Down Expand Up @@ -168,12 +168,11 @@ namespace jacs {
}
)
} else if (
this.classIdentifier == serviceClass("rotaryEncoder") ||
this.classIdentifier == serviceClass("temperature")
this.classIdentifier == ServiceClass.RotaryEncoder ||
this.classIdentifier == ServiceClass.Temperature
) {
const isRotary =
this.classIdentifier ==
serviceClass("rotaryEncoder")
this.classIdentifier == ServiceClass.RotaryEncoder
const sensorVar = isRotary
? this.parent.lookupGlobal("z_rotary" + this.index)
: this.parent.lookupGlobal("z_temp")
Expand Down Expand Up @@ -501,17 +500,7 @@ namespace jacs {
console.error("Error: " + msg)
}

lookupServiceClass(name: string) {
const id = serviceClass(name)
if (id === undefined) {
this.error(`service '${name}' not defined`)
return 0
}
return id
}

lookupRole(name: string, idx: number) {
const id = this.lookupServiceClass(name)
lookupRole(id: ServiceClass, idx: number) {
if (!id) return this.pageStartCondition
let ptr = 0
for (const r of this.roles) {
Expand All @@ -522,7 +511,7 @@ namespace jacs {
}
let r: Role
while (ptr <= idx) {
r = this.addRole(name + "_" + ptr, id)
r = this.addRole(scToName(id) + "_" + ptr, id)
ptr++
}
return r
Expand Down Expand Up @@ -733,7 +722,10 @@ namespace jacs {
const wr = this.writer
switch (microcode.jdKind(mod)) {
case microcode.JdKind.Temperature:
const temperatureRole = this.lookupRole("temperature", 0)
const temperatureRole = this.lookupRole(
ServiceClass.Temperature,
0
)
const temperatureVar = this.lookupGlobal("z_temp")
this.callLinked("round_temp", [temperatureRole.emit(wr)])
temperatureVar.write(wr, wr.emitExpr(Op.EXPR0_RET_VAL, []))
Expand Down Expand Up @@ -1319,7 +1311,7 @@ namespace jacs {
0200000000
0000000000
`
const scr = this.lookupRole("dotMatrix", 0)
const scr = this.lookupRole(ServiceClass.DotMatrix, 0)
this.callLinked("dot_animation", [
scr.emit(this.writer),
this.emitString(loading_anim),
Expand All @@ -1346,7 +1338,7 @@ namespace jacs {
this.emitLogString("MicroCode start!")

// turns off the microphone, if present
const mic = this.lookupRole("soundLevel", 0)
const mic = this.lookupRole(ServiceClass.SoundLevel, 0)
wr.emitIf(
wr.emitExpr(Op.EXPR1_ROLE_IS_CONNECTED, [mic.emit(wr)]),
() => {
Expand Down Expand Up @@ -1387,7 +1379,7 @@ namespace jacs {
deployEmpty() {
const mainProc = this.addProc("main")
// hack for car: stop cars on empty program
const role = this.lookupRole("radio", 0)
const role = this.lookupRole(ServiceClass.Radio, 0)
this.sendActuatorServiceCommand(
role,
0x81,
Expand All @@ -1399,7 +1391,7 @@ namespace jacs {

deploySound(name: string) {
const mainProc = this.addProc("main")
const r = this.lookupRole("soundPlayer", 0)
const r = this.lookupRole(ServiceClass.SoundPlayer, 0)
this.withProcedure(mainProc, wr => {
this.emitLoadBuffer(name)
this.emitSendCmd(r, 0x80)
Expand All @@ -1412,7 +1404,7 @@ namespace jacs {

deployFreq(buf: Buffer) {
const mainProc = this.addProc("main")
const r = this.lookupRole("buzzer", 0)
const r = this.lookupRole(ServiceClass.Buzzer, 0)
this.withProcedure(mainProc, wr => {
this.emitLoadBuffer(buf)
this.emitSendCmd(r, 0x80)
Expand All @@ -1426,62 +1418,59 @@ namespace jacs {

function needsWakeup() {
return [
{ classId: serviceClass("temperature"), convert: undefined },
{ classId: serviceClass("soundLevel"), convert: "sound_1_to_5" },
{ classId: serviceClass("accelerometer"), convert: undefined },
{ classId: serviceClass("lightLevel"), convert: "light_1_to_5" },
{ classId: ServiceClass.Temperature, convert: undefined },
{ classId: ServiceClass.SoundLevel, convert: "sound_1_to_5" },
{ classId: ServiceClass.Accelerometer, convert: undefined },
{ classId: ServiceClass.LightLevel, convert: "light_1_to_5" },
{
classId: serviceClass("potentiometer"),
classId: ServiceClass.Potentiometer,
convert: "slider_1_to_5",
},
{ classId: serviceClass("rotaryEncoder"), convert: undefined },
{ classId: ServiceClass.RotaryEncoder, convert: undefined },
{
classId: serviceClass("magneticFieldLevel"),
classId: ServiceClass.MagneticFieldLevel,
convert: "magnet_1_to_5",
},
]
}

function needsEnable() {
return [serviceClass("radio"), serviceClass("servo")]
return [ServiceClass.Radio, ServiceClass.Servo]
}

function scToName(sc: ServiceClass) {
if (sc == ServiceClass.Button) return "but"
if (sc == ServiceClass.DotMatrix) return "dot"
if (sc == ServiceClass.SoundLevel) return "snd"
if (sc == ServiceClass.Temperature) return "tmp"
if (sc == ServiceClass.SoundPlayer) return "mus"
if (sc == ServiceClass.Buzzer) return "buz"
if (sc == ServiceClass.Accelerometer) return "acc"
if (sc == ServiceClass.Radio) return "rad"
if (sc == ServiceClass.Potentiometer) return "pot"
if (sc == ServiceClass.LightLevel) return "lit"
if (sc == ServiceClass.MagneticFieldLevel) return "mag"
if (sc == ServiceClass.RotaryEncoder) return "rot"
if (sc == ServiceClass.Led) return "led"
if (sc == ServiceClass.Servo) return "srv"
return "unknown"
}

export function serviceClass(name: string): number {
switch (name) {
case "button":
return 0x1473a263
case "dotMatrix":
return 0x110d154b
case "soundLevel":
return 0x14ad1a5d
case "temperature":
return 0x1421bac7
case "soundPlayer":
return 0x1403d338
case "buzzer":
return 0x1b57b1d7
case "accelerometer":
return 0x1f140409
case "radio":
return 0x1ac986cf
// Kit-A
case "potentiometer":
return 0x1f274746
case "lightLevel":
return 0x17dc9a1c
case "magneticFieldLevel":
return 0x12fe180f
case "rotaryEncoder":
return 0x10fa29c9
case "led":
return 0x1609d4f0
// Others
case "servo":
return 0x12fc9103
default:
return undefined
}
// m:b
export enum ServiceClass {
Button = 0x1473a263,
DotMatrix = 0x110d154b,
SoundLevel = 0x14ad1a5d,
Temperature = 0x1421bac7,
SoundPlayer = 0x1403d338,
Buzzer = 0x1b57b1d7,
Accelerometer = 0x1f140409,
Radio = 0x1ac986cf,
Potentiometer = 0x1f274746,
LightLevel = 0x17dc9a1c,
MagneticFieldLevel = 0x12fe180f,
RotaryEncoder = 0x10fa29c9,
Led = 0x1609d4f0,
Servo = 0x12fc9103,
}

export function stop() {
Expand Down
57 changes: 30 additions & 27 deletions tiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace microcode {
TID_SENSOR_RELEASE = 12,
TID_SENSOR_ACCELEROMETER = 13,
TID_SENSOR_TIMER = 14,
TID_SENSOR_LIGHT = 15, // this is jacdac only
TID_SENSOR_LIGHT = 15, // this is jacdac only
TID_SENSOR_TEMP = 16,
TID_SENSOR_RADIO_RECEIVE = 17,
TID_SENSOR_MICROPHONE = 18,
Expand All @@ -164,7 +164,7 @@ namespace microcode {
TID_SENSOR_ROTARY = 24,
TID_SENSOR_CAR_WALL = 25,
TID_SENSOR_LINE = 26,
TID_SENSOR_LED_LIGHT = 27, // this built-in light sensor on microbit
TID_SENSOR_LED_LIGHT = 27, // this built-in light sensor on microbit
SENSOR_END = 27,

ACTUATOR_START = 40,
Expand Down Expand Up @@ -719,8 +719,11 @@ namespace microcode {
const count = jdc.numServiceInstances(ext)
// special case for buttons, which already exist on micro:bit (6 of them)
// we also have light sensor on board micro:bit (1 of them), as well as in Kit A
return ext == jacs.serviceClass("button") ? count > 6 :
ext == jacs.serviceClass("lightLevel") ? count > 1 : count > 0
return ext == jacs.ServiceClass.Button
? count > 6
: ext == jacs.ServiceClass.LightLevel
? count > 1
: count > 0
}
return true
}
Expand Down Expand Up @@ -953,7 +956,7 @@ namespace microcode {
case Tid.TID_FILTER_TEMP_WARMER:
case Tid.TID_FILTER_TEMP_COLDER:
return "temperature_event"
case Tid.TID_FILTER_LOUD: // dead
case Tid.TID_FILTER_LOUD:
case Tid.TID_FILTER_QUIET: // dead
return "sound_event"
case Tid.TID_MODIFIER_LOOP:
Expand Down Expand Up @@ -1310,63 +1313,63 @@ namespace microcode {
switch (tid) {
case Tid.TID_FILTER_KITA_KEY_1:
case Tid.TID_FILTER_KITA_KEY_2:
return jacs.serviceClass("button")
return jacs.ServiceClass.Button
case Tid.TID_SENSOR_SLIDER:
return jacs.serviceClass("potentiometer")
return jacs.ServiceClass.Potentiometer
case Tid.TID_SENSOR_MAGNET:
return jacs.serviceClass("magneticFieldLevel")
return jacs.ServiceClass.MagneticFieldLevel
case Tid.TID_SENSOR_LIGHT:
return jacs.serviceClass("lightLevel")
return jacs.ServiceClass.LightLevel
case Tid.TID_SENSOR_ROTARY:
return jacs.serviceClass("rotaryEncoder")
return jacs.ServiceClass.RotaryEncoder
case Tid.TID_ACTUATOR_RGB_LED:
return jacs.serviceClass("led")
return jacs.ServiceClass.Led
case Tid.TID_ACTUATOR_SERVO_SET_ANGLE:
return jacs.serviceClass("servo")
return jacs.ServiceClass.Servo
default:
return undefined
}
}

export function serviceClassName(tile: Tile) {
export function serviceClassName(tile: Tile): jacs.ServiceClass {
const tid = getTid(tile)
switch (tid) {
case Tid.TID_SENSOR_PRESS:
case Tid.TID_SENSOR_RELEASE:
return "button"
return jacs.ServiceClass.Button
case Tid.TID_SENSOR_TEMP:
return "temperature"
return jacs.ServiceClass.Temperature
case Tid.TID_SENSOR_RADIO_RECEIVE:
case Tid.TID_ACTUATOR_RADIO_SEND:
case Tid.TID_ACTUATOR_RADIO_SET_GROUP:
case Tid.TID_SENSOR_LINE:
case Tid.TID_SENSOR_CAR_WALL:
case Tid.TID_ACTUATOR_CAR:
return "radio"
return jacs.ServiceClass.Radio
case Tid.TID_SENSOR_SLIDER:
return "potentiometer"
return jacs.ServiceClass.Potentiometer
case Tid.TID_SENSOR_MAGNET:
return "magneticFieldLevel"
return jacs.ServiceClass.MagneticFieldLevel
case Tid.TID_SENSOR_LIGHT:
case Tid.TID_SENSOR_LED_LIGHT:
return "lightLevel"
return jacs.ServiceClass.LightLevel
case Tid.TID_SENSOR_ROTARY:
return "rotaryEncoder"
return jacs.ServiceClass.RotaryEncoder
case Tid.TID_SENSOR_ACCELEROMETER:
return "accelerometer"
return jacs.ServiceClass.Accelerometer
case Tid.TID_SENSOR_MICROPHONE:
return "soundLevel"
return jacs.ServiceClass.SoundLevel
case Tid.TID_ACTUATOR_PAINT:
case Tid.TID_ACTUATOR_SHOW_NUMBER:
return "dotMatrix"
return jacs.ServiceClass.DotMatrix
case Tid.TID_ACTUATOR_SPEAKER:
return "soundPlayer"
return jacs.ServiceClass.SoundPlayer
case Tid.TID_ACTUATOR_MUSIC:
return "buzzer"
return jacs.ServiceClass.Buzzer
case Tid.TID_ACTUATOR_RGB_LED:
return "led"
return jacs.ServiceClass.Led
case Tid.TID_ACTUATOR_SERVO_SET_ANGLE:
return "servo"
return jacs.ServiceClass.Servo
default:
return undefined
}
Expand Down

0 comments on commit 2bd24b0

Please sign in to comment.