OP-1156 fixes as suggested per review
[librepilot.git] / flight / modules / Example / examplemodevent.c
blobf38bf294c658ab6b40453471d9ebb438f632bb34
1 /**
2 ******************************************************************************
4 * @file examplemodevent.c
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief Example module to be used as a template for actual modules.
7 * Event callback version.
9 * @see The GNU Public License (GPL) Version 3
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /**
29 * Input objects: ExampleObject1, ExampleSettings
30 * Output object: ExampleObject2
32 * This module executes in response to ExampleObject1 updates. When the
33 * module is triggered it will update the data of ExampleObject2.
35 * No threads are used in this example.
37 * UAVObjects are automatically generated by the UAVObjectGenerator from
38 * the object definition XML file.
40 * Modules have no API, all communication to other modules is done through UAVObjects.
41 * However modules may use the API exposed by shared libraries.
42 * See the OpenPilot wiki for more details.
43 * http://www.openpilot.org/OpenPilot_Application_Architecture
47 #include "openpilot.h"
48 #include "examplemodevent.h"
49 #include "exampleobject1.h" // object the module will listen for updates (input)
50 #include "exampleobject2.h" // object the module will update (output)
51 #include "examplesettings.h" // object holding module settings (input)
53 // Private constants
55 // Private types
57 // Private variables
59 // Private functions
60 static void ObjectUpdatedCb(UAVObjEvent *ev);
62 /**
63 * Initialise the module, called on startup.
64 * \returns 0 on success or -1 if initialisation failed
66 int32_t ExampleModEventInitialize()
68 // Listen for ExampleObject1 updates, connect a callback function
69 ExampleObject1ConnectCallback(&ObjectUpdatedCb);
71 return 0;
74 /**
75 * This function is called each time ExampleObject1 is updated, this could be
76 * a local update or a remote update from the GCS. In this example the module
77 * does not have its own thread, the callbacks are executed from within the
78 * event thread. Because of that the callback execution time must be kept to
79 * a minimum, if the callback needs to wait for I/O or other long operations
80 * then a thread should be used instead (see ExampleModThread.c). This is
81 * important since all callbacks are executed from the same thread, so other
82 * queued events can not be executed until the currently active callback returns.
84 static void ObjectUpdatedCb(UAVObjEvent *ev)
86 ExampleSettingsData settings;
87 ExampleObject1Data data1;
88 ExampleObject2Data data2;
89 int32_t step;
91 // Make sure that the object update is for ExampleObject1
92 // This is redundant in this case since this callback will
93 // only be called for a single object, it is however possible
94 // to use the same callback for multiple object updates.
95 if (ev->obj == ExampleObject1Handle()) {
96 // Update settings with latest value
97 ExampleSettingsGet(&settings);
99 // Get the input object
100 ExampleObject1Get(&data1);
102 // Determine how to update the output object
103 if (settings.StepDirection == EXAMPLESETTINGS_STEPDIRECTION_UP) {
104 step = settings.StepSize;
105 } else {
106 step = -settings.StepSize;
109 // Update data
110 data2.Field1 = data1.Field1 + step;
111 data2.Field2 = data1.Field2 + step;
112 data2.Field3 = data1.Field3 + step;
113 data2.Field4[0] = data1.Field4[0] + step;
114 data2.Field4[1] = data1.Field4[1] + step;
116 // Update the ExampleObject2, after this function is called
117 // notifications to any other modules listening to that object
118 // will be sent and the GCS object will be updated through the
119 // telemetry link. All operations will take place asynchronously
120 // and the following call will return immediately.
121 ExampleObject2Set(&data2);