107 lines
3.3 KiB
C
107 lines
3.3 KiB
C
/*
|
|
* The sensor sends ten readings, each a minute and a moisture level.
|
|
* Check each one, store the valid ones, then report on what you stored.
|
|
* README.md says how to build and run it. Do not edit api/.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "api/moisture_sensor.h"
|
|
|
|
#define MAX_READINGS 10
|
|
|
|
/* What classify() returns: the four outcomes from the first moister detection assignment */
|
|
#define LEVEL_ERROR 0
|
|
#define LEVEL_IDLE 1
|
|
#define LEVEL_COLLECT 2
|
|
#define LEVEL_SATURATED 3
|
|
|
|
int classify(int level);
|
|
int count_in_band(int levels[], size_t count, int low, int high);
|
|
size_t index_of_max(int levels[], size_t count);
|
|
int average(int levels[], size_t count);
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
/* Two parallel arrays: slot i of times and slot i of levels are one reading. */
|
|
int times[MAX_READINGS];
|
|
int levels[MAX_READINGS];
|
|
|
|
int count = 0;
|
|
unsigned int seed = 2060;
|
|
int readings = 10;
|
|
|
|
if (argc > 1) {
|
|
seed = (unsigned int)atoi(argv[1]);
|
|
}
|
|
if (argc > 2) {
|
|
readings = atoi(argv[2]);
|
|
}
|
|
|
|
sensor_start(seed, readings);
|
|
while (sensor_has_next()) {
|
|
int t = sensor_time();
|
|
int level = sensor_level();
|
|
|
|
/*
|
|
* YOUR TASK: use classify(). A sensor fault is printed as
|
|
* printf(" skipped: sensor fault at minute %d\n", t);
|
|
* and not stored. Every other reading goes into the next free slot
|
|
* of times and levels, and count goes up by one.
|
|
* Part C: when the arrays are full, print
|
|
* printf(" dropped: no room for minute %d\n", t);
|
|
* instead of storing.
|
|
*/
|
|
(void)t; (void)level; /* delete once you use them */
|
|
}
|
|
|
|
printf("\n\nstored: %d\n", count);
|
|
printf("collecting (1 to 100): %d\n", count_in_band(levels, count, 1, 100));
|
|
if (count > 0) {
|
|
size_t wettest = index_of_max(levels, count);
|
|
printf("wettest: level %d at minute %d\n", levels[wettest], times[wettest]);
|
|
}
|
|
printf("average level: %d\n", average(levels, count));
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* YOUR TASK: return LEVEL_ERROR for a negative level, LEVEL_IDLE for 0,
|
|
* LEVEL_COLLECT for 1 to 100, and LEVEL_SATURATED above 100.
|
|
*/
|
|
int classify(int level)
|
|
{
|
|
(void)level; /* delete once you use it */
|
|
return LEVEL_IDLE;
|
|
}
|
|
|
|
/*
|
|
* YOUR TASK: return how many levels are between low and high, including both
|
|
* ends. When low is greater than high, no level can be in the band.
|
|
*/
|
|
int count_in_band(int levels[], size_t count, int low, int high)
|
|
{
|
|
(void)levels; (void)count; (void)low; (void)high; /* delete once you use them */
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* YOUR TASK: return the index of the largest level. When two levels tie,
|
|
* return the earlier index. main() only calls this when count is at least 1.
|
|
*/
|
|
size_t index_of_max(int levels[], size_t count)
|
|
{
|
|
(void)levels; (void)count; /* delete once you use them */
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* YOUR TASK: return the average level, rounded toward zero the way integer
|
|
* division rounds. With no levels there is no average: return 0 rather than
|
|
* divide by zero.
|
|
*/
|
|
int average(int levels[], size_t count)
|
|
{
|
|
(void)levels; (void)count; /* delete once you use them */
|
|
return 0;
|
|
}
|