runtime update
This commit is contained in:
@@ -4,28 +4,37 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct printf_arg_t {
|
||||
typedef struct {
|
||||
const char* format;
|
||||
va_list va;
|
||||
va_list* va;
|
||||
int ret;
|
||||
};
|
||||
} printf_arg_t;
|
||||
|
||||
static void __printf_callback(int task_id, void* arg) {
|
||||
struct printf_arg_t* p_arg = (struct printf_arg_t*)(arg);
|
||||
p_arg->ret = vprintf(p_arg->format, p_arg->va);
|
||||
typedef struct {
|
||||
int value;
|
||||
int base;
|
||||
} putint_arg_t;
|
||||
|
||||
typedef struct {
|
||||
float value;
|
||||
int precision;
|
||||
} putfloat_arg_t;
|
||||
|
||||
static void __printf_cb(printf_arg_t* arg) {
|
||||
arg->ret = vprintf(arg->format, *arg->va);
|
||||
}
|
||||
|
||||
int vx_vprintf(const char* format, va_list va) {
|
||||
// need to execute 'vprintf' single-threaded due to potential thread-data dependency
|
||||
struct printf_arg_t arg;
|
||||
printf_arg_t arg;
|
||||
arg.format = format;
|
||||
arg.va = va;
|
||||
vx_serial(__printf_callback, &arg);
|
||||
arg.va = &va;
|
||||
vx_serial(__printf_cb, &arg);
|
||||
return arg.ret;
|
||||
}
|
||||
|
||||
@@ -38,6 +47,45 @@ int vx_printf(const char * format, ...) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void __putint_cb(const putint_arg_t* arg) {
|
||||
char tmp[33];
|
||||
float value = arg->value;
|
||||
int base = arg->base;
|
||||
itoa(value, tmp, base);
|
||||
for (int i = 0; i < 33; ++i) {
|
||||
int c = tmp[i];
|
||||
if (!c) break;
|
||||
vx_putchar(c);
|
||||
}
|
||||
}
|
||||
|
||||
void vx_putint(int value, int base) {
|
||||
putint_arg_t arg;
|
||||
arg.value = value;
|
||||
arg.base = base;
|
||||
vx_serial(__putint_cb, &arg);
|
||||
}
|
||||
|
||||
static void __putfloat_cb(const putfloat_arg_t* arg) {
|
||||
float value = arg->value;
|
||||
int precision = arg->precision;
|
||||
int ipart = (int)value;
|
||||
vx_putint(ipart, 10);
|
||||
if (precision != 0) {
|
||||
vx_putchar('.');
|
||||
float frac = value - (float)ipart;
|
||||
float fscaled = frac * pow(10, precision);
|
||||
vx_putint((int)fscaled, 10);
|
||||
}
|
||||
}
|
||||
|
||||
void vx_putfloat(float value, int precision) {
|
||||
putfloat_arg_t arg;
|
||||
arg.value = value;
|
||||
arg.precision = precision;
|
||||
vx_serial(__putfloat_cb, &arg);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user