4 errors

Dependencies:   KS0108_PCF8574 mbed

Committer:
GuiTwo
Date:
Wed Sep 05 07:22:44 2012 +0000
Revision:
1:4f46d81502aa
Parent:
0:936f1c020120
Child:
2:66e4ebaba5df
Original

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GuiTwo 0:936f1c020120 1 #include "mbed.h"
GuiTwo 0:936f1c020120 2 #include "menbed.h"
GuiTwo 0:936f1c020120 3
GuiTwo 0:936f1c020120 4 AnalogIn photocell(p15);
GuiTwo 0:936f1c020120 5 PwmOut led1(LED1);
GuiTwo 0:936f1c020120 6 PwmOut led2(LED2);
GuiTwo 0:936f1c020120 7 DigitalOut led3(LED3);
GuiTwo 0:936f1c020120 8 DigitalOut led4(LED4);
GuiTwo 0:936f1c020120 9
GuiTwo 0:936f1c020120 10 float photocellVoltage(void) {return 3.3 * photocell;}
GuiTwo 0:936f1c020120 11 void setLed1Pwm (float d) {led1 = d / 100.0;}
GuiTwo 0:936f1c020120 12 float getLed1Pwm (void) {return led1 * 100.0;}
GuiTwo 0:936f1c020120 13 void setLed2Pwm (float d) {led2 = d / 100.0;}
GuiTwo 0:936f1c020120 14 float getLed2Pwm (void) {return led2 * 100.0;}
GuiTwo 0:936f1c020120 15 void toggleLed3 () {led3 = led3 ^ 1;}
GuiTwo 0:936f1c020120 16 void lightLed4 () {led4 = 1;}
GuiTwo 0:936f1c020120 17 void extinguishLed4 () {led4 = 0;}
GuiTwo 0:936f1c020120 18
GuiTwo 0:936f1c020120 19 int main() {
GuiTwo 0:936f1c020120 20
GuiTwo 0:936f1c020120 21 // Declare all the submenus so that they can be referenced in the
GuiTwo 0:936f1c020120 22 // definitions of other menus
GuiTwo 0:936f1c020120 23 MenbedMenu *rootMenu;
GuiTwo 0:936f1c020120 24 MenbedMenu *measurementMenu;
GuiTwo 0:936f1c020120 25 MenbedMenu *controlMenu;
GuiTwo 0:936f1c020120 26 MenbedMenu *aboutMenu;
GuiTwo 0:936f1c020120 27
GuiTwo 0:936f1c020120 28 // Root menu--to create a menu, we first create an array of pointers to
GuiTwo 0:936f1c020120 29 // all the menu items that will populate the menu. The MenbedMenuItem
GuiTwo 0:936f1c020120 30 // constructor take five arguments: the function to be executed when the
GuiTwo 0:936f1c020120 31 // item is selected; the child menu to open when the item is selected;
GuiTwo 0:936f1c020120 32 // a boolean indicating whether the child menu is actually this menu's
GuiTwo 0:936f1c020120 33 // ancestor, (useful when creating "go to root menu" items); a parameter
GuiTwo 0:936f1c020120 34 // object that holds a value to be viewed or modified; and a text string
GuiTwo 0:936f1c020120 35 // indicating what the menu item should say. The text string may contain
GuiTwo 0:936f1c020120 36 // a printf-style specifier for a float that is bracketed by \t characters
GuiTwo 0:936f1c020120 37 // to offset it from the surrounding text. If the float specified is
GuiTwo 0:936f1c020120 38 // found, it will be replaced by the value of the parameter.
GuiTwo 0:936f1c020120 39 MenbedMenuItem *rootMenuItems[3] = {
GuiTwo 0:936f1c020120 40 new MenbedMenuItem (NULL, &measurementMenu, false, NULL, "Measurements"),
GuiTwo 0:936f1c020120 41 new MenbedMenuItem (NULL, &controlMenu, false, NULL, "Controls"),
GuiTwo 0:936f1c020120 42 new MenbedMenuItem (NULL, &aboutMenu, false, NULL, "About"),
GuiTwo 0:936f1c020120 43 };
GuiTwo 0:936f1c020120 44 // After we have the array of pointers to menu items, we pass the number of
GuiTwo 0:936f1c020120 45 // elements in the arry and the array itself to the MenbedMenu constructor.
GuiTwo 0:936f1c020120 46 rootMenu = new MenbedMenu (3, rootMenuItems);
GuiTwo 0:936f1c020120 47
GuiTwo 0:936f1c020120 48 // Measurements menu--the first item of the measurement menu displays the
GuiTwo 0:936f1c020120 49 // voltage and the resistor divider junction formed between a photocell and
GuiTwo 0:936f1c020120 50 // a fixed resistor. To print this voltage as part of a menu item, we
GuiTwo 0:936f1c020120 51 // create a MenbedMenuParam object that takes as arguments to its
GuiTwo 0:936f1c020120 52 // constructor: a pointer to a function that returns a float containing the
GuiTwo 0:936f1c020120 53 // value of the parameter; a pointer to a function that we would call to
GuiTwo 0:936f1c020120 54 // change the value of the parameter; a boolean (irrevelant here)
GuiTwo 0:936f1c020120 55 // indicating whether the parameter, if it were modifiable by the user,
GuiTwo 0:936f1c020120 56 // should be updated as the user is changing its value or whether its
GuiTwo 0:936f1c020120 57 // should only be updated after the user has confirmed the new value; a
GuiTwo 0:936f1c020120 58 // minimum value for the parameter; a maximum value; and a step size.
GuiTwo 0:936f1c020120 59 MenbedMenuParam photocellParam (photocellVoltage, NULL, false, 0.0, 0.0, 0.0);
GuiTwo 0:936f1c020120 60 // Having created the parameter, we pass it as the 4th argument of the
GuiTwo 0:936f1c020120 61 // MenbedMenuItem constructor. Note the \t-offset %.2f format specified
GuiTwo 0:936f1c020120 62 // in the menu item text. The menu system will call the photocellVoltage
GuiTwo 0:936f1c020120 63 // function specified in the parameter constructor above and then print the
GuiTwo 0:936f1c020120 64 // float that it returns in place of the %.2f in the menu text.
GuiTwo 0:936f1c020120 65 MenbedMenuItem *measurementMenuItems[2] = {
GuiTwo 0:936f1c020120 66 new MenbedMenuItem (NULL, NULL, false, &photocellParam, "Photocell: \t%.2f\tV"),
GuiTwo 0:936f1c020120 67 new MenbedMenuItem (NULL, &rootMenu, true, NULL, "Home") };
GuiTwo 0:936f1c020120 68 measurementMenu = new MenbedMenu (2, measurementMenuItems);
GuiTwo 0:936f1c020120 69
GuiTwo 0:936f1c020120 70 // Controls menu--We have modifiable parameters in the first and second
GuiTwo 0:936f1c020120 71 // meu items of the controls menu. Walking through the first parameter
GuiTwo 0:936f1c020120 72 // constructor, the getLed1Pwm function pointer points to a function that
GuiTwo 0:936f1c020120 73 // returns the current value of the PWM duty cycle. The setLed1Pwm
GuiTwo 0:936f1c020120 74 // function pointer points to a function which sets the PWM duty cycle.
GuiTwo 0:936f1c020120 75 // The boolean set to true indicates that as soon as the user makes a
GuiTwo 0:936f1c020120 76 // change to the parameter, the setLed1Pwm function will be called. The
GuiTwo 0:936f1c020120 77 // menu system will not wait for the user to confirm his change before
GuiTwo 0:936f1c020120 78 // making the call. The 0.0 and 100.0 represent the minimum and
GuiTwo 0:936f1c020120 79 // maximum PWM values. The final 1.0 paramter is the step size when
GuiTwo 0:936f1c020120 80 // changing the PWM duty cycle.
GuiTwo 0:936f1c020120 81 MenbedMenuParam pwmParam1 (getLed1Pwm, setLed1Pwm, true, 0.0, 100.0, 1.0);
GuiTwo 0:936f1c020120 82 // The only different in this parameter from the one above is that the PWM
GuiTwo 0:936f1c020120 83 // duty cycle is not updated (setLed2Pwm is not called) until the user
GuiTwo 0:936f1c020120 84 // confirms the new duty cycle. If the user cancels his modifications
GuiTwo 0:936f1c020120 85 // (either by pushing the cancel button or pushing and holding the select
GuiTwo 0:936f1c020120 86 // button) setLed2PWM is never called.
GuiTwo 0:936f1c020120 87 MenbedMenuParam pwmParam2 (getLed2Pwm, setLed2Pwm, false, 0.0, 100.0, 1.0);
GuiTwo 0:936f1c020120 88 // The third, fourth, and fifth items of the control menu demonstrate
GuiTwo 0:936f1c020120 89 // functions when a menu item is selected. The ability to call a function
GuiTwo 0:936f1c020120 90 // can be combined with either displaying a submenu or editing a parameter.
GuiTwo 0:936f1c020120 91 MenbedMenuItem *controlMenuItems[6] = {
GuiTwo 0:936f1c020120 92 new MenbedMenuItem (NULL, NULL, false, &pwmParam1, "LED1 PWM: \t%.0f\t%"),
GuiTwo 0:936f1c020120 93 new MenbedMenuItem (NULL, NULL, false, &pwmParam2, "LED2 PWM: \t%.0f\t%"),
GuiTwo 0:936f1c020120 94 new MenbedMenuItem (toggleLed3, NULL, false, NULL, "Toggle LED3"),
GuiTwo 0:936f1c020120 95 new MenbedMenuItem (lightLed4, NULL, false, NULL, "Light LED4"),
GuiTwo 0:936f1c020120 96 new MenbedMenuItem (extinguishLed4, NULL, false, NULL, "Extinguish LED4"),
GuiTwo 0:936f1c020120 97 new MenbedMenuItem (NULL, &rootMenu, true, NULL, "Home") };
GuiTwo 0:936f1c020120 98 controlMenu = new MenbedMenu (6, controlMenuItems);
GuiTwo 0:936f1c020120 99
GuiTwo 0:936f1c020120 100 // About menu--there's nothing fancy here, but the lack of a "Home" item
GuiTwo 0:936f1c020120 101 // forces the user to employ the cancel button, either directly or by
GuiTwo 0:936f1c020120 102 // pushing and hold the select button, to return to the root menu.
GuiTwo 0:936f1c020120 103 MenbedMenuItem *aboutMenuItems[3] = {
GuiTwo 0:936f1c020120 104 new MenbedMenuItem (NULL, NULL, false, NULL, " NXP3915"),
GuiTwo 0:936f1c020120 105 new MenbedMenuItem (NULL, NULL, false, NULL, " Copyright 2011"),
GuiTwo 0:936f1c020120 106 new MenbedMenuItem (NULL, NULL, false, NULL, " xxxxxxxx@xxx.xxx") };
GuiTwo 0:936f1c020120 107 aboutMenu = new MenbedMenu (3, aboutMenuItems);
GuiTwo 0:936f1c020120 108
GuiTwo 0:936f1c020120 109 // Having created the menus, menu items, and item parameters, we are ready
GuiTwo 0:936f1c020120 110 // to instantiate the display. MenbedDisplayHD44780 extends MenbedDisplay
GuiTwo 0:936f1c020120 111 // and implements the all-important writeLine function allong with some
GuiTwo 0:936f1c020120 112 // other less important functions. The pins we pass to the constructor
GuiTwo 0:936f1c020120 113 // specify the physical interface connection to the LCD.
GuiTwo 0:936f1c020120 114 MenbedDisplayHD44780 *hd44780Lcd = new MenbedDisplayHD44780
GuiTwo 0:936f1c020120 115 (p25, p26, p27, p28, p29, p30, MenbedDisplayHD44780::LCD20x4);
GuiTwo 0:936f1c020120 116
GuiTwo 0:936f1c020120 117 // Now, we have the menu objects and the display object. All we have to do
GuiTwo 0:936f1c020120 118 // is create the menbed menu system. The number of buttons used by the
GuiTwo 0:936f1c020120 119 // menu system is specified by the number of pins passed to the Menbed
GuiTwo 0:936f1c020120 120 // constructor. The pin order is select, down, up, cancel. With
GuiTwo 0:936f1c020120 121 // constructor overloading, we can opt out of using the cancel and up
GuiTwo 0:936f1c020120 122 // buttons.
GuiTwo 0:936f1c020120 123
GuiTwo 0:936f1c020120 124 /* Four buttons (select, down, up, cancel ) */
GuiTwo 1:4f46d81502aa 125 Menbed menbed(p22, p24, p23, p21,
GuiTwo 1:4f46d81502aa 126 rootMenu,
GuiTwo 1:4f46d81502aa 127 hd44780Lcd);
GuiTwo 0:936f1c020120 128
GuiTwo 0:936f1c020120 129
GuiTwo 0:936f1c020120 130 /* Three buttons (select, down, up) */
GuiTwo 1:4f46d81502aa 131 /*
GuiTwo 0:936f1c020120 132 Menbed menbed(p22, p24, p23,
GuiTwo 0:936f1c020120 133 &rootMenu,
GuiTwo 0:936f1c020120 134 hd44780Lcd);
GuiTwo 1:4f46d81502aa 135 */
GuiTwo 0:936f1c020120 136
GuiTwo 0:936f1c020120 137 /* Two buttons (select, down) */
GuiTwo 0:936f1c020120 138 /*
GuiTwo 0:936f1c020120 139 Menbed menbed(p22, p24,
GuiTwo 0:936f1c020120 140 &rootMenu,
GuiTwo 0:936f1c020120 141 hd44780Lcd);
GuiTwo 0:936f1c020120 142 */
GuiTwo 0:936f1c020120 143
GuiTwo 0:936f1c020120 144 // The menu system runs in the background using Ticker objects. The user
GuiTwo 0:936f1c020120 145 // can now run whatever application code he pleases.
GuiTwo 0:936f1c020120 146 while(1) {}
GuiTwo 0:936f1c020120 147 }