This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
SummerSchool2-Controller/306-controller_interface.X/middleware/can/can_sender.c

132 lines
3.0 KiB
C

/**
* @author Rémi Heredero
* @version 1.0.0
* @date August 2023
* @file can_sender.c
*/
#include "can_sender.h"
void CANSENDER_init(CANSENDER* me){
me->state = STCS_INIT;
me->sendingTime = 1;
me->wait.f = NULL;
me->send.f = NULL;
}
void CANSENDER_startBehaviour(CANSENDER* me){
POST(me, &CANSENDER_processEvent, evCSinit, 0, 0);
}
bool CANSENDER_processEvent(Event* ev) {
bool processed = false;
CANSENDER* me = (CANSENDER*)Event_getTarget(ev);
CANSENDER_STATES oldState = me->state;
evIDT evid = Event_getId(ev);
uint64_t data = Event_getData(ev);
switch (me->state) { // onState
case STCS_INIT:
if (ev->id == evCSinit) {
CANSENDER.state = STCS_WAIT;
}
break;
case STCS_WAIT:
if (ev->id == evCSsend) {
CANSENDER.state = STCS_SEND;
}
break;
case STCS_SEND:
if (ev->id == evCSdone) {
CANSENDER.state = STCS_WAIT;
}
uCAN_MSG canMsg;
canMsg.frame.data0 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data1 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data2 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data3 = (uint8_t) data;
data = data >> 8;
canMsg.frame.id = (uint32_t) data;
CAN_transmit(&canMsg);
CANSENDER_emitDone(me, 0);
break;
}
if(oldState != me->state){
switch (oldState) { // onExit
case STCS_INIT:
break;
case STCS_WAIT:
break;
case STCS_SEND:
break;
}
switch (me->state) { // onEntry
case STCS_INIT:
break;
case STCS_WAIT:
if (me->wait.f != NULL) {
me->wait.f(me->wait.p);
}
break;
case STCS_SEND:
if (me->send.f != NULL) {
me->send.f(me->send.p);
}
break;
}
processed = true;
}
return processed;
}
/*************
* Callbacks *
*************/
void CANSENDER_onWait(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p) {
me->wait.f = f;
me->wait.p = p;
}
void CANSENDER_onSend(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p) {
me->send.f = f;
me->send.p = p;
}
/************
* EMITTERS *
************/
void CANSENDER_emitSend(CANSENDER* me, uint16_t t) {
POST(me, &CANSENDER_processEvent, evCSsend, t, 0);
}
void CANSENDER_emitDone(CANSENDER* me, uint16_t t) {
POST(me, &CANSENDER_processEvent, evCSdone, t, 0);
}
CANSENDER_sendCanMsg(CANSENDER* me, uint8_t id, uint32_t data)
/***********
* SETTERS *
***********/
void CANSENDER_setSendingTime(CANSENDER* me, uint8_t v) {
me->sendingTime = v;
}