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 | |
| 6 | using namespace aos; |
| 7 | |
| 8 | static const aos_type_sig signature = {sizeof(DriverStationDisplay), 1234, 10}; |
| 9 | aos_queue *DriverStationDisplay::queue = NULL; |
| 10 | void DriverStationDisplay::GetQueue() { |
| 11 | if (queue == NULL) { |
| 12 | queue = aos_fetch_queue("DriverStationDisplay", &signature); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | void DriverStationDisplay::Send(int line, const char *fmt, ...) { |
| 17 | GetQueue(); |
| 18 | DriverStationDisplay *msg = static_cast<DriverStationDisplay *>( |
| 19 | aos_queue_get_msg(queue)); |
| 20 | if (msg == NULL) { |
| 21 | LOG(WARNING, "could not get message to send '%s' to the DS queue\n", fmt); |
| 22 | return; |
| 23 | } |
| 24 | msg->line = static_cast<uint8_t>(line); |
| 25 | |
| 26 | va_list ap; |
| 27 | va_start(ap, fmt); |
| 28 | int ret = vsnprintf(msg->data, sizeof(msg->data), fmt, ap); |
| 29 | va_end(ap); |
| 30 | if (ret < 0) { |
| 31 | LOG(WARNING, "could not format '%s' with arguments\n", fmt); |
| 32 | aos_queue_free_msg(queue, msg); |
| 33 | return; |
| 34 | } else if (static_cast<uintmax_t>(ret) >= |
| 35 | static_cast<uintmax_t>(sizeof(msg->data))) { |
| 36 | LOG(WARNING, "format '%s' ended up longer than the max size (%zd)\n", |
| 37 | fmt, sizeof(msg->data)); |
| 38 | } |
| 39 | |
| 40 | if (aos_queue_write_msg(queue, msg, NON_BLOCK) < 0) { |
| 41 | LOG(ERROR, "writing '%s' (line %hhd) failed\n", msg->data, msg->line); |
| 42 | aos_queue_free_msg(queue, msg); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | const DriverStationDisplay *DriverStationDisplay::GetNext() { |
| 47 | GetQueue(); |
| 48 | return static_cast<const DriverStationDisplay *>(aos_queue_read_msg(queue, NON_BLOCK)); |
| 49 | } |
| 50 | void DriverStationDisplay::Free(const DriverStationDisplay *msg) { |
| 51 | GetQueue(); |
| 52 | aos_queue_free_msg(queue, msg); |
| 53 | } |
| 54 | |