Add .gitignore
[apps/agl-service-can-low-level.git] / openxc_example / generate_example.cpp
1 /* DO NOT MODIFY:  This source is generated by the scripts in the
2  * vi-firmware repository.
3  *
4  * Generated for v7.x of the OpenXC VI firmware.
5  */
6
7 #include "diagnostics.h"
8 #include "can/canread.h"
9 #include "can/canwrite.h"
10 #include "signals.h"
11 #include "obd2.h"
12 #include "util/log.h"
13 #include "config.h"
14 #include "shared_handlers.h"
15
16 namespace can = openxc::can;
17
18 using openxc::util::log::debug;
19 using openxc::pipeline::Pipeline;
20 using openxc::config::getConfiguration;
21 using openxc::can::read::booleanDecoder;
22 using openxc::can::read::stateDecoder;
23 using openxc::can::read::ignoreDecoder;
24 using openxc::diagnostics::obd2::handleObd2Pid;
25 using namespace openxc::signals::handlers;
26
27 #include "can/canread.h"
28
29 using openxc::can::read::publishNumericalMessage;
30
31 void handleSteeringWheelMessage(CanMessage* message,
32         CanSignal* signals, int signalCount, Pipeline* pipeline) {
33     publishNumericalMessage("latitude", 42.0, pipeline);
34 }
35
36 openxc_DynamicField handleInverted(CanSignal* signal, CanSignal* signals,
37         int signalCount, float value, bool* send) {
38     return openxc::payload::wrapNumber(value * -1);
39 }
40
41 void initializeMyStuff() { }
42
43 void initializeOtherStuff() { }
44
45 void myLooper() {
46     // this function will be called once each time through the main loop, after
47     // all CAN message processing has been completed
48 }
49
50 const int MESSAGE_SET_COUNT = 1;
51 CanMessageSet MESSAGE_SETS[MESSAGE_SET_COUNT] = {
52     { 0, "example", 2, 1, 5, 1 },
53 };
54
55 const int MAX_CAN_BUS_COUNT = 2;
56 CanBus CAN_BUSES[][MAX_CAN_BUS_COUNT] = {
57     { // message set: example
58         { speed: 500000,
59         address: 1,
60         maxMessageFrequency: 0,
61         rawWritable: false,
62         passthroughCanMessages: false,
63         bypassFilters: false,
64         loopback: false
65         },
66
67         { speed: 125000,
68         address: 2,
69         maxMessageFrequency: 0,
70         rawWritable: false,
71         passthroughCanMessages: false,
72         bypassFilters: false,
73         loopback: false
74         },
75
76     },
77 };
78
79 const int MAX_MESSAGE_COUNT = 1;
80 CanMessageDefinition CAN_MESSAGES[][MAX_MESSAGE_COUNT] = {
81     { // message set: example
82         { bus: &CAN_BUSES[0][0], id: 0x128, format: STANDARD, frequencyClock: {0.000000}, forceSendChanged: true}, // ECM_z_5D2
83     },
84 };
85
86 const int MAX_SIGNAL_STATES = 12;
87 const int MAX_SIGNALS_WITH_STATES_COUNT = 1;
88 const CanSignalState SIGNAL_STATES[][MAX_SIGNALS_WITH_STATES_COUNT][MAX_SIGNAL_STATES] = {
89     { // message set: example
90         { {value: 1, name: "FIRST"}, {value: 2, name: "SECOND"}, {value: 3, name: "THIRD"}, {value: 4, name: "FOURTH"}, {value: 5, name: "REVERSE"}, {value: 6, name: "NEUTRAL"}, { 0, NULL }, { 0, NULL }, { 0, NULL }, { 0, NULL }, { 0, NULL }, { 0, NULL }, },
91     },
92 };
93
94 const int MAX_SIGNAL_COUNT = 5;
95 CanSignal SIGNALS[][MAX_SIGNAL_COUNT] = {
96     { // message set: example
97         {message: &CAN_MESSAGES[0][0], genericName: "GearshiftPosition", bitPosition: 41, bitSize: 3, factor: 1.000000, offset: 0.000000, minValue: 0.000000, maxValue: 0.000000, frequencyClock: {0.000000}, sendSame: true, forceSendChanged: false, states: SIGNAL_STATES[0][0], stateCount: 6, writable: false, decoder: stateDecoder, encoder: NULL}, // GrshftPos
98         {message: &CAN_MESSAGES[0][0], genericName: "SteeringWheelAngle", bitPosition: 52, bitSize: 12, factor: 0.153920, offset: 0.000000, minValue: 0.000000, maxValue: 0.000000, frequencyClock: {0.000000}, sendSame: true, forceSendChanged: false, states: NULL, stateCount: 0, writable: false, decoder: handleUnsignedSteeringWheelAngle, encoder: NULL}, // StrAnglAct
99         {message: &CAN_MESSAGES[0][0], genericName: "engine_speed", bitPosition: 12, bitSize: 8, factor: 1.000000, offset: 0.000000, minValue: 0.000000, maxValue: 0.000000, frequencyClock: {15.000000}, sendSame: true, forceSendChanged: false, states: NULL, stateCount: 0, writable: false, decoder: NULL, encoder: NULL}, // EngSpd
100         {message: &CAN_MESSAGES[0][0], genericName: "steering_angle_sign", bitPosition: 52, bitSize: 12, factor: 1.000000, offset: 0.000000, minValue: 0.000000, maxValue: 0.000000, frequencyClock: {0.000000}, sendSame: true, forceSendChanged: false, states: NULL, stateCount: 0, writable: false, decoder: ignoreDecoder, encoder: NULL}, // StrAnglSign
101         {message: &CAN_MESSAGES[0][0], genericName: "steering_wheel_angle_error", bitPosition: 44, bitSize: 12, factor: 1.000000, offset: 0.000000, minValue: 0.000000, maxValue: 0.000000, frequencyClock: {0.000000}, sendSame: true, forceSendChanged: false, states: NULL, stateCount: 0, writable: false, decoder: ignoreDecoder, encoder: NULL}, // StrAnglErr
102     },
103 };
104
105 void openxc::signals::initialize(openxc::diagnostics::DiagnosticsManager* diagnosticsManager) {
106     switch(getConfiguration()->messageSetIndex) {
107     case 0: // message set: example
108         initializeMyStuff();
109         break;
110     }
111 }
112
113 void openxc::signals::loop() {
114     switch(getConfiguration()->messageSetIndex) {
115     case 0: // message set: example
116         myLooper();
117         break;
118     }
119 }
120
121 const int MAX_COMMAND_COUNT = 1;
122 CanCommand COMMANDS[][MAX_COMMAND_COUNT] = {
123     { // message set: example
124         { genericName: "turn_signal_status", handler: handleTurnSignalCommand },
125     },
126 };
127
128 void openxc::signals::decodeCanMessage(Pipeline* pipeline, CanBus* bus, CanMessage* message) {
129     switch(getConfiguration()->messageSetIndex) {
130     case 0: // message set: example
131         switch(bus->address) {
132         case 1:
133             switch (message->id) {
134             case 0x128: // ECM_z_5D2
135                 handleSteeringWheelMessage(message, SIGNALS[0], getSignalCount(), pipeline);
136                 can::read::translateSignal(&SIGNALS[0][0], message, SIGNALS[0], getSignalCount(), pipeline); // GrshftPos
137                 can::read::translateSignal(&SIGNALS[0][1], message, SIGNALS[0], getSignalCount(), pipeline); // StrAnglAct
138                 can::read::translateSignal(&SIGNALS[0][2], message, SIGNALS[0], getSignalCount(), pipeline); // EngSpd
139                 can::read::translateSignal(&SIGNALS[0][3], message, SIGNALS[0], getSignalCount(), pipeline); // StrAnglSign
140                 can::read::translateSignal(&SIGNALS[0][4], message, SIGNALS[0], getSignalCount(), pipeline); // StrAnglErr
141                 break;
142             }
143             break;
144         case 2:
145             switch (message->id) {
146             }
147             break;
148         }
149         break;
150     }
151 }
152
153
154 CanCommand* openxc::signals::getCommands() {
155     return COMMANDS[getActiveMessageSet()->index];
156 }
157
158 int openxc::signals::getCommandCount() {
159     return getActiveMessageSet()->commandCount;
160 }
161
162 CanMessageDefinition* openxc::signals::getMessages() {
163     return CAN_MESSAGES[getActiveMessageSet()->index];
164 }
165
166 int openxc::signals::getMessageCount() {
167     return getActiveMessageSet()->messageCount;
168 }
169
170 CanSignal* openxc::signals::getSignals() {
171     return SIGNALS[getActiveMessageSet()->index];
172 }
173
174 int openxc::signals::getSignalCount() {
175     return getActiveMessageSet()->signalCount;
176 }
177
178 CanBus* openxc::signals::getCanBuses() {
179     return CAN_BUSES[getActiveMessageSet()->index];
180 }
181
182 int openxc::signals::getCanBusCount() {
183     return getActiveMessageSet()->busCount;
184 }
185
186 CanMessageSet* openxc::signals::getActiveMessageSet() {
187     return &MESSAGE_SETS[getConfiguration()->messageSetIndex];
188 }
189
190 CanMessageSet* openxc::signals::getMessageSets() {
191     return MESSAGE_SETS;
192 }
193
194 int openxc::signals::getMessageSetCount() {
195     return MESSAGE_SET_COUNT;
196 }
197