Analog & PWM
Three classes for varying signals: ADC (analog in), DAC (analog out),
and PWM (pulse-width out). Construction is the channel setup, and the units are raw
counts, millivolts, and nanoseconds.
ADC
Analog input channel. Construction sets up the channel: gain and reference are constructor options, defaulting to the pair your chip's driver validates against when omitted.
Example first
import { ADC, A1 } from '@typecad/hal';
const sense = new ADC(A1, {
gain: ADC.GAIN_1_3,
reference: ADC.REF_INTERNAL,
});
const counts = sense.read(); // raw counts at the chip's resolution
const mv = sense.readMillivolts(); // converted to millivoltsGain and reference are set at construction with ADC. tokens such as ADC.GAIN_1_4 — a misspelling is an editor error, and an invalid combination is a
build error naming the valid tokens for your chip.
read read(): number → raw counts
Raw counts at the chip's resolution.
readMillivolts readMillivolts(): number → mV
The same channel, converted to millivolts.
DAC
Analog output channel. Construction carries the resolution in bits (omitted = your chip's channel resolution).
Example first
import { DAC, DAC0 } from '@typecad/hal';
const out = new DAC(DAC0);
out.write(128); // mid-scale on an 8-bit channelwrite write(value: number): void
| Parameter | Type | Units / values | Description |
|---|---|---|---|
| value | number | raw code | 0-255 for an 8-bit channel, 0-4095 for 12-bit — the raw code at the channel's resolution. |
Writes the raw code. There is no 0–1 normalized form.
PWM
Pulse-width output. The period is a required construction fact, in nanoseconds — the channel's time base exists before any pulse does.
Example first
import { PWM } from '@typecad/hal';
const servo = new PWM(6, { periodNs: 20_000_000 }); // 50 Hz
servo.setPulse(1_500_000); // 1.5 ms — center
const dimmer = new PWM(7, { periodNs: 1_000_000 }); // 1 kHz LED dimming
dimmer.setDuty(0.5); // fraction 0.0-1.0setPulse setPulse(ns: number): void
| Parameter | Type | Units / values | Description |
|---|---|---|---|
| ns | number | nanoseconds | The pulse width. |
setDuty setDuty(fraction: number): void
| Parameter | Type | Units / values | Description |
|---|---|---|---|
| fraction | number | 0.0-1.0 | The pulse as a fraction of the constructed period. |
setPeriod setPeriod(ns: number): void
Changes the period; the pulse resets to idle, so follow it with setPulse/setDuty.
All methods
| Member | Description |
|---|---|
| new PWM(pin, { periodNs }) | Construction — the period is required (ns) |
| setPulse(ns) | Set the pulse width |
| setDuty(f) | Set the pulse as a fraction of the period |
| setPeriod(ns) | Changes the period; pulse resets to idle — follow with setPulse/setDuty |
API reference
| Member | Description |
|---|---|
| new ADC(pin, opts?) | gain / reference — ADC.GAIN_* / ADC.REF_* tokens |
| read() / readMillivolts() | Raw counts / millivolts |
| new DAC(pin, opts?) | resolution in bits; omitted = chip default |
| write(value) | Raw code at the channel's resolution |
| new PWM(pin, { periodNs }) | Period is required (ns) |
| setPulse(ns) / setDuty(f) / setPeriod(ns) | Pulse control |