Add serial module
This commit is contained in:
1 parent
93a4065dea
commit
e25d28ebc7
3 files changed
+77
-1
No files matched your search
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Serial port access.
|
||||
*/
|
||||
module hulk.serial;
|
||||
|
||||
import hulk.cpu;
|
||||
import core.stdc.stdarg;
|
||||
static import hulk.writef;
|
||||
|
||||
struct Serial
|
||||
{
|
||||
enum BASE_PORT = 0x3F8;
|
||||
|
||||
/**
|
||||
* Initialize the serial module.
|
||||
*/
|
||||
static void initialize()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a character to the serial port.
|
||||
*
|
||||
* @param ch Character to write.
|
||||
*/
|
||||
static void write(ubyte ch)
|
||||
{
|
||||
if (ch == '\n')
|
||||
{
|
||||
out8(BASE_PORT, '\r');
|
||||
}
|
||||
out8(BASE_PORT, ch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a formatted string to the serial port.
|
||||
*
|
||||
* @param s Format string.
|
||||
* @param args Variable arguments structure.
|
||||
*/
|
||||
public static void writef(string s, va_list args)
|
||||
{
|
||||
hulk.writef.writef(s, args, function void(ubyte ch) {
|
||||
Serial.write(ch);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a formatted string to the serial port.
|
||||
*
|
||||
* @param s Format string.
|
||||
*/
|
||||
public static extern (C) void writef(string s, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, s);
|
||||
writef(s, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a formatted string and newline to the serial port.
|
||||
*
|
||||
* @param s Format string.
|
||||
*/
|
||||
public static extern (C) void writefln(string s, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, s);
|
||||
writef(s, args);
|
||||
writef("\n", args);
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user