2012-12-18 20:09:40 -04:00
|
|
|
|
|
|
|
#include "Util.h"
|
2012-12-19 18:01:26 -04:00
|
|
|
#include "print_vprintf.h"
|
2012-12-18 20:09:40 -04:00
|
|
|
using namespace AP_HAL_AVR;
|
|
|
|
|
2012-12-18 20:44:25 -04:00
|
|
|
/* Helper class implements AP_HAL::Print so we can use utility/vprintf */
|
|
|
|
class BufferPrinter : public AP_HAL::Print {
|
|
|
|
public:
|
|
|
|
BufferPrinter(char* str, size_t size) : _str(str), _size(size), _offs(0) {}
|
|
|
|
size_t write(uint8_t c) {
|
|
|
|
if (_offs < _size) {
|
|
|
|
_str[_offs] = c;
|
|
|
|
_offs++;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t _offs;
|
|
|
|
char* const _str;
|
|
|
|
const size_t _size;
|
|
|
|
};
|
|
|
|
|
2012-12-18 20:09:40 -04:00
|
|
|
int AVRUtil::snprintf(char* str, size_t size, const char *format, ...)
|
|
|
|
{
|
2012-12-18 20:44:25 -04:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
int res = this->vsnprintf(str, size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
2012-12-18 20:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int AVRUtil::snprintf_P(char* str, size_t size, const prog_char_t *format, ...)
|
|
|
|
{
|
2012-12-18 20:44:25 -04:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
int res = this->vsnprintf_P(str, size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
2012-12-18 20:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int AVRUtil::vsnprintf(char* str, size_t size, const char *format, va_list ap)
|
|
|
|
{
|
2012-12-18 20:44:25 -04:00
|
|
|
BufferPrinter buf(str, size);
|
2012-12-19 18:01:26 -04:00
|
|
|
print_vprintf(&buf, 0, format, ap);
|
2012-12-18 20:44:25 -04:00
|
|
|
return (int) buf._offs;
|
2012-12-18 20:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int AVRUtil::vsnprintf_P(char* str, size_t size, const prog_char_t *format,
|
|
|
|
va_list ap)
|
|
|
|
{
|
2012-12-18 20:44:25 -04:00
|
|
|
BufferPrinter buf(str, size);
|
2012-12-19 18:01:26 -04:00
|
|
|
print_vprintf(&buf, 1,(const char*) format, ap);
|
2012-12-18 20:44:25 -04:00
|
|
|
return (int) buf._offs;
|
2012-12-18 20:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|