forked from asynclabs/WiShield
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaple-util.c
165 lines (145 loc) · 3.64 KB
/
maple-util.c
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
/*
* maple-util.c
*
* various utility routines useful for debugging
*
*/
#include <string.h>
#include "libmaple_types.h"
#include "gpio.h"
#include "timers.h"
#include "usb.h"
#include "wirish.h"
#include "maple-util.h"
#define USB_TIMEOUT 50
const char *hex = "0123456789ABCDEF";
void serialUsbInit() {
setupUSB();
}
void serialUsbDisable(void) {
disableUSB();
}
void serialUsbWrite(uint8 ch) {
if(!(usbIsConnected() && usbIsConfigured())) {
return;
}
uint16 status = 0;
uint32 start = millis();
while(status == 0 && (millis() - start <= USB_TIMEOUT)) {
status = usbSendBytes(&ch, 1);
}
}
void serialUsbWriteStr(const char *str) {
if(!(usbIsConnected() && usbIsConfigured())) {
return;
}
uint32 len = strlen(str);
uint16 status = 0;
uint16 oldstatus = 0;
uint32 start = millis();
while(status < len && (millis() - start < USB_TIMEOUT)) {
status += usbSendBytes((uint8*)str+status, len-status);
if(oldstatus != status)
start = millis();
oldstatus = status;
}
}
void serialUsbWriteBuf(void *buf, uint32 size) {
if(!(usbIsConnected() && usbIsConfigured())) {
return;
}
if (!buf) {
return;
}
uint16 status = 0;
uint16 oldstatus = 0;
uint32 start = millis();
while(status < size && (millis() - start < USB_TIMEOUT)) {
status += usbSendBytes((uint8*)buf+status, size-status);
if(oldstatus != status)
start = millis();
oldstatus = status;
}
}
uint32 serialUsbReadBuf(void *buf, uint32 len) {
if (!buf) {
return 0;
}
return usbReceiveBytes((uint8*)buf, len);
}
uint8 serialUsbRead(void) {
uint8 ch;
usbReceiveBytes(&ch, 1);
return ch;
}
uint8 serialUsbIsConnected(void) {
return (usbIsConnected() && usbIsConfigured());
}
void serialUsbPrintNewline(void) {
serialUsbWrite('\r');
serialUsbWrite('\n');
}
void serialUsbPrintln(const char c[]) {
serialUsbWriteStr(c);
serialUsbPrintNewline();
}
void serialUsbPrintlnChar(char c) {
serialUsbWrite(c);
serialUsbPrintNewline();
}
char input;
int toggle = 0;
void serialUsbInput() {
serialUsbPrintln("(waiting for input...)");
toggle = 0;
while(!usbBytesAvailable()) {
toggle ^= 1;
digitalWrite(LED_PIN, toggle);
delay(100);
}
input = serialUsbRead();
serialUsbPrintlnChar(input);
toggle = 0;
digitalWrite(LED_PIN, toggle);
}
void serialUsbPrintlnWaitForInput(const char c[]) {
serialUsbPrintln(c);
serialUsbInput();
}
void blinky() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(LED_PIN, LOW);
delay(500);
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
void blinkyConn() {
pinMode(LED_CONN_PIN, OUTPUT);
digitalWrite(LED_CONN_PIN, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(LED_CONN_PIN, LOW);
delay(500);
digitalWrite(LED_CONN_PIN, HIGH);
delay(500);
digitalWrite(LED_CONN_PIN, LOW);
delay(500);
digitalWrite(LED_CONN_PIN, HIGH);
delay(500);
digitalWrite(LED_CONN_PIN, LOW);
delay(500);
}
void serialUsbPrintHex(uint8 byte) {
char upper, lower;
upper = byte >> 4;
lower = (byte &= 0x0F);
serialUsbWrite(hex[upper]);
serialUsbWrite(hex[lower]);
}