-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlego-temp-test2.c
140 lines (123 loc) · 4.07 KB
/
lego-temp-test2.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
#pragma config(Sensor, S1, LEGOTMP, sensorI2CCustom)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/**
* lego-temp.h provides an API for the Lego Temperature Sensor. This program
* demonstrates how to use that API to use the sensor in single-shot mode.
*
* Changelog:
* - 0.1: Initial release
*
* Credits:
* - Big thanks to Sylvain Cacheux for writing the initial drivers.
*
* License: You may use this code as you wish, provided you give credit where it's due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.
* Xander Soldaat (xander_at_botbench.com)
* 16 february 2010
* version 0.1
*/
#include "drivers/lego-temp.h"
// Small function to convert the accuracy level into something humans can read
void accuracyToString(tLEGOTMPAccuracy _accuracy, string &text) {
strcpy(text, "");
switch (_accuracy) {
case A_MIN:
text = "A_MIN";
break;
case A_MEAN1:
text = "A_MEAN1";
break;
case A_MEAN2:
text = "A_MEAN2";
break;
case A_MAX:
text = "A_MAX";
break;
}
}
task main() {
float temp;
tLEGOTMPAccuracy accuracy;
string strAcc;
nxtDisplayCenteredTextLine(0, "LEGO");
nxtDisplayCenteredBigTextLine(1, "Temp");
nxtDisplayCenteredTextLine(3, "Test 1");
nxtDisplayCenteredTextLine(5, "Connect sensor");
nxtDisplayCenteredTextLine(6, "to S1");
wait1Msec(2000);
eraseDisplay();
// Setup the sensor for Single shot mode
LEGOTMPsetSingleShot(LEGOTMP);
//setting minimum accuracy
accuracy = A_MIN;
if (!LEGOTMPsetAccuracy(LEGOTMP, accuracy)) {
nxtDisplayTextLine(0, "Error setAccuracy");
wait1Msec(5000);
StopAllTasks();
}
//reads the current accuracy of the sensor
if (!LEGOTMPreadAccuracy(LEGOTMP, accuracy)) {
nxtDisplayTextLine(0, "Error readAccuracy");
wait1Msec(5000);
StopAllTasks();
}
accuracyToString(accuracy, strAcc);
nxtDisplayTextLine(0, "Accuracy: %s", strAcc);
//loop to read temp
while (true) {
switch(nNxtButtonPressed) {
// If the left button is pressed, decrease the accuracy
case kLeftButton:
switch(accuracy) {
case A_MIN: accuracy = A_MAX; break;
case A_MEAN1: accuracy = A_MIN; break;
case A_MEAN2: accuracy = A_MEAN1; break;
case A_MAX: accuracy = A_MEAN2; break;
}
if (!LEGOTMPsetAccuracy(LEGOTMP, accuracy)) {
nxtDisplayTextLine(0, "Error setAccuracy");
wait1Msec(5000);
StopAllTasks();
}
accuracyToString(accuracy, strAcc);
nxtDisplayTextLine(0, "Accuracy: %s", strAcc);
// debounce the button
while (nNxtButtonPressed != kNoButton) EndTimeSlice();
break;
// If the right button is pressed, increase the accuracy
case kRightButton:
switch(accuracy) {
case A_MIN: accuracy = A_MEAN1; break;
case A_MEAN1: accuracy = A_MEAN2; break;
case A_MEAN2: accuracy = A_MAX; break;
case A_MAX: accuracy = A_MIN; break;
}
if (!LEGOTMPsetAccuracy(LEGOTMP, accuracy)) {
nxtDisplayTextLine(0, "Error setAccuracy");
wait1Msec(5000);
StopAllTasks();
}
accuracyToString(accuracy, strAcc);
nxtDisplayTextLine(0, "Accuracy: %s", strAcc);
// debounce the button
while (nNxtButtonPressed != kNoButton) EndTimeSlice();
break;
}
if (!LEGOTMPreadTemp(LEGOTMP, temp)) {
eraseDisplay();
nxtDisplayTextLine(0, "Temp reading pb");
wait10Msec(100);
StopAllTasks();
}
nxtDisplayCenteredBigTextLine(2, "Temp:");
// Depending on the level of accuracy, you need to change the
// the formatting of the float, makes it look nicer.
switch(accuracy) {
case A_MIN: nxtDisplayCenteredBigTextLine(4, "%4.1f", temp); break;
case A_MEAN1: nxtDisplayCenteredBigTextLine(4, "%5.2f", temp); break;
case A_MEAN2: nxtDisplayCenteredBigTextLine(4, "%6.3f", temp); break;
case A_MAX: nxtDisplayCenteredBigTextLine(4, "%7.4f", temp); break;
}
}
}