Source code for the Curilights Controller. See http://www.saccade.com/writing/projects/CuriController/ for details.

Dependencies:   FatFileSystem mbed

This is the source code for the Curilights controller. This lets you interactively control a string of Curilights. It provides a simple click-wheel user interface for changing colors, brightness and behavior. It responds to movement and lighting.

Finished Controller

/media/uploads/isonno/nxp3872_controllerclose.jpg

System Block Diagram

/media/uploads/isonno/blockdiagram.png

Committer:
isonno
Date:
Mon Feb 11 05:04:18 2013 +0000
Revision:
4:cfef06d8bb96
Parent:
0:6da5625a6946
Minor changes to add backlight routines.  Not hooked up yet, shouldn't affect build operation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
isonno 0:6da5625a6946 1 /*
isonno 0:6da5625a6946 2 Copyright (c) 2011 Andy Kirkham
isonno 0:6da5625a6946 3
isonno 0:6da5625a6946 4 Permission is hereby granted, free of charge, to any person obtaining a copy
isonno 0:6da5625a6946 5 of this software and associated documentation files (the "Software"), to deal
isonno 0:6da5625a6946 6 in the Software without restriction, including without limitation the rights
isonno 0:6da5625a6946 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
isonno 0:6da5625a6946 8 copies of the Software, and to permit persons to whom the Software is
isonno 0:6da5625a6946 9 furnished to do so, subject to the following conditions:
isonno 0:6da5625a6946 10
isonno 0:6da5625a6946 11 The above copyright notice and this permission notice shall be included in
isonno 0:6da5625a6946 12 all copies or substantial portions of the Software.
isonno 0:6da5625a6946 13
isonno 0:6da5625a6946 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
isonno 0:6da5625a6946 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
isonno 0:6da5625a6946 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
isonno 0:6da5625a6946 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
isonno 0:6da5625a6946 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
isonno 0:6da5625a6946 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
isonno 0:6da5625a6946 20 THE SOFTWARE.
isonno 0:6da5625a6946 21 */
isonno 0:6da5625a6946 22
isonno 0:6da5625a6946 23 // Modified from Kirkham's version to accept a signed (vs. unsigned) argument
isonno 0:6da5625a6946 24 // J. Peterson, Jan-2011
isonno 0:6da5625a6946 25
isonno 0:6da5625a6946 26 #ifndef AJK_FPOINTER_H
isonno 0:6da5625a6946 27 #define AJK_FPOINTER_H
isonno 0:6da5625a6946 28
isonno 0:6da5625a6946 29 namespace AjK {
isonno 0:6da5625a6946 30
isonno 0:6da5625a6946 31 class FPointerDummy;
isonno 0:6da5625a6946 32
isonno 0:6da5625a6946 33 /** FPointer - Adds callbacks that take and return a 32bit uint32_t data type.
isonno 0:6da5625a6946 34 *
isonno 0:6da5625a6946 35 * The Mbed library supplies a callback using the FunctionPointer object as
isonno 0:6da5625a6946 36 * defined in FunctionPointer.h However, this callback system does not allow
isonno 0:6da5625a6946 37 * the caller to pass a value to the callback. Likewise, the callback itself
isonno 0:6da5625a6946 38 * cannot return a value.
isonno 0:6da5625a6946 39 *
isonno 0:6da5625a6946 40 * FPointer operates in the same way but allows the callback function to be
isonno 0:6da5625a6946 41 * passed one arg, a uint32_t value. Additionally, the callback can return
isonno 0:6da5625a6946 42 * a single uint32_t value. The reason for using uint32_t is that the Mbed
isonno 0:6da5625a6946 43 * and the microcontroller (LPC1768) have a natural data size of 32bits and
isonno 0:6da5625a6946 44 * this means we can use the uint32_t as a pointer. See example1.h for more
isonno 0:6da5625a6946 45 * information. This example passes an "int" by passing a pointer to that
isonno 0:6da5625a6946 46 * int as a 32bit value. Using this technique you can pass any value you like.
isonno 0:6da5625a6946 47 * All you have to do is pass a pointer to your value cast to (uint32_t). Your
isonno 0:6da5625a6946 48 * callback can the deference it to get the original value.
isonno 0:6da5625a6946 49 *
isonno 0:6da5625a6946 50 * example2.h shows how to do the same thing but demostrates how to specify
isonno 0:6da5625a6946 51 * the callback into a class object/method.
isonno 0:6da5625a6946 52 *
isonno 0:6da5625a6946 53 * Finally, example3.h shows how to pass multiple values. In this example we
isonno 0:6da5625a6946 54 * define a data structure and in the callback we pass a pointer to that
isonno 0:6da5625a6946 55 * data structure thus allowing the callback to again get the values.
isonno 0:6da5625a6946 56 *
isonno 0:6da5625a6946 57 * Note, when passing pointers to variables to the callback, if the callback
isonno 0:6da5625a6946 58 * function/method changes that variable's value then it will also change the
isonno 0:6da5625a6946 59 * value the caller sees. If C pointers are new to you, you are strongly
isonno 0:6da5625a6946 60 * advised to read up on the subject. It's pointers that often get beginners
isonno 0:6da5625a6946 61 * into trouble when mis-used.
isonno 0:6da5625a6946 62 *
isonno 0:6da5625a6946 63 * @see example1.h
isonno 0:6da5625a6946 64 * @see example2.h
isonno 0:6da5625a6946 65 * @see example3.h
isonno 0:6da5625a6946 66 * @see http://mbed.org/handbook/C-Data-Types
isonno 0:6da5625a6946 67 * @see http://mbed.org/projects/libraries/svn/mbed/trunk/FunctionPointer.h
isonno 0:6da5625a6946 68 */
isonno 0:6da5625a6946 69 class FPointer {
isonno 0:6da5625a6946 70
isonno 0:6da5625a6946 71 protected:
isonno 0:6da5625a6946 72
isonno 0:6da5625a6946 73 //! C callback function pointer.
isonno 0:6da5625a6946 74 int32_t (*c_callback)(int32_t);
isonno 0:6da5625a6946 75
isonno 0:6da5625a6946 76 //! C++ callback object/method pointer (the object part).
isonno 0:6da5625a6946 77 FPointerDummy *obj_callback;
isonno 0:6da5625a6946 78
isonno 0:6da5625a6946 79 //! C++ callback object/method pointer (the method part).
isonno 0:6da5625a6946 80 int32_t (FPointerDummy::*method_callback)(int32_t);
isonno 0:6da5625a6946 81
isonno 0:6da5625a6946 82 public:
isonno 0:6da5625a6946 83
isonno 0:6da5625a6946 84 /** Constructor
isonno 0:6da5625a6946 85 */
isonno 0:6da5625a6946 86 FPointer() {
isonno 0:6da5625a6946 87 c_callback = NULL;
isonno 0:6da5625a6946 88 obj_callback = NULL;
isonno 0:6da5625a6946 89 method_callback = NULL;
isonno 0:6da5625a6946 90 }
isonno 0:6da5625a6946 91
isonno 0:6da5625a6946 92 /** attach - Overloaded attachment function.
isonno 0:6da5625a6946 93 *
isonno 0:6da5625a6946 94 * Attach a C type function pointer as the callback.
isonno 0:6da5625a6946 95 *
isonno 0:6da5625a6946 96 * Note, the callback function prototype must be:-
isonno 0:6da5625a6946 97 * @code
isonno 0:6da5625a6946 98 * int32_t myCallbackFunction(int32_t);
isonno 0:6da5625a6946 99 * @endcode
isonno 0:6da5625a6946 100 * @param A C function pointer to call.
isonno 0:6da5625a6946 101 */
isonno 0:6da5625a6946 102 void attach(int32_t (*function)(int32_t) = 0) { c_callback = function; }
isonno 0:6da5625a6946 103
isonno 0:6da5625a6946 104 /** attach - Overloaded attachment function.
isonno 0:6da5625a6946 105 *
isonno 0:6da5625a6946 106 * Attach a C++ type object/method pointer as the callback.
isonno 0:6da5625a6946 107 *
isonno 0:6da5625a6946 108 * Note, the callback method prototype must be:-
isonno 0:6da5625a6946 109 * @code
isonno 0:6da5625a6946 110 * public:
isonno 0:6da5625a6946 111 * int32_t myCallbackFunction(int32_t);
isonno 0:6da5625a6946 112 * @endcode
isonno 0:6da5625a6946 113 * @param A C++ object pointer.
isonno 0:6da5625a6946 114 * @param A C++ method within the object to call.
isonno 0:6da5625a6946 115 */
isonno 0:6da5625a6946 116 template<class T>
isonno 0:6da5625a6946 117 void attach(T* item, int32_t (T::*method)(int32_t)) {
isonno 0:6da5625a6946 118 obj_callback = (FPointerDummy *)item;
isonno 0:6da5625a6946 119 method_callback = (int32_t (FPointerDummy::*)(int32_t))method;
isonno 0:6da5625a6946 120 }
isonno 0:6da5625a6946 121
isonno 0:6da5625a6946 122 /** call - Overloaded callback initiator.
isonno 0:6da5625a6946 123 *
isonno 0:6da5625a6946 124 * call the callback function.
isonno 0:6da5625a6946 125 *
isonno 0:6da5625a6946 126 * @param int32_t The value to pass to the callback.
isonno 0:6da5625a6946 127 * @return int32_t The value the callback returns.
isonno 0:6da5625a6946 128 */
isonno 0:6da5625a6946 129 int32_t call(int32_t arg) {
isonno 0:6da5625a6946 130 if (c_callback != NULL) {
isonno 0:6da5625a6946 131 return (*c_callback)(arg);
isonno 0:6da5625a6946 132 }
isonno 0:6da5625a6946 133 else {
isonno 0:6da5625a6946 134 if (obj_callback != NULL && method_callback != NULL) {
isonno 0:6da5625a6946 135 return (obj_callback->*method_callback)(arg);
isonno 0:6da5625a6946 136 }
isonno 0:6da5625a6946 137 }
isonno 0:6da5625a6946 138 return (int32_t)NULL;
isonno 0:6da5625a6946 139 }
isonno 0:6da5625a6946 140
isonno 0:6da5625a6946 141 /** call - Overloaded callback initiator.
isonno 0:6da5625a6946 142 *
isonno 0:6da5625a6946 143 * Call the callback function without passing an argument.
isonno 0:6da5625a6946 144 * The callback itself is passed NULL. Note, the callback
isonno 0:6da5625a6946 145 * prototype should still be <b>int32_t callback(int32_t)</b>.
isonno 0:6da5625a6946 146 *
isonno 0:6da5625a6946 147 * @return int32_t The value the callback returns.
isonno 0:6da5625a6946 148 */
isonno 0:6da5625a6946 149 int32_t call(void) {
isonno 0:6da5625a6946 150 if (c_callback != NULL) {
isonno 0:6da5625a6946 151 return (*c_callback)((int32_t)NULL);
isonno 0:6da5625a6946 152 }
isonno 0:6da5625a6946 153 else {
isonno 0:6da5625a6946 154 if (obj_callback != NULL && method_callback != NULL) {
isonno 0:6da5625a6946 155 return (obj_callback->*method_callback)((int32_t)NULL);
isonno 0:6da5625a6946 156 }
isonno 0:6da5625a6946 157 }
isonno 0:6da5625a6946 158 return (uint32_t)NULL;
isonno 0:6da5625a6946 159 }
isonno 0:6da5625a6946 160 };
isonno 0:6da5625a6946 161
isonno 0:6da5625a6946 162 }; // namespace AjK ends
isonno 0:6da5625a6946 163
isonno 0:6da5625a6946 164 using namespace AjK;
isonno 0:6da5625a6946 165
isonno 0:6da5625a6946 166 #endif