2016-04-06 00:59:25 +02:00
|
|
|
/**
|
|
|
|
* Control currents in Pins for analog/digital signals, servos, i2c, ...
|
|
|
|
*/
|
2017-01-20 05:55:31 +01:00
|
|
|
//% color=#A80000 weight=30 icon="\uf140"
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2022-06-30 19:01:19 +02:00
|
|
|
//% groups=['Analog', 'Digital', 'Servos', 'Pulse', 'Pitch', 'i2c', 'spi']
|
|
|
|
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace pins {
|
|
|
|
/**
|
2019-12-02 05:58:26 +01:00
|
|
|
* Map a number from one range to another. That is, a value of ``from low`` would get mapped to ``to low``, a value of ``from high`` to ``to high``, values in-between to values in-between, etc.
|
2016-03-10 23:01:04 +01:00
|
|
|
* @param value value to map in ranges
|
|
|
|
* @param fromLow the lower bound of the value's current range
|
|
|
|
* @param fromHigh the upper bound of the value's current range, eg: 1023
|
|
|
|
* @param toLow the lower bound of the value's target range
|
|
|
|
* @param toHigh the upper bound of the value's target range, eg: 4
|
|
|
|
*/
|
2016-08-17 20:18:15 +02:00
|
|
|
//% help=pins/map weight=23
|
2019-12-02 05:58:26 +01:00
|
|
|
//% blockId=pin_map block="map %value|from low %fromLow|from high %fromHigh|to low %toLow|to high %toHigh"
|
2022-06-30 19:01:19 +02:00
|
|
|
//% deprecated=true
|
2016-03-10 23:01:04 +01:00
|
|
|
export function map(value: number, fromLow: number, fromHigh: number, toLow: number, toHigh: number): number {
|
|
|
|
return ((value - fromLow) * (toHigh - toLow)) / (fromHigh - fromLow) + toLow;
|
|
|
|
}
|
2016-04-05 04:02:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read one number from 7-bit I2C address.
|
|
|
|
*/
|
2019-12-02 05:58:26 +01:00
|
|
|
//% help=pins/i2c-read-number blockGap=8 advanced=true
|
|
|
|
//% blockId=pins_i2c_readnumber block="i2c read number|at address %address|of format %format|repeated %repeat" weight=7
|
2022-06-30 19:01:19 +02:00
|
|
|
//% group="i2c"
|
2019-12-02 05:58:26 +01:00
|
|
|
export function i2cReadNumber(address: number, format: NumberFormat, repeated?: boolean): number {
|
|
|
|
let buf = pins.i2cReadBuffer(address, pins.sizeOf(format), repeated)
|
2016-04-05 04:02:40 +02:00
|
|
|
return buf.getNumber(format, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write one number to a 7-bit I2C address.
|
|
|
|
*/
|
2019-12-02 05:58:26 +01:00
|
|
|
//% help=pins/i2c-write-number blockGap=8 advanced=true
|
|
|
|
//% blockId=i2c_writenumber block="i2c write number|at address %address|with value %value|of format %format|repeated %repeat" weight=6
|
2022-06-30 19:01:19 +02:00
|
|
|
//% group="i2c"
|
2019-12-02 05:58:26 +01:00
|
|
|
export function i2cWriteNumber(address: number, value: number, format: NumberFormat, repeated?: boolean): void {
|
2016-04-05 04:02:40 +02:00
|
|
|
let buf = createBuffer(pins.sizeOf(format))
|
|
|
|
buf.setNumber(format, 0, value)
|
2019-12-02 05:58:26 +01:00
|
|
|
pins.i2cWriteBuffer(address, buf, repeated)
|
2016-04-05 04:02:40 +02:00
|
|
|
}
|
2016-04-05 05:28:08 +02:00
|
|
|
}
|