WIP: Reading from serial port
This commit is contained in:
parent
917c881da8
commit
f1f3260373
4 changed files with 59 additions and 5 deletions
|
|
@ -5,11 +5,13 @@ pub fn build(b: *std.Build) void {
|
|||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const win32_dep = b.dependency("zigwin32", .{});
|
||||
const serial_dep = b.dependency("serial", .{});
|
||||
const mod = b.addModule("win_telegraph", .{
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.target = target,
|
||||
.imports = &.{
|
||||
.{ .name = "win32", .module = win32_dep.module("win32") },
|
||||
.{ .name = "serial", .module = serial_dep.module("serial") },
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,10 @@
|
|||
.url = "git+https://github.com/marlersoft/zigwin32.git#3be22a8d33d330662b1fbb5b68c9411bf0293e27",
|
||||
.hash = "zigwin32-25.0.28-preview-AAAAAB6N5AP3iLHsHKzg7r_aiDyYWo92qLOAG2eBLWhE",
|
||||
},
|
||||
.serial = .{
|
||||
.url = "git+https://github.com/ZigEmbeddedGroup/serial.git#fbd7389ff8bbc9fa362aa74081588755b5d028a0",
|
||||
.hash = "serial-0.0.1-PoeRzF60AAAN8Iu0yXTIX-t3DVzsnmN7vWHKM2HA2Zbq",
|
||||
},
|
||||
},
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
|
|
|
|||
44
src/main.zig
44
src/main.zig
|
|
@ -1,10 +1,44 @@
|
|||
const std = @import("std");
|
||||
const win_telegraph = @import("win_telegraph");
|
||||
|
||||
pub fn main() !void {
|
||||
std.debug.print("Will press space in 3 seconds\n", .{});
|
||||
std.Thread.sleep(3 * std.time.ns_per_s);
|
||||
const dash_key = win_telegraph.Key.SPACE;
|
||||
const dot_key = win_telegraph.Key.CONTROL;
|
||||
|
||||
win_telegraph.setKey(.SPACE, true);
|
||||
win_telegraph.setKey(.SPACE, false);
|
||||
pub fn main() !void {
|
||||
var serial = try win_telegraph.getSerial();
|
||||
defer serial.close();
|
||||
|
||||
var reader_buf: [128]u8 = undefined;
|
||||
var reader = serial.reader(&reader_buf);
|
||||
|
||||
var writer = serial.writer(&.{});
|
||||
try writer.interface.writeAll("Hello\n");
|
||||
|
||||
defer {
|
||||
// Clean up key state
|
||||
win_telegraph.setKey(dash_key, false);
|
||||
win_telegraph.setKey(dot_key, false);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
std.debug.print("Reading...\n", .{});
|
||||
const byte = try reader.interface.takeByte(); // FIXME: Why does it block here?
|
||||
|
||||
std.debug.print("Byte {d}\n", .{byte});
|
||||
switch (byte) {
|
||||
'0' => {
|
||||
win_telegraph.setKey(dash_key, false);
|
||||
win_telegraph.setKey(dot_key, false);
|
||||
},
|
||||
'1' => {
|
||||
win_telegraph.setKey(dash_key, false);
|
||||
win_telegraph.setKey(dot_key, true);
|
||||
},
|
||||
'2' => {
|
||||
win_telegraph.setKey(dash_key, true);
|
||||
win_telegraph.setKey(dot_key, false);
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
src/root.zig
14
src/root.zig
|
|
@ -1,5 +1,6 @@
|
|||
const std = @import("std");
|
||||
const win32 = @import("win32").everything;
|
||||
const zig_serial = @import("serial");
|
||||
|
||||
pub const Key = win32.VIRTUAL_KEY;
|
||||
|
||||
|
|
@ -18,3 +19,16 @@ pub fn setKey(key: Key, pressed: bool) void {
|
|||
};
|
||||
_ = win32.SendInput(1, @ptrCast(&ip), @sizeOf(win32.INPUT));
|
||||
}
|
||||
|
||||
pub fn getSerial() !std.fs.File {
|
||||
const port_name = "\\\\.\\COM9";
|
||||
const serial = try std.fs.cwd().openFile(port_name, .{ .mode = .read_write });
|
||||
try zig_serial.configureSerialPort(serial, .{
|
||||
.baud_rate = 115200,
|
||||
.handshake = .none,
|
||||
.parity = .none,
|
||||
.stop_bits = .one,
|
||||
.word_size = .eight,
|
||||
});
|
||||
return serial;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue