Skip to content

Commit bddf3f2

Browse files
committed
analogConfigure and analogRead
1 parent e3430a0 commit bddf3f2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,58 @@ export class AxiDraw {
267267
await wait(timeToTarget);
268268
});
269269
}
270+
271+
/**
272+
* Configure an analog input channel.
273+
* | Channel | Pin | Comments |
274+
* | :------ | :- | :--------------------------------------- |
275+
* | 0 | RA0 | Connected to current adjustment trim-pot |
276+
* | 1 | RA1 | |
277+
* | 2 | RA2 | |
278+
* | 3 | RA3 | (don't use) Pen lift servo power enable |
279+
* | 4 | RA5 | |
280+
* | 5 | RE0 | (don't use) Motor enable 1 |
281+
* | 6 | RE1 | (don't use) MS2 |
282+
* | 7 | RE2 | (don't use) MS1 |
283+
* | 8 | RB2 | Servo connector JP3 |
284+
* | 9 | RB3 | Servo connector JP4 |
285+
* | 10 | RB1 | Pen lift servo connector |
286+
* | 11 | RC2 | |
287+
* | 12 | RB0 | Servo connector JP2 |
288+
* @see analogRead
289+
* @param {number} channel - The analog channel number (0-12).
290+
* @param {boolean} enabled - Whether the channel should be enabled.
291+
* @returns {Promise<void>} - Resolves when the channel is configured.
292+
*/
293+
async analogConfigure(channel, enabled) {
294+
// The EiBotBoard analog configure command allows for 16 channels, but only
295+
// the lower 13 correspond to physical pins.
296+
if (channel < 0 || channel > 12) {
297+
throw new Error('Invalid channel number');
298+
}
299+
300+
await this.#command(async () => {
301+
await this.ebb.analogConfigure(channel, enabled);
302+
});
303+
}
304+
305+
/**
306+
* Read an analog input channel. The value is normalized to a range of 0-1,
307+
* where 1 represents 3.3V. The channel must be enabled with `analogConfigure`
308+
* first. See `analogConfigure` for a list of channels and their pins.
309+
* @see analogConfigure
310+
* @param {number} channel - The analog channel number (0-12).
311+
* @returns {Promise<number>} - Resolves with the normalized value (0-1).
312+
*/
313+
analogRead(channel) {
314+
return this.#command(async () => {
315+
const analogValues = await this.ebb.analogValueGet(channel);
316+
317+
if (!(channel in analogValues)) {
318+
throw new Error(`Channel not enabled. Enable it with analogConfigure(${channel}, true) first.`);
319+
}
320+
321+
return analogValues[channel] / 1023; // Return normalized value
322+
});
323+
}
270324
}

0 commit comments

Comments
 (0)