mcp3008.j
use spi;
export func request(ch as int) {
if ($ch < 0 or $ch > 7) {
throw Error{kind: "mcp3008", message: "channel 0..7, got {$ch}"};
}
def tx as bytes;
$tx[] = 0x01;
$tx[] = (0x08 | $ch) << 4;
$tx[] = 0x00;
return $tx;
}
export func value(rx as bytes) {
return (($rx[1] & 0x03) << 8) | $rx[2];
}
export func read(dev as spi.Device, ch as int) {
return value(spi.transfer($dev, request($ch)));
}