forked from ZigEmbeddedGroup/microzig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.zig
More file actions
254 lines (208 loc) · 8.2 KB
/
framework.zig
File metadata and controls
254 lines (208 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//!
//! The driver framework provides device-independent drivers for peripherials supported by MicroZig.
//!
const std = @import("std");
pub const display = struct {
pub const sh1106 = @import("display/sh1106.zig");
pub const ssd1306 = @import("display/ssd1306.zig");
pub const st77xx = @import("display/st77xx.zig");
pub const hd44780 = @import("display/hd44780.zig");
pub const sharp_memory_lcd = @import("display/sharp_memory_lcd.zig");
// Export generic drivers:
pub const SH1106 = sh1106.SH1106;
pub const SSD1306_I2C = ssd1306.SSD1306_I2C;
pub const ST7735 = st77xx.ST7735;
pub const ST7789 = st77xx.ST7789;
pub const HD44780 = hd44780.HD44780;
pub const SharpMemory_LCD = sharp_memory_lcd.SharpMemory_LCD;
// Export color types:
pub const colors = @import("display/colors.zig");
};
pub const input = struct {
pub const rotary_encoder = @import("input/rotary-encoder.zig");
pub const keyboard_matrix = @import("input/keyboard-matrix.zig");
pub const debounced_button = @import("input/debounced-button.zig");
// Export generic drivers:
pub const Key = keyboard_matrix.Key;
pub const Keyboard_Matrix = keyboard_matrix.Keyboard_Matrix;
pub const Debounced_Button = debounced_button.Debounced_Button;
pub const Rotary_Encoder = rotary_encoder.Rotary_Encoder;
pub const touch = struct {
// const xpt2046 = @import("input/touch/xpt2046.zig");
};
};
pub const led = struct {
pub const ws2812 = @import("led/ws2812.zig");
pub const WS2812 = ws2812.WS2812;
};
pub const sensor = struct {
pub const AS5600 = @import("sensor/AS5600.zig").AS5600;
pub const HTS221 = @import("sensor/HTS221.zig").HTS221;
pub const DS18B20 = @import("sensor/DS18B20.zig").DS18B20;
pub const ICM_20948 = @import("sensor/ICM-20948.zig").ICM_20948;
pub const MLX90640 = @import("sensor/MLX90640.zig").MLX90640;
pub const MPU_6050 = @import("sensor/MPU-6050.zig").MPU_6050;
pub const TLV493D = @import("sensor/TLV493D.zig").TLV493D;
pub const TMP117 = @import("sensor/TMP117.zig").TMP117;
pub const AHT30 = @import("sensor/AHT30.zig").AHT30;
};
pub const stepper = struct {
const s = @import("stepper/stepper.zig");
pub const A4988 = s.Stepper(s.A4988);
pub const DRV8825 = s.Stepper(s.DRV8825);
pub const ULN2003 = @import("stepper/ULN2003.zig").ULN2003;
};
pub const IO_expander = struct {
pub const pcf8574 = @import("io_expander/pcf8574.zig");
pub const PCF8574 = pcf8574.PCF8574;
pub const pca9685 = @import("io_expander/pca9685.zig");
pub const PCA9685 = pca9685.PCA9685;
};
pub const wireless = struct {
pub const cyw43 = @import("wireless/cyw43/cyw43.zig");
pub const cyw43_bus = @import("wireless/cyw43/bus.zig");
pub const cyw43_runner = @import("wireless/cyw43/runner.zig");
pub const Cyw43_Spi = cyw43_bus.Cyw43_Spi;
pub const Cyw43_Bus = cyw43_bus.Cyw43_Bus;
pub const Cyw43_Runner = cyw43_runner.Cyw43_Runner;
// pub const sx1278 = @import("wireless/sx1278.zig");
pub const Cyw43439 = @import("wireless/cyw43439.zig");
};
pub const time = struct {
///
/// An absolute point in time since the startup of the device.
///
/// NOTE: Using an enum to make it a distinct type, the underlying number is
/// time since boot in microseconds.
///
pub const Absolute = enum(u64) {
_,
pub fn from_us(us: u64) Absolute {
return @as(Absolute, @enumFromInt(us));
}
pub fn to_us(abs: Absolute) u64 {
return @intFromEnum(abs);
}
pub fn is_reached_by(deadline: Absolute, point: Absolute) bool {
return deadline.to_us() <= point.to_us();
}
pub fn diff(future: Absolute, past: Absolute) Duration {
return Duration.from_us(future.to_us() - past.to_us());
}
pub fn add_duration(abs: Absolute, dur: Duration) Absolute {
return Absolute.from_us(abs.to_us() + dur.to_us());
}
};
///
/// A duration with microsecond precision.
///
/// NOTE: Using an `enum` type here prevents type confusion with other
/// related or unrelated integer-like types.
///
pub const Duration = enum(u64) {
_,
pub fn from_us(us: u64) Duration {
return @as(Duration, @enumFromInt(us));
}
pub fn from_ms(ms: u64) Duration {
return from_us(1000 * ms);
}
pub fn to_us(duration: Duration) u64 {
return @intFromEnum(duration);
}
pub fn less_than(self: Duration, other: Duration) bool {
return self.to_us() < other.to_us();
}
pub fn minus(self: Duration, other: Duration) Duration {
return from_us(self.to_us() - other.to_us());
}
pub fn plus(self: Duration, other: Duration) Duration {
return from_us(self.to_us() + other.to_us());
}
};
///
/// The deadline construct is a construct to create optional timeouts.
///
/// NOTE: Deadlines use maximum possible `Absolute` time for
/// marking the deadline as "unreachable", as this would mean the device
/// would ever reach an uptime of over 500.000 years.
///
pub const Deadline = struct {
pub const no_deadline: Deadline = .init_absolute(null);
const disabled_sentinel = Absolute.from_us(std.math.maxInt(u64));
timeout: Absolute,
/// Create a new deadline with an absolute end point.
///
/// NOTE: `abs` must not point to the absolute maximum time stamp, as this is
/// used as a sentinel for "unset" deadlines.
pub fn init_absolute(abs: ?Absolute) Deadline {
if (abs) |a|
std.debug.assert(a != disabled_sentinel);
return .{ .timeout = abs orelse disabled_sentinel };
}
/// Create a new deadline with a certain duration from provided time.
pub fn init_relative(since: Absolute, dur: ?Duration) Deadline {
return init_absolute(if (dur) |d|
make_timeout(since, d)
else
null);
}
/// Returns `true` if the deadline can be reached.
pub fn can_be_reached(deadline: Deadline) bool {
return (deadline.timeout != disabled_sentinel);
}
/// Returns `true` if the deadline is reached.
pub fn is_reached_by(deadline: Deadline, now: Absolute) bool {
return deadline.can_be_reached() and deadline.timeout.is_reached_by(now);
}
/// Checks if the deadline is reached and returns an error if so,
pub fn check(deadline: Deadline, now: Absolute) error{Timeout}!void {
if (deadline.is_reached_by(now))
return error.Timeout;
}
};
pub fn make_timeout(since: Absolute, timeout: Duration) Absolute {
return @as(Absolute, @enumFromInt(since.to_us() + timeout.to_us()));
}
pub fn make_timeout_us(since: Absolute, timeout_us: u64) Absolute {
return @as(Absolute, @enumFromInt(since.to_us() + timeout_us));
}
};
pub const DateTime = @import("base/DateTime.zig");
pub const base = struct {
pub const Datagram_Device = @import("base/Datagram_Device.zig");
pub const Stream_Device = @import("base/Stream_Device.zig");
pub const Digital_IO = @import("base/Digital_IO.zig");
pub const Clock_Device = @import("base/Clock_Device.zig");
pub const Block_Memory = @import("base/Block_Memory.zig");
pub const I2C_Device = @import("base/I2C_Device.zig");
};
test {
_ = display.sh1106;
_ = display.ssd1306;
_ = display.st77xx;
_ = display.HD44780;
_ = display.sharp_memory_lcd;
_ = input.keyboard_matrix;
_ = input.debounced_button;
_ = input.rotary_encoder;
_ = sensor.ICM_20948;
_ = sensor.MLX90640;
_ = sensor.MPU_6050;
_ = sensor.TLV493D;
_ = sensor.TMP117;
_ = sensor.AHT30;
_ = @import("stepper/common.zig");
_ = stepper.A4988;
_ = stepper.DRV8825;
_ = stepper.ULN2003;
_ = IO_expander.pcf8574;
_ = time;
_ = DateTime;
_ = base.Datagram_Device;
_ = base.Stream_Device;
_ = base.Digital_IO;
_ = base.Block_Memory;
_ = base.Clock_Device;
_ = base.I2C_Device;
}