blob: 076ba3f5f9c601b15996e076534ce4fa3406bb3a [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "DriverStationDisplay.h"
2
3#include <stdarg.h>
4#include <stdio.h>
5
Brian Silvermana4520912013-03-16 14:39:51 -07006#include "aos/common/logging/logging.h"
7
brians343bc112013-02-10 01:53:46 +00008using namespace aos;
9
10static const aos_type_sig signature = {sizeof(DriverStationDisplay), 1234, 10};
11aos_queue *DriverStationDisplay::queue = NULL;
12void DriverStationDisplay::GetQueue() {
13 if (queue == NULL) {
14 queue = aos_fetch_queue("DriverStationDisplay", &signature);
15 }
16}
17
18void DriverStationDisplay::Send(int line, const char *fmt, ...) {
19 GetQueue();
20 DriverStationDisplay *msg = static_cast<DriverStationDisplay *>(
21 aos_queue_get_msg(queue));
22 if (msg == NULL) {
23 LOG(WARNING, "could not get message to send '%s' to the DS queue\n", fmt);
24 return;
25 }
26 msg->line = static_cast<uint8_t>(line);
27
28 va_list ap;
29 va_start(ap, fmt);
30 int ret = vsnprintf(msg->data, sizeof(msg->data), fmt, ap);
31 va_end(ap);
32 if (ret < 0) {
33 LOG(WARNING, "could not format '%s' with arguments\n", fmt);
34 aos_queue_free_msg(queue, msg);
35 return;
36 } else if (static_cast<uintmax_t>(ret) >=
37 static_cast<uintmax_t>(sizeof(msg->data))) {
38 LOG(WARNING, "format '%s' ended up longer than the max size (%zd)\n",
39 fmt, sizeof(msg->data));
40 }
41
42 if (aos_queue_write_msg(queue, msg, NON_BLOCK) < 0) {
43 LOG(ERROR, "writing '%s' (line %hhd) failed\n", msg->data, msg->line);
44 aos_queue_free_msg(queue, msg);
45 }
46}
47
48const DriverStationDisplay *DriverStationDisplay::GetNext() {
49 GetQueue();
50 return static_cast<const DriverStationDisplay *>(aos_queue_read_msg(queue, NON_BLOCK));
51}
52void DriverStationDisplay::Free(const DriverStationDisplay *msg) {
53 GetQueue();
54 aos_queue_free_msg(queue, msg);
55}
56