Skip to content

Commit

Permalink
Merge pull request #32 from CarletonURocketry/sysclock-overhaul
Browse files Browse the repository at this point in the history
Sysclock overhaul
  • Loading branch information
linguini1 authored May 13, 2024
2 parents 1aaaa0d + 1fcf22f commit c3a873d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 142 deletions.
46 changes: 25 additions & 21 deletions src/collectors/sysclock_clctr.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
#include "../drivers/sysclock/sysclock.h"
#include "collectors.h"
#include "sensor_api.h"
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

/** Type to simplify sending time data. */
struct sysclock_msg_t {
uint8_t type; /**< The type of message (always time). */
uint32_t millis; /**< The number of milliseconds since launch. */
} __attribute__((packed));

/**
* Collector thread for the system clock.
* @param args Arguments in the form of `collector_args_t`
* @return The error `errno_t` which caused the thread to exit, encoded as a pointer.
*/
void *sysclock_collector(void *args) {

(void)(args); // Ignore that args is unused

/* Open message queue to send data. */
mqd_t sensor_q = mq_open(SENSOR_QUEUE, O_WRONLY);
if (sensor_q == -1) {
fprintf(stderr, "Sysclock collector could not open message queue '%s': '%s' \n", SENSOR_QUEUE, strerror(errno));
return (void *)((uint64_t)errno);
}

/* Create system clock instance. */
Sensor clock;
sysclock_init(&clock, clctr_args(args)->bus, clctr_args(args)->addr, PRECISION_HIGH);
uint8_t sysclock_context[sensor_get_ctx_size(clock)];
sensor_set_ctx(&clock, sysclock_context);

errno_t err = sensor_open(clock);

if (err != EOK) {
fprintf(stderr, "%s\n", strerror(err));
return (void *)((uint64_t)err); // Extra uint64_t cast to silence compiler warning
}

// Data storage
uint8_t data[sensor_max_dsize(&clock) + 1];
size_t nbytes;
// Get the current UNIX time and time information
time_t start_unix_time;
time(&start_unix_time);
struct tm *time_info = localtime(&start_unix_time);
struct timezone tz = {.tz_dsttime = 0, .tz_minuteswest = time_info->tm_gmtoff};
struct timeval tval;

// Infinitely check the time
struct sysclock_msg_t msg;
msg.type = TAG_TIME;
for (;;) {

clock.read(&clock, TAG_TIME, &data[1], &nbytes);
data[0] = TAG_TIME; // Encode the contained data type
// Get time with nanosecond precision
gettimeofday(&tval, &tz);

// Calculate elapsed time from launch
time_t elapsed_s = tval.tv_sec - start_unix_time;
msg.millis = (elapsed_s * 1000) + (tval.tv_usec / 1000);

// Infinitely send the time
if (mq_send(sensor_q, (char *)data, sizeof(data), 0) == -1) {
if (mq_send(sensor_q, (char *)&msg, sizeof(msg), 0) == -1) {
fprintf(stderr, "Sysclock couldn't send message: %s.\n", strerror(errno));
}
usleep(10000); // Little sleep to not flood output
usleep(10000); // Little sleep to not flood message queue
}
}
106 changes: 0 additions & 106 deletions src/drivers/sysclock/sysclock.c

This file was deleted.

13 changes: 0 additions & 13 deletions src/drivers/sysclock/sysclock.h

This file was deleted.

3 changes: 1 addition & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ int main(int argc, char **argv) {

/* Add sysclock sensor because it won't be specified in board ID. */
collector_t sysclock = collector_search("sysclock");
collector_args[num_sensors] = (collector_args_t){.bus = bus, .addr = 0x00};
err = pthread_create(&collector_threads[num_sensors], NULL, sysclock, &collector_args[num_sensors]);
err = pthread_create(&collector_threads[num_sensors], NULL, sysclock, NULL);

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

0 comments on commit c3a873d

Please sign in to comment.