brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "DriverStationDisplay.h" |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | |
Brian Silverman | a452091 | 2013-03-16 14:39:51 -0700 | [diff] [blame] | 6 | #include "aos/common/logging/logging.h" |
| 7 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | using namespace aos; |
| 9 | |
| 10 | static const aos_type_sig signature = {sizeof(DriverStationDisplay), 1234, 10}; |
| 11 | aos_queue *DriverStationDisplay::queue = NULL; |
| 12 | void DriverStationDisplay::GetQueue() { |
| 13 | if (queue == NULL) { |
| 14 | queue = aos_fetch_queue("DriverStationDisplay", &signature); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | void 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 | |
| 48 | const DriverStationDisplay *DriverStationDisplay::GetNext() { |
| 49 | GetQueue(); |
| 50 | return static_cast<const DriverStationDisplay *>(aos_queue_read_msg(queue, NON_BLOCK)); |
| 51 | } |
| 52 | void DriverStationDisplay::Free(const DriverStationDisplay *msg) { |
| 53 | GetQueue(); |
| 54 | aos_queue_free_msg(queue, msg); |
| 55 | } |
| 56 | |