fome-fw/firmware/controllers/core/state_sequence.cpp

99 lines
2.8 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file state_sequence.cpp
2015-07-10 06:01:56 -07:00
*
* @date May 18, 2014
2018-01-20 17:55:31 -08:00
* @author Andrey Belomutskiy, (c) 2012-2018
2015-07-10 06:01:56 -07:00
*/
2018-09-16 19:26:57 -07:00
#include "global.h"
#include "state_sequence.h"
2015-07-10 06:01:56 -07:00
#include "trigger_structure.h"
SingleChannelStateSequence::SingleChannelStateSequence() {
2015-07-10 06:01:56 -07:00
init(NULL);
}
SingleChannelStateSequence::SingleChannelStateSequence(pin_state_t *ps) {
2015-07-10 06:01:56 -07:00
init(ps);
}
void SingleChannelStateSequence::init(pin_state_t *pinStates) {
2015-07-10 06:01:56 -07:00
this->pinStates = pinStates;
}
pin_state_t SingleChannelStateSequence::getState(int switchIndex) const {
pin_state_t state = pinStates[switchIndex];
efiAssert(OBD_PCM_Processor_Fault, state == 0 || state == 1, "wave state get", TV_FALL);
return state;
2018-12-24 19:40:48 -08:00
}
void SingleChannelStateSequence::setState(int switchIndex, pin_state_t state) {
efiAssertVoid(OBD_PCM_Processor_Fault, state == 0 || state == 1, "wave state set");
2019-02-02 21:50:45 -08:00
pinStates[switchIndex] = state;
2018-12-24 19:40:48 -08:00
}
MultiChannelStateSequence::MultiChannelStateSequence() {
2015-07-10 06:01:56 -07:00
reset();
}
MultiChannelStateSequence::MultiChannelStateSequence(float *switchTimes, SingleChannelStateSequence *waves) : MultiChannelStateSequence() {
2015-07-10 06:01:56 -07:00
init(switchTimes, waves);
}
void MultiChannelStateSequence::init(float *switchTimes, SingleChannelStateSequence *channels) {
2015-07-10 06:01:56 -07:00
this->switchTimes = switchTimes;
2018-12-25 07:13:00 -08:00
this->channels = channels;
2015-07-10 06:01:56 -07:00
}
void MultiChannelStateSequence::reset(void) {
2015-07-10 06:01:56 -07:00
waveCount = 0;
}
float MultiChannelStateSequence::getSwitchTime(const int index) const {
2015-07-10 06:01:56 -07:00
return switchTimes[index];
}
void MultiChannelStateSequence::checkSwitchTimes(const int size) {
2015-07-10 06:01:56 -07:00
if (switchTimes[size - 1] != 1) {
2018-01-23 09:05:14 -08:00
firmwareError(CUSTOM_ERR_WAVE_1, "last switch time has to be 1 not %.2f", switchTimes[size - 1]);
2015-07-10 06:01:56 -07:00
return;
}
for (int i = 0; i < size - 1; i++) {
if (switchTimes[i] >= switchTimes[i + 1]) {
2018-01-23 09:05:14 -08:00
firmwareError(CUSTOM_ERR_WAVE_2, "invalid switchTimes @%d: %.2f/%.2f", i, switchTimes[i], switchTimes[i + 1]);
2015-07-10 06:01:56 -07:00
}
}
}
2018-12-08 13:38:44 -08:00
pin_state_t MultiChannelStateSequence::getChannelState(const int channelIndex, const int phaseIndex) const {
2019-03-03 22:10:31 -08:00
if (channelIndex >= waveCount) {
// todo: would be nice to get this asserting working
//firmwareError(OBD_PCM_Processor_Fault, "channel index %d/%d", channelIndex, waveCount);
}
2018-12-25 07:13:00 -08:00
return channels[channelIndex].pinStates[phaseIndex];
2018-12-08 13:38:44 -08:00
}
/**
* returns the index at which given value would need to be inserted into sorted array
*/
int MultiChannelStateSequence::findInsertionAngle(const float angle, const int size) const {
2018-12-08 13:38:44 -08:00
for (int i = size - 1; i >= 0; i--) {
if (angle > switchTimes[i])
return i + 1;
}
return 0;
}
int MultiChannelStateSequence::findAngleMatch(const float angle, const int size) const {
2018-12-08 13:38:44 -08:00
for (int i = 0; i < size; i++) {
if (isSameF(switchTimes[i], angle))
return i;
}
return EFI_ERROR_CODE;
}
void MultiChannelStateSequence::setSwitchTime(const int index, const float value) {
2019-11-04 06:20:00 -08:00
efiAssertVoid(CUSTOM_ERR_PWM_SWITCH_ASSERT, switchTimes != NULL, "switchTimes");
2018-12-08 13:38:44 -08:00
switchTimes[index] = value;
}