-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.ts
105 lines (81 loc) · 2.37 KB
/
i2c.ts
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
import { Console } from "as-wasi";
import { Handle, Bytes, WasiMutPtr } from "./index"
/// I2C Device Object
export class I2c {
private handle: Handle;
constructor(handle: Handle) {
this.handle = handle
}
/// Initialise a new I2C instance with the provided configuration
public static init(dev: u32, baud: u32, sda: i32, scl: i32): I2c {
// Allocate storage for returned handle
let handle = changetype<i32>(__alloc(4));
Console.log(`as.I2C.init initialising device: ${dev} with baud: ${baud} sda: ${sda} scl: ${scl}`);
let err = i2c_init(dev, baud, sda, scl, handle);
if(err < 0) {
throw new Error(`as.I2C.init error: ${err}`)
}
// Load returned value
let h = load<i32>(handle);
// Free allocated memory
__free(handle);
return new I2c(h);
}
/// Write the provided data to the specified peripheral address
public write(addr: u8, data: Uint8Array): void {
Console.log(`as.I2C.write addr: ${addr} data: ${data}`);
let err = i2c_write(this.handle, addr, new Bytes(data));
if(err < 0) {
throw new Error(`as.I2C.write error: ${err}`)
}
}
/// Read from the specified peripheral address
public read(addr: u8, data: Uint8Array): void {
Console.log(`as.I2C.read addr: ${addr} data: ${data}`);
let d = new Bytes(data);
let err = i2c_read(this.handle, addr, d);
if(err < 0) {
throw new Error(`as.I2C.read error: ${err}`)
}
}
/// Write the provided data then read into the buffer using the
/// specified peripheral addresss
public write_read(addr: u8, data: Uint8Array, buff: Uint8Array): void {
Console.log(`as.I2C.write_read addr: ${addr} data: ${data}`);
let d = new Bytes(data);
let b = new Bytes(buff);
let err = i2c_write_read(this.handle, addr, d, b);
if(err < 0) {
throw new Error(`as.I2C.write_read error: ${err}`)
}
}
}
// External function definitions
//Initialise an I2C device
@external("i2c", "init")
declare function i2c_init(
device: u32,
baud: u32,
sda: i32,
scl: i32,
handle: i32,
): i32;
@external("i2c", "write")
declare function i2c_write(
handle: u32,
addr: u8,
data: Bytes,
): i32;
@external("i2c", "read")
declare function i2c_read(
handle: u32,
addr: u8,
buff: Bytes,
): i32;
@external("i2c", "write_read")
declare function i2c_write_read(
handle: u32,
addr: u8,
data: Bytes,
buff: Bytes,
): i32;