rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
state_sequence.h
Go to the documentation of this file.
1/**
2 * @file state_sequence.h
3 *
4 * @date May 18, 2014
5 * @author Andrey Belomutskiy, (c) 2012-2020
6 */
7
8#pragma once
9
10#include <stdint.h>
11#include "rusefi_enums.h"
12#include <rusefi/expected.h>
13
14enum class TriggerValue : uint8_t {
15 FALL = 0,
16 RISE = 1
17};
18
19// see also 'HW_EVENT_TYPES'
26
27/**
28 * This layer has two primary usages:
29 * 1) 'simple' PWM generation is used to produce actuator square control wave
30 * 2) 'complex' PWM generation is used for trigger simulator.
31 * Some triggers like Nissan 360 slot optical wheel need a lot of points to describe the shape of the wave.
32 * Looks like 252 is explained by 60 tooth * 2 (number of fronts) * 2 (number of crank rotations within engine cycle)
33 */
34#ifndef PWM_PHASE_MAX_COUNT
35// as of April 2020, trigger which requires most array length is REMIX_66_2_2_2
36// we can probably reduce RAM usage if we have more custom logic of triggers with large number of tooth while
37// pretty easy logic. like we do not need to REALLY have an array to remember the shape of evenly spaces 360 or 60/2 trigger :)
38// todo https://github.com/rusefi/rusefi/issues/3003
39// as of 2025, stm32f4 is kind of on the way out - we are focusing on stm32*7 where we have double the RAM amount
40#define PWM_PHASE_MAX_COUNT 280
41#endif /* PWM_PHASE_MAX_COUNT */
42// todo: rename to TRIGGER_CHANNEL_COUNT?
43#define PWM_PHASE_MAX_WAVE_PER_PWM 2
44
46
47/**
48 * This class represents multi-channel logical signals with shared time axis
49 *
50 * This is a semi-abstract interface so that implementations can exist for either regularized
51 * patterns (60-2, etc) or completely arbitrary patterns stored in arrays.
52 */
54public:
55 /**
56 * values in the (0..1] range which refer to points within the period at at which pin state
57 * should be changed So, in the simplest case we turn pin off at 0.3 and turn it on at 1 -
58 * that would give us a 70% duty cycle PWM
59 */
60 virtual float getSwitchTime(int phaseIndex) const = 0;
61 virtual pin_state_t getChannelState(int channelIndex, int phaseIndex) const = 0;
62
63 // Make sure the switch times are in order and end at the very end.
64 void checkSwitchTimes(float scale) const;
65
66 // Find the exact angle, or unexpected if it doesn't exist
67 expected<int> findAngleMatch(float angle) const;
68
69 // returns the index at which given value would need to be inserted into sorted array
70 int findInsertionAngle(float angle) const;
71
72 uint16_t phaseCount = 0; // Number of timestamps
73 uint16_t waveCount = 0; // Number of waveforms
74};
75
76template<unsigned max_phase>
78public:
79 float getSwitchTime(int phaseIndex) const override {
80 return switchTimes[phaseIndex];
81 }
82
83 pin_state_t getChannelState(int channelIndex, int phaseIndex) const override {
84 if (channelIndex >= waveCount) {
85 // todo: would be nice to get this asserting working
86 //criticalError("channel index %d/%d", channelIndex, waveCount);
87 }
88 return ((waveForm[phaseIndex] >> channelIndex) & 1) ? TriggerValue::RISE : TriggerValue::FALL;
89 }
90
91 void reset() {
92 waveCount = 0;
93 }
94
95 void setSwitchTime(const int phaseIndex, const float value) {
96 switchTimes[phaseIndex] = value;
97 }
98
99 void setChannelState(const int channelIndex, const int phaseIndex, pin_state_t state) {
100 if (channelIndex >= waveCount) {
101 // todo: would be nice to get this asserting working
102 //criticalError("channel index %d/%d", channelIndex, waveCount);
103 }
104 uint8_t & ref = waveForm[phaseIndex];
105 ref = (ref & ~(1U << channelIndex)) | ((state == TriggerValue::RISE ? 1 : 0) << channelIndex);
106 }
107
108private:
109 float switchTimes[max_phase];
110 uint8_t waveForm[max_phase];
111};
112
void checkSwitchTimes(float scale) const
virtual float getSwitchTime(int phaseIndex) const =0
virtual pin_state_t getChannelState(int channelIndex, int phaseIndex) const =0
int findInsertionAngle(float angle) const
expected< int > findAngleMatch(float angle) const
pin_state_t getChannelState(int channelIndex, int phaseIndex) const override
float getSwitchTime(int phaseIndex) const override
void setSwitchTime(const int phaseIndex, const float value)
void setChannelState(const int channelIndex, const int phaseIndex, pin_state_t state)
Fundamental rusEFI enumerable types live here.
state("state", SensorCategory.SENSOR_INPUTS, FieldType.INT8, 1878, 1.0, -1.0, -1.0, "")
TriggerValue pin_state_t
trigger_event_e
@ SHAFT_SECONDARY_RISING
@ SHAFT_SECONDARY_FALLING
@ SHAFT_PRIMARY_FALLING
@ SHAFT_PRIMARY_RISING
TriggerValue