-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeensyArduino.c
234 lines (192 loc) · 5.87 KB
/
TeensyArduino.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
PSMonitor Sketch by Matthew Painter 16-Jan-2012
This 'sketch' will monitor the serial port and respond to appropriate commands sent to it.
Sketch assumes it is communicating with the associated Powershell PSMonitor 'script'.
Example shows how to react and respond to messages with certain headers. Sketch sends PowerShell
command to the connected system and then interprets the executed results.
Compile using USB type: Serial
*/
#include <Time.h>
// library here: http://arduino.cc/playground/Code/Time
int ledPin = 11;
int ExternalLED = 20; //connect your test LED to pin 20 on Teensy
int ModeZ;
int PSCmd;
char c;
time_t pctime;
boolean TimeisSet;
long SecsStart, SecsEnd, SecsElapsed;
//----------------------------------------------------------------------------
void setup()
{
digitalWrite(ledPin, HIGH); // set the onboard LED on
Serial.begin(9600);
pinMode(ExternalLED, OUTPUT);
delay(2000);
}
//----------------------------------------------------------------------------
void loop()
{
ModeZ = GetMode(); //retrieve Mode 0-15
if (ModeZ == 0)
{
Serial.println(F("*** Teensy functions programmed on this board ***"));
Serial.println(F("0 - This Menu"));
Serial.println(F("1 - PC Speaker Beep"));
Serial.println(F("2 - Get Teensy Time"));
Serial.println(F("3 - Set Teensy Time"));
Serial.println(F("4 - "));
Serial.println(F("5 - Get Computer Serial Number"));
Serial.println(F("6 - Get PSMonitor Run Time"));
Serial.println(F("7 - "));
Serial.println(F("8 - "));
Serial.println(F("9 - Activate Output1 (LED) on Teensy"));
Serial.println(F("10- Deactivate Output1 (LED) on Teensy"));
Serial.println(F("11- "));
Serial.println(F("12- "));
Serial.println(F("13- "));
Serial.println(F("14- "));
Serial.println(F("15- TEST"));
}
if (ModeZ == 1)
{
Serial.println(F("Teensy makes Computer Beep"));
Serial.println(F("PSCmd:[console]::Beep()"));
}
if (ModeZ == 2)
{
Serial.println(F("The time set on the Teensy is:"));
Serial.print(hour()); Serial.print(":");
if(minute() < 10){Serial.print('0');}Serial.print(minute()); Serial.print(":");
if(second() < 10){Serial.print('0');}Serial.println(second());
}
if (ModeZ == 3)
{
Serial.println(F("Teensy Requests Computer Send Timestamp"));
SetTime();
}
if (ModeZ == 4)
{
Serial.println(F("Teensy Requests Username"));
Serial.println(F("PSCmd:[environment]::username"));
}
if (ModeZ == 5)
{
Serial.println(F("Teensy Requests Computer Serial Number"));
Serial.println(F("PSCmd:(gwmi 'Win32_BIOS').SerialNumber"));
}
if (ModeZ == 6)
{
Serial.println(F("Teensy Requests PSMonitor Runtime in Seconds"));
Serial.println(F("PSCmd:[math]::round((New-TimeSpan ((gps -id $pid).starttime) (Get-Date)).TotalSeconds,0)"));
}
if (ModeZ == 7)
{
}
if (ModeZ == 8)
{
}
if (ModeZ == 9)
{
Serial.println(F("LED ON"));
digitalWrite(ExternalLED, HIGH);
}
if (ModeZ == 10)
{
Serial.println(F("LED OFF"));
digitalWrite(ExternalLED, LOW);
}
if (ModeZ == 11)
{
}
if (ModeZ == 12)
{
}
if (ModeZ == 13)
{
}
if (ModeZ == 14)
{
}
if (ModeZ == 15)
{
Serial.println(F("Teensy Running script block 15"));
}
}
//----------------------------------------------------------------------------
int GetMode()
{
//clear read buffer
while (Serial.available()){ Serial.read();}
//attempt to read serial port for 2 seconds
SecsStart = now();
do
{
if (Serial.available())
{
c = Serial.read();
if (c == '@')
{
PSCmd = 0;
for(int i=0; i < 2; i++)
{
c = Serial.read();
if( c >= '0' && c <= '9')
{
PSCmd = (10 * PSCmd) + (c - '0');
}
}
return PSCmd;
}
//clear read buffer
while (Serial.available()){ Serial.read();}
}
//break loop after 2 secs
SecsEnd = now();
SecsElapsed = SecsEnd-SecsStart;
}while(SecsElapsed<2);
return 16;
}
//----------------------------------------------------------------------------
void SetTime()
{
// set time with timestamp transmitted from local PC
while (Serial.available()){ Serial.read();} //clear read buffer
TimeisSet = false;
//send command to PowerShell requesting timestamp be echoed back
Serial.println(F("PSCmd:'T'+[math]::round((New-TimeSpan (get-date('1 Jan 1970')) (get-date)).TotalSeconds,0)"));
delay(250);
//read timestamp from serial port
if(Serial.available() >= 11 )
{
do
{
c = Serial.read();
if( c == 'T' )
{
pctime = 0;
for(int i=0; i < 11 -1; i++)
{
c = Serial.read();
if( c >= '0' && c <= '9')
{
pctime = (10 * pctime) + (c - '0');
}
}
}
} while (Serial.available());
}
setTime(pctime);
if (timeStatus() == timeSet)
{
TimeisSet = true;
}
while (Serial.available()){ Serial.read();} //clear read buffer
if(TimeisSet)
{
Serial.print(hour()); Serial.print(":");
if(minute() < 10){Serial.print('0');}Serial.print(minute()); Serial.print(":");
if(second() < 10){Serial.print('0');}Serial.println(second());
}
}
#>