Skip to content

Commit

Permalink
Merge pull request #47 from CarletonURocketry/select-sensor
Browse files Browse the repository at this point in the history
Add an option for only starting a single sensor
  • Loading branch information
linguini1 authored Jun 2, 2024
2 parents d40b374 + 6bc0898 commit e1df64c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions fetcher.use
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ OPTIONS:
-p If this flag is passed, fetcher will print its sensor data to
stdout. Enabling this flag will take messages off the output
message queue.

-s If this flag is passed, fetcher will only open and read
sensor data from the sensor whose name follows.
32 changes: 26 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@
/** The maximum number of sensors that fetcher can support. */
#define MAX_SENSORS 8

#define SYSCLOCK_NAME "sysclock"

/** Whether or not to print data to stdout. */
bool print_output = false;

/** The name of a single sensor to enable, or null if no sensor was selected */
char *select_sensor = NULL;

/** Stores the thread IDs of all the collector threads. */
static pthread_t collector_threads[MAX_SENSORS];

Expand Down Expand Up @@ -112,11 +117,14 @@ int main(int argc, char **argv) {
opterr = 0;

/* Get command line options. */
while ((c = getopt(argc, argv, ":p")) != -1) {
while ((c = getopt(argc, argv, ":ps:")) != -1) {
switch (c) {
case 'p':
print_output = true;
break;
case 's':
select_sensor = optarg;
break;
case ':':
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -194,8 +202,17 @@ int main(int argc, char **argv) {
uint8_t naddrs = MAX_ADDR_PER_SENSOR;
cur = read_sensor_addresses(cur, addresses, &naddrs);

// Check if the sensor that was requested is this sensor (if none match, will do nothing)
if (select_sensor != NULL) {
if (strncasecmp(select_sensor, sensor_name, MAX_SENSOR_NAME) != 0) {
// Skip this sensor, not the right one
fprintf(stderr, "Skipping sensor %s\n", sensor_name);
continue;
} else {
fprintf(stderr, "Found sensor %s, starting\n", select_sensor);
}
}
for (uint8_t i = 0; i < naddrs; i++) {

/* Create sensor data collection threads. */
collector_t collector = collector_search(sensor_name);
if (collector == NULL) {
Expand All @@ -211,10 +228,13 @@ int main(int argc, char **argv) {
num_sensors++; // Record that a new sensor was created
}
}

/* Add sysclock sensor because it won't be specified in board ID. */
collector_t sysclock = collector_search("sysclock");
err = pthread_create(&collector_threads[num_sensors], NULL, sysclock, NULL);
// Only start the sysclock if we're not debugging a single sensor or if this is the sensor that was selected
if (select_sensor == NULL || !strncasecmp(select_sensor, SYSCLOCK_NAME, sizeof(SYSCLOCK_NAME))) {
/* Add sysclock sensor because it won't be specified in board ID. */
collector_t sysclock = collector_search(SYSCLOCK_NAME);
err = pthread_create(&collector_threads[num_sensors], NULL, sysclock, NULL);
num_sensors++;
}

/* Constantly receive from sensors on message queue and print data. */
while (print_output) {
Expand Down

0 comments on commit e1df64c

Please sign in to comment.