-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathimposcope.device.nut
89 lines (75 loc) · 2.64 KB
/
imposcope.device.nut
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
imp.enableblinkup(false);
// BlinkUp Tester (fake oscilloscope)
// Will display a graph or something
// Constants & Globals
const SAMPLE_RATE = 960; // Sample rate in hertz
const DURATION = 20; // Sampling duration in seconds
blinkupData <- {length = 0, buffer = blob()}; // Data to be sent
sampling <- 0; // Sampling flag
// lastButtonPress <- 0; // Debounce counter
config <- { sampleRate = SAMPLE_RATE }
// Alias for pin connected to collector of phototransistor
// Needs to be pulled down externally with around 47k (maybe less?)
pt_in <- hardware.pin1 // Phototransistor input
pt_s <- hardware.pin2 // Phototransistor supply - write hi to use
// btn <- hardware.pin8 // Button - will pull pin low when pressed
led <- hardware.pin9; // Indicator LED - active low
function processBuffer(buffer, length) {
if (length > 0) {
blinkupData.length = length;
blinkupData.buffer = buffer;
agent.send("data", blinkupData); // Upload data to agent
server.log("Sending buffer of length " + length);
}
else {
server.error("Buffer overrun!");
}
}
// function checkButton() {
// // server.log("Button state change");
// if (!(btn.read()) && hardware.millis() - lastButtonPress > 500) {
// lastButtonPress = hardware.millis();
// if (sampling == 0) {
// startRead();
// }
// else {
// stopRead();
// }
// }
// }
function startRead() {
sampling = 1;
blinkupData.length = 0; // Clear old blinkup data
pt_s.write(1); // Enable phototransistor supply
server.log("Starting sampler!");
led.write(1); // Turn off status LED
hardware.sampler.start(); // Start sampler
agent.send("state", config); // Tell agent we're starting
imp.wakeup(DURATION, stopRead);
}
function stopRead() {
if (sampling) {
hardware.sampler.stop(); // Stop sampler
led.write(0); // Turn on LED
server.log("Stopping sampler.");
pt_s.write(0); // Disable phototransistor supply
agent.send("state", 0); // Tell agent we're done
sampling = 0;
}
}
buffer1 <- blob(4000);
buffer2 <- blob(4000);
buffer3 <- blob(4000);
hardware.sampler.configure(pt_in, SAMPLE_RATE, [buffer1, buffer2, buffer3], processBuffer);
pt_s.configure(DIGITAL_OUT);
pt_s.write(0);
// btn.configure(DIGITAL_IN_PULLUP, checkButton);
led.configure(DIGITAL_OUT_OD);
led.write(0);
function scopeStateChange(newState) {
if (newState == 1)
startRead();
else if (newState == 0)
stopRead();
}
agent.on("scopeRunning", scopeStateChange);