@pamoja/gpio 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Ergonomic facade over the generated on-board bus binding.
3
+ *
4
+ * Before a node reaches any network it talks to the chips wired to its own board.
5
+ * Three interfaces cover almost everything cheap hardware uses, and each carries
6
+ * one small piece of logic that is a classic field bug when it is wrong: the I2C
7
+ * address byte, the SPI clock mode, and whether a relay is active high or active
8
+ * low.
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+ import { type PinEdge as PinEdgeName, type PinLevel as PinLevelName, type PinPolarity as PinPolarityName, type SpiClock } from '@pamoja/native';
13
+ export { type SpiClock };
14
+ /** The physical voltage level on a pin. */
15
+ export declare const PinLevel: {
16
+ /** A low level, near ground. */
17
+ readonly Low: PinLevelName;
18
+ /** A high level, near the supply voltage. */
19
+ readonly High: PinLevelName;
20
+ };
21
+ /** The physical voltage level on a pin. */
22
+ export type PinLevel = PinLevelName;
23
+ /** The signal transition that triggers a pin interrupt. */
24
+ export declare const PinEdge: {
25
+ /** A low-to-high transition. */
26
+ readonly Rising: PinEdgeName;
27
+ /** A high-to-low transition. */
28
+ readonly Falling: PinEdgeName;
29
+ /** Either transition. */
30
+ readonly Both: PinEdgeName;
31
+ };
32
+ /** The signal transition that triggers a pin interrupt. */
33
+ export type PinEdge = PinEdgeName;
34
+ /** Whether a signal is asserted by a high or a low physical level. */
35
+ export declare const PinPolarity: {
36
+ /** A high level means asserted. */
37
+ readonly ActiveHigh: PinPolarityName;
38
+ /** A low level means asserted, the wiring of most buttons and relay boards. */
39
+ readonly ActiveLow: PinPolarityName;
40
+ };
41
+ /** Whether a signal is asserted by a high or a low physical level. */
42
+ export type PinPolarity = PinPolarityName;
43
+ /** I2C addressing per the NXP I2C-bus specification (UM10204). */
44
+ export declare const i2c: {
45
+ /** The lowest 7-bit address the specification keeps for itself. */
46
+ RESERVED_FROM: number;
47
+ /** The first 7-bit address above the reserved block at the bottom of the range. */
48
+ RESERVED_BELOW: number;
49
+ /**
50
+ * Returns the address bytes a controller puts on the bus for a transfer.
51
+ *
52
+ * @param address - The device address.
53
+ * @param options - `read` selects the direction, `tenBit` the address width.
54
+ * @returns One byte for a 7-bit address, two for a 10-bit one.
55
+ * @throws If the address is outside its width's range.
56
+ */
57
+ addressFrame(address: number, options?: {
58
+ read?: boolean;
59
+ tenBit?: boolean;
60
+ }): Buffer;
61
+ /**
62
+ * Returns how many bytes an address frame occupies.
63
+ *
64
+ * @param address - The device address.
65
+ * @param tenBit - Whether it is a 10-bit address.
66
+ * @returns `1` for a 7-bit address, `2` for a 10-bit one.
67
+ * @throws If the address is outside its width's range.
68
+ */
69
+ frameLen(address: number, tenBit?: boolean): number;
70
+ /**
71
+ * Reports whether an address falls in a range the specification reserves.
72
+ *
73
+ * UM10204 reserves `0x00..=0x07` and `0x78..=0x7F`, leaving `0x08..=0x77` for
74
+ * ordinary devices.
75
+ *
76
+ * @param address - The device address.
77
+ * @param tenBit - Whether it is a 10-bit address, which is never reserved.
78
+ * @returns Whether the address is reserved.
79
+ * @throws If the address is outside its width's range.
80
+ */
81
+ isReserved(address: number, tenBit?: boolean): boolean;
82
+ /**
83
+ * Reports whether an address is the general call address `0x00`.
84
+ *
85
+ * @param address - The device address.
86
+ * @param tenBit - Whether it is a 10-bit address.
87
+ * @returns Whether this is the broadcast every device listens to.
88
+ * @throws If the address is outside its width's range.
89
+ */
90
+ isGeneralCall(address: number, tenBit?: boolean): boolean;
91
+ };
92
+ /** The four SPI clock modes, as the `(CPOL, CPHA)` pair datasheets quote. */
93
+ export declare const spi: {
94
+ /**
95
+ * Returns the clock polarity and phase a mode number names.
96
+ *
97
+ * @param mode - The mode number, 0 to 3.
98
+ * @returns The pair.
99
+ * @throws If the mode number is above 3.
100
+ */
101
+ clockFor(mode: number): SpiClock;
102
+ /**
103
+ * Returns the mode number a clock polarity and phase name.
104
+ *
105
+ * @param cpol - Whether the clock idles high.
106
+ * @param cpha - Whether data is sampled on the trailing edge.
107
+ * @returns The mode number, 0 to 3. Every pair names a mode.
108
+ */
109
+ modeFor(cpol: boolean, cpha: boolean): number;
110
+ };
111
+ /** The GPIO pin model: levels, interrupt edges, and active polarity. */
112
+ export declare const pin: {
113
+ /**
114
+ * Returns the level a boolean names.
115
+ *
116
+ * @param high - `true` for high, `false` for low.
117
+ * @returns The level.
118
+ */
119
+ levelFrom(high: boolean): PinLevel;
120
+ /**
121
+ * Returns the opposite level.
122
+ *
123
+ * @param level - The level to invert.
124
+ * @returns The other level.
125
+ */
126
+ invert(level: PinLevel): PinLevel;
127
+ /**
128
+ * Reports whether a transition fires an interrupt trigger.
129
+ *
130
+ * @param edge - The trigger configured on the pin.
131
+ * @param from - The level before the change.
132
+ * @param to - The level after it.
133
+ * @returns Whether the trigger fires.
134
+ */
135
+ triggers(edge: PinEdge, from: PinLevel, to: PinLevel): boolean;
136
+ /**
137
+ * Returns the physical level that represents a logical state.
138
+ *
139
+ * @param polarity - How the signal is wired.
140
+ * @param asserted - Whether the signal should be asserted.
141
+ * @returns The level to drive, inverted for active-low wiring.
142
+ */
143
+ levelFor(polarity: PinPolarity, asserted: boolean): PinLevel;
144
+ /**
145
+ * Reports whether a physical level means the signal is asserted.
146
+ *
147
+ * @param polarity - How the signal is wired.
148
+ * @param level - The level read on the pin.
149
+ * @returns Whether the signal is asserted.
150
+ */
151
+ isAsserted(polarity: PinPolarity, level: PinLevel): boolean;
152
+ };
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ /**
3
+ * Ergonomic facade over the generated on-board bus binding.
4
+ *
5
+ * Before a node reaches any network it talks to the chips wired to its own board.
6
+ * Three interfaces cover almost everything cheap hardware uses, and each carries
7
+ * one small piece of logic that is a classic field bug when it is wrong: the I2C
8
+ * address byte, the SPI clock mode, and whether a relay is active high or active
9
+ * low.
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.pin = exports.spi = exports.i2c = exports.PinPolarity = exports.PinEdge = exports.PinLevel = void 0;
15
+ const native_1 = require("@pamoja/native");
16
+ /** The physical voltage level on a pin. */
17
+ exports.PinLevel = {
18
+ /** A low level, near ground. */
19
+ Low: 'Low',
20
+ /** A high level, near the supply voltage. */
21
+ High: 'High',
22
+ };
23
+ /** The signal transition that triggers a pin interrupt. */
24
+ exports.PinEdge = {
25
+ /** A low-to-high transition. */
26
+ Rising: 'Rising',
27
+ /** A high-to-low transition. */
28
+ Falling: 'Falling',
29
+ /** Either transition. */
30
+ Both: 'Both',
31
+ };
32
+ /** Whether a signal is asserted by a high or a low physical level. */
33
+ exports.PinPolarity = {
34
+ /** A high level means asserted. */
35
+ ActiveHigh: 'ActiveHigh',
36
+ /** A low level means asserted, the wiring of most buttons and relay boards. */
37
+ ActiveLow: 'ActiveLow',
38
+ };
39
+ /** I2C addressing per the NXP I2C-bus specification (UM10204). */
40
+ exports.i2c = {
41
+ /** The lowest 7-bit address the specification keeps for itself. */
42
+ RESERVED_FROM: native_1.I2C_RESERVED_FROM,
43
+ /** The first 7-bit address above the reserved block at the bottom of the range. */
44
+ RESERVED_BELOW: native_1.I2C_RESERVED_BELOW,
45
+ /**
46
+ * Returns the address bytes a controller puts on the bus for a transfer.
47
+ *
48
+ * @param address - The device address.
49
+ * @param options - `read` selects the direction, `tenBit` the address width.
50
+ * @returns One byte for a 7-bit address, two for a 10-bit one.
51
+ * @throws If the address is outside its width's range.
52
+ */
53
+ addressFrame(address, options = {}) {
54
+ return (0, native_1.i2cAddressFrame)(address, options.tenBit ?? false, options.read ?? false);
55
+ },
56
+ /**
57
+ * Returns how many bytes an address frame occupies.
58
+ *
59
+ * @param address - The device address.
60
+ * @param tenBit - Whether it is a 10-bit address.
61
+ * @returns `1` for a 7-bit address, `2` for a 10-bit one.
62
+ * @throws If the address is outside its width's range.
63
+ */
64
+ frameLen(address, tenBit = false) {
65
+ return (0, native_1.i2cAddressFrameLen)(address, tenBit);
66
+ },
67
+ /**
68
+ * Reports whether an address falls in a range the specification reserves.
69
+ *
70
+ * UM10204 reserves `0x00..=0x07` and `0x78..=0x7F`, leaving `0x08..=0x77` for
71
+ * ordinary devices.
72
+ *
73
+ * @param address - The device address.
74
+ * @param tenBit - Whether it is a 10-bit address, which is never reserved.
75
+ * @returns Whether the address is reserved.
76
+ * @throws If the address is outside its width's range.
77
+ */
78
+ isReserved(address, tenBit = false) {
79
+ return (0, native_1.i2cAddressIsReserved)(address, tenBit);
80
+ },
81
+ /**
82
+ * Reports whether an address is the general call address `0x00`.
83
+ *
84
+ * @param address - The device address.
85
+ * @param tenBit - Whether it is a 10-bit address.
86
+ * @returns Whether this is the broadcast every device listens to.
87
+ * @throws If the address is outside its width's range.
88
+ */
89
+ isGeneralCall(address, tenBit = false) {
90
+ return (0, native_1.i2cAddressIsGeneralCall)(address, tenBit);
91
+ },
92
+ };
93
+ /** The four SPI clock modes, as the `(CPOL, CPHA)` pair datasheets quote. */
94
+ exports.spi = {
95
+ /**
96
+ * Returns the clock polarity and phase a mode number names.
97
+ *
98
+ * @param mode - The mode number, 0 to 3.
99
+ * @returns The pair.
100
+ * @throws If the mode number is above 3.
101
+ */
102
+ clockFor(mode) {
103
+ return (0, native_1.spiModeClock)(mode);
104
+ },
105
+ /**
106
+ * Returns the mode number a clock polarity and phase name.
107
+ *
108
+ * @param cpol - Whether the clock idles high.
109
+ * @param cpha - Whether data is sampled on the trailing edge.
110
+ * @returns The mode number, 0 to 3. Every pair names a mode.
111
+ */
112
+ modeFor(cpol, cpha) {
113
+ return (0, native_1.spiModeFromClock)(cpol, cpha);
114
+ },
115
+ };
116
+ /** The GPIO pin model: levels, interrupt edges, and active polarity. */
117
+ exports.pin = {
118
+ /**
119
+ * Returns the level a boolean names.
120
+ *
121
+ * @param high - `true` for high, `false` for low.
122
+ * @returns The level.
123
+ */
124
+ levelFrom(high) {
125
+ return (0, native_1.pinLevelFromBool)(high);
126
+ },
127
+ /**
128
+ * Returns the opposite level.
129
+ *
130
+ * @param level - The level to invert.
131
+ * @returns The other level.
132
+ */
133
+ invert(level) {
134
+ return (0, native_1.pinLevelInverted)(level);
135
+ },
136
+ /**
137
+ * Reports whether a transition fires an interrupt trigger.
138
+ *
139
+ * @param edge - The trigger configured on the pin.
140
+ * @param from - The level before the change.
141
+ * @param to - The level after it.
142
+ * @returns Whether the trigger fires.
143
+ */
144
+ triggers(edge, from, to) {
145
+ return (0, native_1.pinEdgeTriggeredBy)(edge, from, to);
146
+ },
147
+ /**
148
+ * Returns the physical level that represents a logical state.
149
+ *
150
+ * @param polarity - How the signal is wired.
151
+ * @param asserted - Whether the signal should be asserted.
152
+ * @returns The level to drive, inverted for active-low wiring.
153
+ */
154
+ levelFor(polarity, asserted) {
155
+ return (0, native_1.pinPolarityLevel)(polarity, asserted);
156
+ },
157
+ /**
158
+ * Reports whether a physical level means the signal is asserted.
159
+ *
160
+ * @param polarity - How the signal is wired.
161
+ * @param level - The level read on the pin.
162
+ * @returns Whether the signal is asserted.
163
+ */
164
+ isAsserted(polarity, level) {
165
+ return (0, native_1.pinPolarityIsAsserted)(polarity, level);
166
+ },
167
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pamoja/gpio",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "I2C address frames with reserved-range checks, the four SPI clock modes, and active-high or active-low pins.",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -34,6 +34,6 @@
34
34
  "node": ">= 16"
35
35
  },
36
36
  "dependencies": {
37
- "@pamoja/native": "0.1.15"
37
+ "@pamoja/native": "0.1.16"
38
38
  }
39
39
  }