view src/cs/services/vibr/vibr_process.c @ 297:8dfdf88d632f

BUZM SWE initial implementation
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 29 Mar 2022 03:45:41 +0000
parents e17bdedfbf2b
children
line wrap: on
line source

/*
 * In this module we are going to implement the main process functions
 * for VIBR.
 */

#include "vibr/vibr_env.h"
#include "vibr/vibr_func_i.h"
#include "rv/rv_general.h"
#include "rvf/rvf_api.h"
#include "rvm/rvm_use_id_list.h"
#include "main/sys_types.h"
#include "buzzer/vibrator.h"

/* duration of "on" and "off" phases of each vibration cycle */
#define	ON_PHASE_MS	500
#define	OFF_PHASE_MS	500

void vibr_process_start_req(struct vibr_start_msg *msg)
{
	vibr_env->vibr_level = msg->vibr_level;
	/* start the first pulse */
	HW_vibrator_on(vibr_env->vibr_level);
	vibr_env->on_state = TRUE;
	rvf_start_timer(VIBR_TIMER, RVF_MS_TO_TICKS(ON_PHASE_MS), FALSE);
	if (msg->num_pulses) {
		vibr_env->cont_mode = FALSE;
		vibr_env->remain_cycles = msg->num_pulses;
	} else
		vibr_env->cont_mode = TRUE;
}

void vibr_process_stop_req(struct vibr_stop_msg *msg)
{
	HW_vibrator_off();
	rvf_stop_timer(VIBR_TIMER);
}

static void on_phase_end(void)
{
	HW_vibrator_off();
	/* got more pulses? */
	if (!vibr_env->cont_mode) {
		vibr_env->remain_cycles--;
		if (!vibr_env->remain_cycles)
			return;		/* pulse train finished */
	}
	/* time the "off" phase before next pulse */
	vibr_env->on_state = FALSE;
	rvf_start_timer(VIBR_TIMER, RVF_MS_TO_TICKS(OFF_PHASE_MS), FALSE);
}

static void off_phase_end(void)
{
	/* start the next pulse */
	HW_vibrator_on(vibr_env->vibr_level);
	vibr_env->on_state = TRUE;
	rvf_start_timer(VIBR_TIMER, RVF_MS_TO_TICKS(ON_PHASE_MS), FALSE);
}

void vibr_handle_timer(void)
{
	if (vibr_env->on_state)
		on_phase_end();
	else
		off_phase_end();
}