1 // Main javascript for the BeagleBone Weather Cape demo.
3 // This code handles drawing the thermometer, gauges, and light level dot.
4 // Most of this code is from the original weatherstation demo. There is some
5 // room for improvement (i.e. changing the scale on the gauges requires a lot
6 // of tweaking, not just changing pmax and pmin), but it gets the job done.
29 var hunit
= "% Humidity";
36 var setWidth = function() {
37 var canvasWidth
= window
.innerWidth
* 0.9;
38 if ( canvasWidth
> (0.8 * window
.innerHeight
)) {
39 canvasWidth
= 1.8 * window
.innerHeight
;
44 barometerWidth
= canvasWidth
*0.5;
45 barometerHeight
= barometerWidth
;
46 thermometerWidth
= canvasWidth
*0.25;
47 lightWidth
= canvasWidth
*0.25;
51 var barometerSketchProc = function(p
) {
52 p
.size(barometerWidth
, barometerWidth
);
54 p
.size(barometerWidth
, barometerWidth
);
55 barometerWidth
=p
.width
;
61 // Angles for sin() and cos() start at 3 o'clock;
62 // subtract HALF_PI to make them start at the top
63 p
.ellipse(barometerWidth
/2, barometerWidth/2, barometerWidth
*0.8, barometerWidth
*0.8);
65 var angle
= -HALF_PI
+ HALF_PI
/ 3;
66 var inc
= TWO_PI
/ 12;
68 p
.strokeWeight(barometerWidth
*0.015);
69 p
.arc(barometerWidth
/2, barometerWidth/2, barometerWidth
*0.8, barometerWidth
*0.8, -(4/3)*PI, PI/3);
71 // we want a range from 200 hPa to 1200 hPa
72 // we want a range from ±950 - ±1050 milibar
73 // 1-5=1010-1050, 7-12=950-1000
74 p
.textSize(Math
.round(barometerWidth
*0.04));
75 for ( i
= 1; i
<= 12; i
++, angle
+= inc
) {
77 p
.line(barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.35,barometerWidth/2 + Math
.sin(angle
) * barometerWidth
*.35,barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.4,barometerWidth/2 + Math
.sin(angle
) * barometerWidth
*.4);
83 myWidth
= p
.textWidth(myText
);
85 p
.text(myText
, Math
.round(barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.3 - myWidth/2),Math
.round(barometerWidth
/2 + Math
.sin(angle
) * barometerWidth
*0.3));
87 myText
= pdata
+ ' ' + punit
;
88 myWidth
= p
.textWidth(myText
);
90 p
.text(myText
, Math
.round(barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.3 - myWidth/2),Math
.round(barometerWidth
/2 + Math
.sin(angle
) * barometerWidth
*0.3));
98 p
.strokeWeight(barometerWidth
*0.005);
99 p
.arc(barometerWidth
/2, barometerWidth/2, barometerWidth
*0.4, barometerWidth
*0.4, -(4/3)*PI, PI/3);
101 // we want a range from 0 - 100%
102 // 1-5=60-100, 7-12=0-50
103 p
.textSize(Math
.round(barometerWidth
*0.02));
104 for ( i
= 1; i
<= 12; i
++, angle
+= inc
) {
106 p
.line(barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.18,barometerWidth/2 + Math
.sin(angle
) * barometerWidth
*.18,barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.2,barometerWidth/2 + Math
.sin(angle
) * barometerWidth
*.2);
112 myWidth
= p
.textWidth(myText
);
114 p
.text(myText
, Math
.round(barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.16 - myWidth/2),Math
.round(barometerWidth
/2 + Math
.sin(angle
) * barometerWidth
*0.16));
116 myText
= hdata
+ ' ' + hunit
;
117 myWidth
= p
.textWidth(myText
);
119 p
.text(myText
, Math
.round(barometerWidth
/2 + Math.cos(angle) * barometerWidth*0.12 - myWidth/2),Math
.round(barometerWidth
/2 + Math
.sin(angle
) * barometerWidth
*0.12));
125 p
.strokeWeight(barometerWidth
*0.005);
126 p
.line(-Math
.cos(humidity
) * barometerWidth
*0.05 + barometerWidth
/2, -Math.sin(humidity) * barometerWidth*0.05 + barometerWidth/2, Math
.cos(humidity
) * barometerWidth
*0.15 + barometerWidth
/2, Math.sin(humidity) * barometerWidth*0.15 + barometerWidth/2);
127 //p.ellipse(barometerWidth/2, barometerWidth/2, barometerWidth/20, barometerWidth/20);
131 p
.strokeWeight(barometerWidth
*0.015);
132 p
.line(-Math
.cos(pressure
) * barometerWidth
*0.05 + barometerWidth
/2, -Math.sin(pressure) * barometerWidth*0.05 + barometerWidth/2, Math
.cos(pressure
) * barometerWidth
*0.35 + barometerWidth
/2, Math.sin(pressure) * barometerWidth*0.35 + barometerWidth/2);
133 p
.ellipse(barometerWidth
/2, barometerWidth/2, barometerWidth
/20, barometerWidth/20);
139 var thermometerSketchProc = function(p
) {
140 p
.size(thermometerWidth
, barometerHeight
);
141 p
.draw = function() {
142 p
.size(thermometerWidth
, barometerHeight
);
143 thermometerWidth
=p
.width
;
149 p
.strokeWeight(thermometerWidth
*0.27);
150 p
.line(thermometerWidth
/2,thermometerWidth*1.75,thermometerWidth/2,thermometerWidth
/4);
151 p
.ellipse(thermometerWidth
/2, thermometerWidth*1.75, thermometerWidth/5, thermometerWidth
/5);
153 p
.strokeWeight(thermometerWidth
*0.22);
155 p
.line(thermometerWidth
/2,thermometerWidth*1.75,thermometerWidth/2,thermometerWidth
/4);
162 p
.ellipse(thermometerWidth
/2, thermometerWidth*1.75, thermometerWidth/5, thermometerWidth
/5);
167 // don't exceed thermometer bounds.
169 p
.strokeCap("round");
171 p
.line(thermometerWidth
/2,thermometerWidth*1.75,thermometerWidth/2,thermometerWidth
*1.1 - (thermometerWidth
/50) * temp
);
173 p
.strokeCap("round");
175 p
.strokeWeight(thermometerWidth
*0.03);
176 var theight
= thermometerWidth
*1.5;
177 var inc
= thermometerWidth
/5;
179 p
.textSize(Math
.round(thermometerWidth
*0.06));
181 for ( i
= 1; i
<= 7; i
++, theight
-= inc
) {
182 // longer bar at zero degrees C
184 extra
= thermometerWidth
/10;
186 extra
= thermometerWidth
/20;
188 p
.line((thermometerWidth
/2) - thermometerWidth/8,theight
,(thermometerWidth
/2) - thermometerWidth/8 + extra
, theight
);
192 p
.text(myText
, (thermometerWidth
/2) - thermometerWidth
*0.09 + extra
, theight
+ 4);
195 p
.strokeWeight(thermometerWidth
*0.01);
196 p
.line((thermometerWidth
/2) + thermometerWidth/8,thermometerWidth
*1.1 - (thermometerWidth
/50) * tmin,(thermometerWidth/2) + thermometerWidth
/8 - thermometerWidth/20, thermometerWidth
*1.1 - (thermometerWidth
/50) * tmin
);
197 p
.line((thermometerWidth
/2) + thermometerWidth/8,thermometerWidth
*1.1 - (thermometerWidth
/50) * tmax,(thermometerWidth/2) + thermometerWidth
/8 - thermometerWidth/20, thermometerWidth
*1.1 - (thermometerWidth
/50) * tmax
);
198 p
.strokeWeight(thermometerWidth
*0.03);
200 myText
= temp
+ ' ' + tunit
;
202 p
.textSize(Math
.round(thermometerWidth
*0.09));
203 myWidth
= p
.textWidth(myText
);
204 p
.text(myText
, thermometerWidth
/2 - myWidth/2, thermometerWidth
*1.75 + thermometerWidth
*0.045);
210 var lightSketchProc = function(p
) {
212 p
.size(lightWidth
, barometerHeight
);
213 p
.draw = function() {
214 p
.size(lightWidth
, barometerHeight
);
220 p
.strokeWeight(lightWidth
*0.01);
222 if(fill_lux
> (3*255 - 10))
223 fill_lux
= (3*255 - 10);
224 p
.fill(fill_lux
/3 + 10);
225 p
.ellipse(lightWidth
/2,lightWidth,lightWidth/2,lightWidth
/2);
227 myText
= lux
+ ' ' + lunit
;
229 p
.textSize(Math
.round(lightWidth
*0.09));
230 myWidth
= p
.textWidth(myText
);
231 p
.text(myText
, lightWidth
/2 - myWidth/2, lightWidth
*1.4 + lightWidth
*0.045);
236 var canvas
= document
.getElementById("barometerCanvas");
237 var thermometerCanvas
= document
.getElementById("thermometerCanvas");
238 var lightCanvas
= document
.getElementById("lightCanvas");
239 var barometer
= new Processing(canvas
, barometerSketchProc
);
240 var thermometer
= new Processing(thermometerCanvas
, thermometerSketchProc
);
241 var light
= new Processing(lightCanvas
, lightSketchProc
);
243 function set_pressure(data
) {
245 var myData
= parseFloat(data
);
247 // Angles for sin() and cos() start at 3 o'clock;
248 // subtract HALF_PI to make them start at the top
249 pressure
= ((myData
- 700) / 10) * (TWO_PI
/ 120) - HALF_PI
;
252 if (myData
> pmax
) pmax
= myData
;
253 if (myData
< pmin
) pmin
= myData
;
258 function set_humidity(data
) {
260 var myData
= parseFloat(data
);
262 // Angles for sin() and cos() start at 3 o'clock;
263 // subtract HALF_PI to make them start at the top
265 humidity
= (myData
- 50) * (TWO_PI
/ 120) - HALF_PI
;
268 if (myData
> hmax
) hmax
= myData
;
269 if (myData
< hmin
) hmin
= myData
;
274 function set_temp(data
) {
276 var myData
= parseFloat(data
);
280 if (myData
> tmax
) tmax
= myData
;
281 if (myData
< tmin
) tmin
= myData
;
283 thermometer
.redraw();
286 function set_lux(data
) {
288 var myData
= parseFloat(data
);
294 function resizeHandler() {
298 thermometer
.redraw();
302 window
.onresize
=resizeHandler
;