Skip to content

Commit

Permalink
deprecated Math.clamp in favor of constrain
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jul 24, 2023
1 parent cffa584 commit 0e2ee13
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/buzzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import * as ds from "@devicescript/core"

ds.Buzzer.prototype.playNote = async function (frequency, volume, duration) {
const p = 1000000 / frequency
volume = Math.clamp(0, volume, 1)
volume = Math.constrain(volume, 0, 1)
await this.playTone(p, p * volume * 0.5, duration)
}
8 changes: 0 additions & 8 deletions packages/core/src/devicescript-lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ declare interface Math {
*/
idiv(x: number, y: number): number

/**
* Clamp `v` to between `low` and `high` inclusive.
* @param low lower bound
* @param v value to clamp
* @param hi upper bound
*/
clamp(low: number, v: number, hi: number): number

/**
* Return an integer between 0 and `max` inclusive
* @param max upper bound
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/ledstrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function ledStripEncode(format: string, args: (number | number[])[]) {
outarr.push(0xd8) // tmpmode
outarr.push(3) // mult
outarr.push(0xd0) // setall
const mm = Math.clamp(0, 255, Math.round(128 * f))
const mm = Math.constrain(Math.round(128 * f), 0, 255)
outarr.push(0xc1)
outarr.push(mm)
outarr.push(mm)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/rotaryencoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ds.RotaryEncoder.prototype.asPotentiometer = function (steps?: number) {
if (!steps) steps = await self.clicksPerTurn.read()
let p0 = await self.reading.read()
self.reading.subscribe(v => {
const curr = Math.clamp(0, v - p0, steps)
const curr = Math.constrain(v - p0, 0, steps)
p0 = v - curr
reg.emit(curr / steps)
})
Expand Down
6 changes: 0 additions & 6 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ Math.sign = function sign(v) {
return NaN
}

Math.clamp = function clamp(low, v, hi) {
if (v < low) return low
if (v > hi) return hi
return v
}

Math.sqrt = function sqrt(x) {
return Math.pow(x, 0.5)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/drivers/src/bme680.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class BME680Driver extends I2CSensorDriver<{
await delay(5)
const id = await this.readReg(BME680_REG_CHIPID)
console.debug(`BME680 id=${id}`)
if (id !== BME680_CHIPID) throw new DriverError(`BME680: wrong chip id (${id})`)
if (id !== BME680_CHIPID)
throw new DriverError(`BME680: wrong chip id (${id})`)
this.chipVariant = await this.readReg(BME680_REG_VARIANT)
await this.readCalibration()

Expand Down Expand Up @@ -251,7 +252,7 @@ class BME680Driver extends I2CSensorDriver<{
let var6 = (var4 * var5) / 2
let calc_hum = (((var3 + var6) / 1024) * 1000) / 4096
calc_hum /= 1000 // get back to RH
return Math.clamp(0, calc_hum, 100)
return Math.constrain(calc_hum, 0, 100)
}

private fillTables() {
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/src/ledserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class LedServer extends Server implements ds.LedServerSpec {
return this._intensity
}
set_intensity(value: number): void {
this._intensity = Math.clamp(0, value, 1)
this._intensity = Math.constrain(value, 0, 1)
}
actualBrightness(): number {
return this._intensity
Expand Down
16 changes: 7 additions & 9 deletions packages/server/src/sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ export interface SensorServerOptions extends ServerOptions {
interval?: number
}

export class SensorServer
extends Server
implements ds.SensorServerSpec
{
export class SensorServer extends Server implements ds.SensorServerSpec {
_preferredInterval: number
_streamingInterval: number
_streamingSamples = 0
_interval: number

constructor(
spec: ds.ServiceSpec,
options?: SensorServerOptions
) {
constructor(spec: ds.ServiceSpec, options?: SensorServerOptions) {
super(spec, options)
const interval = options?.interval || 500
this.set_streamingInterval(interval)
Expand Down Expand Up @@ -62,7 +56,11 @@ export class SensorServer
return this._streamingInterval
}
set_streamingInterval(value: number) {
this._streamingInterval = Math.clamp(minInterval, value, maxInterval)
this._streamingInterval = Math.constrain(
value,
minInterval,
maxInterval
)
if (this._interval)
updateInterval(this._interval, this._streamingInterval)
}
Expand Down

0 comments on commit 0e2ee13

Please sign in to comment.