2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2019-12-03 22:11:10 -08:00
|
|
|
* @file state_sequence.cpp
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* @date May 18, 2014
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2019-12-03 22:11:10 -08:00
|
|
|
#include "state_sequence.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "trigger_structure.h"
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
SingleChannelStateSequence::SingleChannelStateSequence() {
|
2015-07-10 06:01:56 -07:00
|
|
|
init(NULL);
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
SingleChannelStateSequence::SingleChannelStateSequence(pin_state_t *ps) {
|
2015-07-10 06:01:56 -07:00
|
|
|
init(ps);
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
void SingleChannelStateSequence::init(pin_state_t *pinStates) {
|
2015-07-10 06:01:56 -07:00
|
|
|
this->pinStates = pinStates;
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
pin_state_t SingleChannelStateSequence::getState(int switchIndex) const {
|
2019-04-03 09:16:27 -07:00
|
|
|
pin_state_t state = pinStates[switchIndex];
|
2019-04-03 09:39:01 -07:00
|
|
|
efiAssert(OBD_PCM_Processor_Fault, state == 0 || state == 1, "wave state get", TV_FALL);
|
2019-04-03 09:16:27 -07:00
|
|
|
return state;
|
2018-12-24 19:40:48 -08:00
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
void SingleChannelStateSequence::setState(int switchIndex, pin_state_t state) {
|
2019-04-03 09:16:27 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
MultiChannelStateSequence::MultiChannelStateSequence() {
|
2015-07-10 06:01:56 -07:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
MultiChannelStateSequence::MultiChannelStateSequence(float *switchTimes, SingleChannelStateSequence *waves) : MultiChannelStateSequence() {
|
2015-07-10 06:01:56 -07:00
|
|
|
init(switchTimes, waves);
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
void MultiChannelStateSequence::reset(void) {
|
2015-07-10 06:01:56 -07:00
|
|
|
waveCount = 0;
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
float MultiChannelStateSequence::getSwitchTime(const int index) const {
|
2015-07-10 06:01:56 -07:00
|
|
|
return switchTimes[index];
|
|
|
|
}
|
|
|
|
|
2021-11-10 16:47:27 -08:00
|
|
|
void MultiChannelStateSequence::checkSwitchTimes(const float scale) const {
|
|
|
|
if (switchTimes[phaseCount - 1] != 1) {
|
2020-05-08 22:49:17 -07:00
|
|
|
firmwareError(CUSTOM_ERR_WAVE_1, "last switch time has to be 1/%f not %.2f/%f", scale,
|
2021-11-10 16:47:27 -08:00
|
|
|
switchTimes[phaseCount - 1], scale * switchTimes[phaseCount - 1]);
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
}
|
2021-11-10 16:47:27 -08:00
|
|
|
for (int i = 0; i < phaseCount - 1; i++) {
|
2015-07-10 06:01:56 -07:00
|
|
|
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
|
|
|
|
2019-12-03 22:11:10 -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
|
|
|
}
|
|
|
|
|
2021-11-11 06:19:22 -08:00
|
|
|
void MultiChannelStateSequence::setChannelState(const int channelIndex, const int phaseIndex,
|
|
|
|
pin_state_t state) {
|
|
|
|
if (channelIndex >= waveCount) {
|
|
|
|
// todo: would be nice to get this asserting working
|
|
|
|
//firmwareError(OBD_PCM_Processor_Fault, "channel index %d/%d", channelIndex, waveCount);
|
|
|
|
}
|
|
|
|
channels[channelIndex].pinStates[phaseIndex] = state;
|
|
|
|
}
|
|
|
|
|
2018-12-08 13:38:44 -08:00
|
|
|
/**
|
|
|
|
* returns the index at which given value would need to be inserted into sorted array
|
|
|
|
*/
|
2021-11-10 16:47:27 -08:00
|
|
|
int MultiChannelStateSequence::findInsertionAngle(const float angle) const {
|
|
|
|
for (int i = phaseCount - 1; i >= 0; i--) {
|
2018-12-08 13:38:44 -08:00
|
|
|
if (angle > switchTimes[i])
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-10 16:47:27 -08:00
|
|
|
int MultiChannelStateSequence::findAngleMatch(const float angle) const {
|
|
|
|
for (int i = 0; i < phaseCount; i++) {
|
2018-12-08 13:38:44 -08:00
|
|
|
if (isSameF(switchTimes[i], angle))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return EFI_ERROR_CODE;
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:11:10 -08:00
|
|
|
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;
|
|
|
|
}
|