-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
70 lines (57 loc) · 2.06 KB
/
main.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
/**
This is a MakeCode extension for the Grove Keyboard CardKB" of M5Stack.
Further information can be found here: https://docs.m5stack.com/en/unit/cardkb
*/
//% color="#AA278D" weight=200 //% icon="\uf11c"
namespace CardKB {
let i2cDevice = 95;
//% block="read charcode" blockId=readCharcode
export function readCharcode() {
let charcode: number = 0;
charcode = pins.i2cReadNumber(i2cDevice, NumberFormat.Int8LE, true)
return charcode
}
//% block="read string" blockId=readString
export function readString() {
let charcode: number = 0;
charcode = pins.i2cReadNumber(i2cDevice, NumberFormat.Int8LE, true)
if (charcode < 0) {
return assignSpecialCharacter(charcode)
} else {
return String.fromCharCode(charcode)
}
}
function assignSpecialCharacter(negativeCharcode: number) {
let specialCharacters: string[] = ['esc', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 'del', 'tab', '{', '}', '[', ']', '/', '\\', '|', '~', "'", '"', '', '', 'shift', ';', ':', '`', '+', '-', '_', '=', '?', '', 'enter', 'sym', '', '', '', '', '', '', '', '', '<', '>', ' '];
return specialCharacters[128 + negativeCharcode]
}
}
/**
* Functions are mapped to blocks using various macros
* in comments starting with %. The most important macro
* is "block", and it specifies that a block should be
* generated for an **exported** function.
enum characterFormat {
//% block=ASCII
ascii,
//% block=Charcode
charcode
}
//% color="#AA278D" weight=200 //% icon="\uf11c"
namespace CardKB {
let i2cDevice = 95;
//% block
export function readLetter(format: characterFormat) {
let message = 0;
message = pins.i2cReadNumber(i2cDevice, NumberFormat.Int8LE, false)
if (message != 0) {
if (format == characterFormat.ascii) {
return String.fromCharCode(message)
} else if (characterFormat.charcode) {
return message.toString()
}
}
return ""
}
}
*/