stmbl/shared/comps/wobl.c

61 lines
1.3 KiB
C
Raw Permalink Normal View History

2020-09-23 16:19:25 +00:00
#include "comps/wobl_comp.h"
2018-12-16 17:30:52 +00:00
#include "commands.h"
#include "hal.h"
#include "math.h"
#include "defines.h"
#include "angle.h"
HAL_COMP(wobl);
HAL_PIN(home_pos);
HAL_PIN(amp);
HAL_PIN(freq);
HAL_PIN(freq_diff);
HAL_PIN(duty);
HAL_PIN(en);
HAL_PIN(pos);
HAL_PIN(out);
2019-01-16 14:16:10 +00:00
HAL_PIN(out_freq);
2018-12-16 17:30:52 +00:00
struct wobl_ctx_t {
float ang;
float ang2;
};
2019-04-29 20:08:54 +00:00
static void frt_func(float period, void *ctx_ptr, hal_pin_inst_t *pin_ptr) {
2018-12-16 17:30:52 +00:00
struct wobl_ctx_t *ctx = (struct wobl_ctx_t *)ctx_ptr;
struct wobl_pin_ctx_t *pins = (struct wobl_pin_ctx_t *)pin_ptr;
if(PIN(en) <= 0.0){
ctx->ang = 0.0;
ctx->ang2 = 0.0;
}
else{
ctx->ang += period * PIN(freq) * 2.0 * M_PI;
ctx->ang = mod(ctx->ang);
ctx->ang2 += period * (PIN(freq) + PIN(freq_diff)) * 2.0 * M_PI;
ctx->ang2 = mod(ctx->ang2);
}
PIN(pos) = PIN(home_pos) + sinf(ctx->ang) * PIN(amp);
2019-01-16 14:16:10 +00:00
PIN(out_freq) = PIN(freq) + PIN(freq_diff);
2018-12-16 17:30:52 +00:00
PIN(out) = ((sinf(ctx->ang2) / 2 - 0.5 + PIN(duty)) > 0.0) ? 1.0 : 0.0;
}
hal_comp_t wobl_comp_struct = {
.name = "wobl",
.nrt = 0,
2018-12-25 12:37:46 +00:00
.rt = 0,
.frt = frt_func,
2018-12-16 17:30:52 +00:00
.nrt_init = 0,
.rt_start = 0,
.frt_start = 0,
.rt_stop = 0,
.frt_stop = 0,
.ctx_size = sizeof(struct wobl_ctx_t),
.pin_count = sizeof(struct wobl_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
};