47 enum class Unit {Centim, Inch, Ms, Kmh, Mh, Cms, mHz, Hz};
62 virtual long long int measure(
Unit unit = Unit::Centim) = 0;
68 hwlib::target::pin_out & trig;
69 hwlib::target::pin_in & echo;
73 hcsr04(hwlib::target::pin_out & trig, hwlib::target::pin_in & echo):
80 long long int timestamp1 = 0, timestamp2 = 0, timedif = 0;
87 while (echo.get() == 0) {}
88 timestamp1 = hwlib::now_us();
89 while (echo.get() == 1){}
90 timestamp2 = hwlib::now_us();
92 timedif = timestamp2 - timestamp1;
94 case Unit::Centim :
return timedif / 58;
95 case Unit::Inch :
return timedif / 148;
96 default : hwlib::cout <<
"ERROR: Entered unit not available in this function\n";
return 0;
128 int timestamp = hwlib::now_us();
129 long long int res1 = meter.
measure();
130 hwlib::wait_ms(delay);
131 long long int res2 = meter.
measure();
132 int timedif = (hwlib::now_us() - timestamp) / 1000;
133 long long int result = res1-res2;
135 case Unit::Ms :
return ((result*10) / timedif);
136 case Unit::Kmh :
return ((result*36) / timedif);
137 case Unit::Mh :
return ((result * 22) / timedif);
138 case Unit::Cms :
return ((result*1000) / timedif);
139 default : hwlib::cout <<
"ERROR: Entered unit not available in this function\n";
return 0;
150 int getFreq(
int measurements,
Unit unit = Unit::Hz,
int delay1 = 10,
int delay2 = 100) {
152 int timestamp = hwlib::now_us();
153 bool status = getSpeed(Unit::Cms) > 0;
154 for (
int i = 0; i < measurements; i++) {
156 if (getSpeed(Unit::Cms, delay2) < 0) {
162 if (getSpeed(Unit::Cms, delay2) > 0) {
167 hwlib::wait_ms(delay1);
169 int time = (hwlib::now_us() - timestamp) / 1000;
171 case Unit::Hz :
return switches / (time / 1000);
172 case Unit::mHz :
return (switches*1000) / (time /1000);
173 default : hwlib::cout <<
"ERROR: Entered unit not available in this function\n";
return 0;
182 int bitmapsize[123] = {};
184 hwlib::location bitmaps[123][20];
203 bitmapsize[104] = 11;
204 bitmapsize[107] = 11;
205 bitmapsize[109] = 12;
206 bitmapsize[115] = 10;
207 bitmapsize[122] = 10;
209 hwlib::location b39[] = {
210 hwlib::location(2, 1), hwlib::location(4, 1),
211 hwlib::location(2, 2), hwlib::location(4, 2)
213 memcpy(bitmaps[39], b39, 20*
sizeof(b39[0]));
215 hwlib::location b45[] = {
216 hwlib::location(2, 3), hwlib::location(3, 3), hwlib::location(4, 3), hwlib::location(5, 3),
217 hwlib::location(2, 4), hwlib::location(3, 4), hwlib::location(4, 4), hwlib::location(5, 4)
219 memcpy(bitmaps[45], b45, 20*
sizeof(b39[0]));
221 hwlib::location b47[] = {
222 hwlib::location(6, 1),
223 hwlib::location(5, 2),
224 hwlib::location(4, 3),
225 hwlib::location(3, 4),
226 hwlib::location(2, 5),
227 hwlib::location(1, 6)
229 memcpy(bitmaps[47], b47, 20*
sizeof(b39[0]));
231 hwlib::location b48[] = {
232 hwlib::location(3, 1), hwlib::location(4, 1),
233 hwlib::location(2, 2), hwlib::location(5, 2),
234 hwlib::location(1, 3), hwlib::location(6, 3),
235 hwlib::location(1, 4), hwlib::location(6, 4),
236 hwlib::location(2, 5), hwlib::location(5, 5),
237 hwlib::location(3, 6), hwlib::location(4, 6)
239 memcpy(bitmaps[48], b48, 20*
sizeof(b39[0]));
241 hwlib::location b49[] = {
242 hwlib::location(2, 1),
243 hwlib::location(1, 2), hwlib::location(2, 2),
244 hwlib::location(2, 3),
245 hwlib::location(2, 4),
246 hwlib::location(2, 5),
247 hwlib::location(1, 6), hwlib::location(2, 6), hwlib::location(3, 6)
249 memcpy(bitmaps[49], b49, 20*
sizeof(b39[0]));
251 hwlib::location b50[] = {
252 hwlib::location(2, 1), hwlib::location(3, 1), hwlib::location(4, 1), hwlib::location(5, 1),
253 hwlib::location(1, 2), hwlib::location(6, 2),
254 hwlib::location(5, 3),
255 hwlib::location(3, 4), hwlib::location(4, 4),
256 hwlib::location(2, 5),
257 hwlib::location(1, 6), hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6), hwlib::location(5, 6), hwlib::location(6, 6)
259 memcpy(bitmaps[50], b50, 20*
sizeof(b39[0]));
261 hwlib::location b51[] = {
262 hwlib::location(2, 1), hwlib::location(3, 1), hwlib::location(4, 1), hwlib::location(5, 1),
263 hwlib::location(1, 2), hwlib::location(6, 2),
264 hwlib::location(5, 3),
265 hwlib::location(3, 4), hwlib::location(4, 4),
266 hwlib::location(1, 5), hwlib::location(6, 5),
267 hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6), hwlib::location(5, 6)
269 memcpy(bitmaps[51], b51, 20*
sizeof(b39[0]));
271 hwlib::location b52[] = {
272 hwlib::location(4, 1),
273 hwlib::location(3, 2), hwlib::location(4, 2),
274 hwlib::location(2, 3), hwlib::location(4, 3),
275 hwlib::location(1, 4), hwlib::location(4, 4),
276 hwlib::location(1, 5), hwlib::location(2, 5), hwlib::location(3, 5), hwlib::location(4, 5), hwlib::location(5, 5),
277 hwlib::location(4, 6)
279 memcpy(bitmaps[52], b52, 20*
sizeof(b39[0]));
281 hwlib::location b53[] = {
282 hwlib::location(1, 1), hwlib::location(2, 1), hwlib::location(3, 1), hwlib::location(4, 1), hwlib::location(5, 1), hwlib::location(6,6),
283 hwlib::location(1, 2),
284 hwlib::location(1, 3), hwlib::location(2, 3), hwlib::location(3, 3), hwlib::location(4, 3), hwlib::location(5, 3),
285 hwlib::location(6, 4),
286 hwlib::location(1, 5), hwlib::location(6, 5),
287 hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6), hwlib::location(5, 6)
289 memcpy(bitmaps[53], b53, 20*
sizeof(b39[0]));
291 hwlib::location b54[] = {
292 hwlib::location(2, 1), hwlib::location(3, 1), hwlib::location(4, 1), hwlib::location(5, 1),
293 hwlib::location(1, 2),
294 hwlib::location(1, 3), hwlib::location(2, 3), hwlib::location(3, 3), hwlib::location(4, 3), hwlib::location(5, 3),
295 hwlib::location(1, 4), hwlib::location(6, 4),
296 hwlib::location(1, 5), hwlib::location(6, 5),
297 hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6), hwlib::location(5, 6)
299 memcpy(bitmaps[54], b54, 20*
sizeof(b39[0]));
301 hwlib::location b55[] = {
302 hwlib::location(1, 1), hwlib::location(2, 1), hwlib::location(3, 1), hwlib::location(4, 1), hwlib::location(5, 1), hwlib::location(6, 1),
303 hwlib::location(5, 2),
304 hwlib::location(4, 3),
305 hwlib::location(3, 4),
306 hwlib::location(2, 5),
307 hwlib::location(1, 6)
309 memcpy(bitmaps[55], b55, 20*
sizeof(b39[0]));
311 hwlib::location b56[] = {
312 hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6), hwlib::location(5, 6),
313 hwlib::location(1, 5), hwlib::location(6, 5),
314 hwlib::location(2, 4), hwlib::location(3, 4), hwlib::location(4, 4), hwlib::location(5, 4),
315 hwlib::location(1, 3), hwlib::location(6, 3),
316 hwlib::location(1, 2), hwlib::location(6, 2),
317 hwlib::location(2, 1), hwlib::location(3, 1), hwlib::location(4, 1), hwlib::location(5, 1)
319 memcpy(bitmaps[56], b56, 20*
sizeof(b39[0]));
321 hwlib::location b57[] = {
322 hwlib::location(2, 1), hwlib::location(3, 1), hwlib::location(4, 1), hwlib::location(5, 1),
323 hwlib::location(1, 2), hwlib::location(6, 2),
324 hwlib::location(1, 3), hwlib::location(6, 3),
325 hwlib::location(2, 4), hwlib::location(3, 4), hwlib::location(4, 4), hwlib::location(5, 4), hwlib::location(6, 4),
326 hwlib::location(6, 5),
327 hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6), hwlib::location(5, 6)
329 memcpy(bitmaps[57], b57, 20*
sizeof(b39[0]));
331 hwlib::location b72[] = {
332 hwlib::location(1, 1), hwlib::location(6, 1),
333 hwlib::location(1, 2), hwlib::location(6, 2),
334 hwlib::location(1, 3), hwlib::location(2, 3), hwlib::location(3, 3), hwlib::location(4, 3), hwlib::location(5, 3), hwlib::location(6, 3),
335 hwlib::location(1, 4), hwlib::location(6, 4),
336 hwlib::location(1, 5), hwlib::location(6, 5),
337 hwlib::location(1, 6), hwlib::location(6, 6)
339 memcpy(bitmaps[72], b72, 20*
sizeof(b39[0]));
341 hwlib::location b99[] = {
342 hwlib::location(2, 3), hwlib::location(3, 3), hwlib::location(4, 3),
343 hwlib::location(1, 4),
344 hwlib::location(1, 5),
345 hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6)
347 memcpy(bitmaps[99], b99, 20*
sizeof(b39[0]));
349 hwlib::location b104[] = {
350 hwlib::location(1, 1),
351 hwlib::location(1, 2),
352 hwlib::location(1, 3), hwlib::location(2, 3), hwlib::location(3, 3),
353 hwlib::location(1, 4), hwlib::location(4, 4),
354 hwlib::location(1, 5), hwlib::location(4, 5),
355 hwlib::location(1, 6), hwlib::location(4, 6)
357 memcpy(bitmaps[104], b104, 20*
sizeof(b39[0]));
359 hwlib::location b107[] = {
360 hwlib::location(1, 1),
361 hwlib::location(1, 2),
362 hwlib::location(1, 3), hwlib::location(4, 3),
363 hwlib::location(1, 4), hwlib::location(3, 4),
364 hwlib::location(1, 5), hwlib::location(2, 5),
365 hwlib::location(1, 6), hwlib::location(3, 6), hwlib::location(4, 6)
367 memcpy(bitmaps[107], b107, 20*
sizeof(b39[0]));
369 hwlib::location b109[] = {
370 hwlib::location(1, 3), hwlib::location(2, 3), hwlib::location(5, 3), hwlib::location(6, 3),
371 hwlib::location(1, 4), hwlib::location(3, 4), hwlib::location(4, 4), hwlib::location(6, 4),
372 hwlib::location(1, 5), hwlib::location(6, 5),
373 hwlib::location(1, 6), hwlib::location(6, 6)
375 memcpy(bitmaps[109], b109, 20*
sizeof(b39[0]));
377 hwlib::location b115[] = {
378 hwlib::location(2, 2), hwlib::location(3, 2), hwlib::location(4, 2),
379 hwlib::location(1, 3),
380 hwlib::location(2, 4), hwlib::location(3, 4),
381 hwlib::location(3, 5),
382 hwlib::location(1, 6), hwlib::location(2, 6), hwlib::location(3, 6)
384 memcpy(bitmaps[115], b115, 20*
sizeof(b39[0]));
386 hwlib::location b122[] = {
387 hwlib::location(1, 3), hwlib::location(2, 3), hwlib::location(3, 3), hwlib::location(4, 3),
388 hwlib::location(3, 4),
389 hwlib::location(2, 5),
390 hwlib::location(1, 6), hwlib::location(2, 6), hwlib::location(3, 6), hwlib::location(4, 6),
392 memcpy(bitmaps[122], b122, 20*
sizeof(b39[0]));
398 return bitmapsize[value];
417 for (
int i = 1; i < ((41*width)/64); i++) {
418 for (
int j = 1; j < ((perc*164)/128); j++) {
419 w.write(hwlib::location(j,i), hwlib::black);
431 hwlib::location heightcorr(0, 29+height);
433 for (
int i = 0; i < arrays.
getSize(
'0'); i++) {
434 w.write(arrays.bitmaps[
'0'][i] + heightcorr, hwlib::black);
440 int a[4] = { 0,0,0,0 };
445 if ((number / 1000) > 0) {
455 else if ((number / 100) > 0) {
463 else if ((number / 10) > 0) {
475 for (
int i = 0; i < arrays.
getSize(
'-'); i++) {
476 w.write(arrays.bitmaps[
'-'][i] + heightcorr, hwlib::black);
480 for (
int i = 0; i < digits; i++) {
481 for (
int j = 0; j < arrays.
getSize(a[i] +
'0'); j++) {
482 w.write(arrays.bitmaps[a[i] +
'0'][j] + heightcorr + hwlib::location(xcorrection, 0), hwlib::black);
498 hwlib::location heightcorr(0, 29+height);
505 int a[4] = { 0,0,0,0 };
510 if ((number / 1000) > 0) {
520 else if ((number / 100) > 0) {
528 else if ((number / 10) > 0) {
540 for (
int i = 0; i < arrays.
getSize(
'-'); i++) {
541 w.write(arrays.bitmaps[
'-'][i] + heightcorr, hwlib::black);
546 for (
int i = 0; i < digits; i++) {
547 for (
int j = 0; j < arrays.
getSize(a[i] +
'0'); j++) {
548 w.write(arrays.bitmaps[a[i] +
'0'][j] + heightcorr + hwlib::location(xcorrection, 0), hwlib::black);
554 for (
int i = 0; i < arrays.
getSize(
'0'); i++) {
555 w.write(arrays.bitmaps[
'0'][i] + heightcorr, hwlib::black);
561 for (
int i = 0; unit[i] !=
'\0'; i++) {
562 for (
int j = 0; j < arrays.
getSize(unit[i]); j++) {
563 int character = unit[i];
564 w.write(arrays.bitmaps[character][j] + heightcorr + hwlib::location(xcorrection, 0), hwlib::black);
585 virtual int getResult(
Unit unit) = 0;
590 virtual void dispResult(
Unit unit,
int height) = 0;
622 int result = meter.
measure(unit);
624 hwlib::cout <<
"WARNING: Inaccurate measurement detected!\n";
628 case Unit::Centim : screen.
dispNumberUnit(result, (
char*)
"cm", height);
return;
629 case Unit::Inch : screen.
dispNumberUnit(result, (
char*)
"\'", height);
return;
630 default : hwlib::cout <<
"ERROR: Entered unit not available in this function\n";
return;
666 int result = meter.
getSpeed(unit, delay);
668 case Unit::Cms : screen.
dispNumberUnit(result, (
char*)
"cm/s", height);
return;
669 case Unit::Kmh : screen.
dispNumberUnit(result, (
char*)
"km/h", height);
return;
670 case Unit::Mh : screen.
dispNumberUnit(result, (
char*)
"m/h", height);
return;
671 case Unit::Ms : screen.
dispNumberUnit(result, (
char*)
"m/s", height);
return;
672 default : hwlib::cout <<
"ERROR: Entered unit not available in this function\n";
return;
691 int measurements = 50;
704 return meter.
getFreq(measurements, unit, delay1, delay2);
715 int result = meter.
getFreq(measurements, unit, delay1, delay2);
717 case Unit::Hz : screen.
dispNumberUnit(result, (
char*)
"Hz", height);
return;
718 case Unit::mHz : screen.
dispNumberUnit(result, (
char*)
"mHz", height);
return;
719 default : hwlib::cout <<
"ERROR: Entered unit not available in this function\n";
return;
hcsr04(hwlib::target::pin_out &trig, hwlib::target::pin_in &echo)
Constructor which has two pins as parameters, trig output pin and echo input pin. ...
Definition: HCSR04.h:73
Controller superclass.
Definition: HCSR04.h:574
void setDelay1(int delay)
Function for setting the delay between getSpeed() calls.
Definition: HCSR04.h:724
Sub-class for distance sensor HCSR04 which is a sonar based distance sensor.
Definition: HCSR04.h:66
int getFreq(int measurements, Unit unit=Unit::Hz, int delay1=10, int delay2=100)
Calculates the frequency of a moving object.
Definition: HCSR04.h:150
distancemeter()
Empty constuctor.
Definition: HCSR04.h:53
int getResult(Unit unit)
Function for fetching result.
Definition: HCSR04.h:652
Class solely for storing the character bitmaps, and information about these character bitmaps...
Definition: HCSR04.h:180
void dispResult(Unit unit, int height)
Function for displaying result directly on display.
Definition: HCSR04.h:621
int getResult(Unit unit)
Function for fetching result.
Definition: HCSR04.h:611
Abstract class which is able to measure distance.
Definition: HCSR04.h:50
void dispResult(Unit unit, int height)
Function for displaying result directly on display.
Definition: HCSR04.h:665
void setDelay(int pdelay)
Function for setting the delay between the two distance measurements inside getSpeed().
Definition: HCSR04.h:680
void dispNumber(int number, int height)
This function displays an integer on the display.
Definition: HCSR04.h:429
Unit
Units used in distancemeter and speedometer.
Definition: HCSR04.h:47
void dispResult(Unit unit, int height)
Function for displaying result directly on display.
Definition: HCSR04.h:714
Speedometer controller.
Definition: HCSR04.h:636
void dispNumberUnit(int number, char unit[], int height)
This function displays a number and a unit. .
Definition: HCSR04.h:496
void setDelay2(int delay)
Function for setting the delay inside the getSpeed() function, between the two distance measurements...
Definition: HCSR04.h:729
int getResult(Unit unit)
Function for fetching result..
Definition: HCSR04.h:703
controller(display &screen)
Superclass constructor.
Definition: HCSR04.h:579
long long int getSpeed(Unit unit=Unit::Ms, int delay=10)
Calculates the speed of a moving object.
Definition: HCSR04.h:127
void setMeasurements(int mes)
Function for setting the amount of times getSpeed() is called during getResult() or dispResult()...
Definition: HCSR04.h:734
long long int measure(Unit unit=Unit::Centim)
Distance measurement function for the HCSR04 sonar distance sensor.
Definition: HCSR04.h:79
Class which can create objects which can calculate speed. Requires object distance sensor...
Definition: HCSR04.h:103
Class for displaying certain data on a display.
Definition: HCSR04.h:403
Frequency meter controller.
Definition: HCSR04.h:686
virtual long long int measure(Unit unit=Unit::Centim)=0
Virtual function which returns the distance from the sensor to an object in unit defined in parameter...
Distance meter controller.
Definition: HCSR04.h:597
void dispPerc(int perc, int width)
Function which display a percentage in the form of a percentage bar in horizontal direction...
Definition: HCSR04.h:415
int getSize(char c)
Function for requesting the bitmap size of a certain character.
Definition: HCSR04.h:396