![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
ohne lcd
countClassMain.cpp@0:fe2cf615c330, 2015-11-20 (annotated)
- Committer:
- TFuchsbichler
- Date:
- Fri Nov 20 10:33:20 2015 +0000
- Revision:
- 0:fe2cf615c330
Nicht ganz;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TFuchsbichler | 0:fe2cf615c330 | 1 | #include "mbed.h" |
TFuchsbichler | 0:fe2cf615c330 | 2 | |
TFuchsbichler | 0:fe2cf615c330 | 3 | #define MAX_UINT8 255 |
TFuchsbichler | 0:fe2cf615c330 | 4 | |
TFuchsbichler | 0:fe2cf615c330 | 5 | // class for a counter (counting up (+1,+2..), down (-1,-2..), setValue, getValue..) http://www.cplusplus.com/doc/tutorial/classes/ |
TFuchsbichler | 0:fe2cf615c330 | 6 | class clCount { |
TFuchsbichler | 0:fe2cf615c330 | 7 | uint8_t count, maxCount; |
TFuchsbichler | 0:fe2cf615c330 | 8 | int8_t direction,dirNew; |
TFuchsbichler | 0:fe2cf615c330 | 9 | #define UP 1 |
TFuchsbichler | 0:fe2cf615c330 | 10 | #define DOWN -1 |
TFuchsbichler | 0:fe2cf615c330 | 11 | |
TFuchsbichler | 0:fe2cf615c330 | 12 | public: |
TFuchsbichler | 0:fe2cf615c330 | 13 | clCount(void); // constructor 1 |
TFuchsbichler | 0:fe2cf615c330 | 14 | clCount(uint8_t, uint8_t); // constructor 2 (overloaded) |
TFuchsbichler | 0:fe2cf615c330 | 15 | void countUp(void); |
TFuchsbichler | 0:fe2cf615c330 | 16 | void countDown(void); |
TFuchsbichler | 0:fe2cf615c330 | 17 | void countUp(uint8_t); |
TFuchsbichler | 0:fe2cf615c330 | 18 | void countDown(uint8_t); |
TFuchsbichler | 0:fe2cf615c330 | 19 | uint8_t getValue() { |
TFuchsbichler | 0:fe2cf615c330 | 20 | return count; |
TFuchsbichler | 0:fe2cf615c330 | 21 | } |
TFuchsbichler | 0:fe2cf615c330 | 22 | void setValue(uint8_t); |
TFuchsbichler | 0:fe2cf615c330 | 23 | void doCount(int8_t,uint8_t); |
TFuchsbichler | 0:fe2cf615c330 | 24 | void doCount(uint8_t); |
TFuchsbichler | 0:fe2cf615c330 | 25 | void changeDir(int8_t dirNew){direction = dirNew;} |
TFuchsbichler | 0:fe2cf615c330 | 26 | }; |
TFuchsbichler | 0:fe2cf615c330 | 27 | // mit dem scope-Operator Member-Functions ausserhalb der Klassendefinition definieren |
TFuchsbichler | 0:fe2cf615c330 | 28 | clCount::clCount(void) { // "::" .. is the scope operator! |
TFuchsbichler | 0:fe2cf615c330 | 29 | count = 0; |
TFuchsbichler | 0:fe2cf615c330 | 30 | maxCount = MAX_UINT8; |
TFuchsbichler | 0:fe2cf615c330 | 31 | } |
TFuchsbichler | 0:fe2cf615c330 | 32 | clCount::clCount(uint8_t start, uint8_t max) { |
TFuchsbichler | 0:fe2cf615c330 | 33 | count = start; |
TFuchsbichler | 0:fe2cf615c330 | 34 | maxCount = max; |
TFuchsbichler | 0:fe2cf615c330 | 35 | } |
TFuchsbichler | 0:fe2cf615c330 | 36 | void clCount::countUp(void) { |
TFuchsbichler | 0:fe2cf615c330 | 37 | if (count < maxCount) |
TFuchsbichler | 0:fe2cf615c330 | 38 | count++; |
TFuchsbichler | 0:fe2cf615c330 | 39 | else |
TFuchsbichler | 0:fe2cf615c330 | 40 | count = 0; |
TFuchsbichler | 0:fe2cf615c330 | 41 | } |
TFuchsbichler | 0:fe2cf615c330 | 42 | |
TFuchsbichler | 0:fe2cf615c330 | 43 | void clCount::countUp(uint8_t ink) { |
TFuchsbichler | 0:fe2cf615c330 | 44 | if (count < maxCount) |
TFuchsbichler | 0:fe2cf615c330 | 45 | count=count+ink; |
TFuchsbichler | 0:fe2cf615c330 | 46 | else |
TFuchsbichler | 0:fe2cf615c330 | 47 | count = 0; |
TFuchsbichler | 0:fe2cf615c330 | 48 | } |
TFuchsbichler | 0:fe2cf615c330 | 49 | |
TFuchsbichler | 0:fe2cf615c330 | 50 | void clCount::countDown(void) { |
TFuchsbichler | 0:fe2cf615c330 | 51 | if (count > 0) |
TFuchsbichler | 0:fe2cf615c330 | 52 | count--; |
TFuchsbichler | 0:fe2cf615c330 | 53 | else |
TFuchsbichler | 0:fe2cf615c330 | 54 | count = maxCount; |
TFuchsbichler | 0:fe2cf615c330 | 55 | } |
TFuchsbichler | 0:fe2cf615c330 | 56 | |
TFuchsbichler | 0:fe2cf615c330 | 57 | void clCount::countDown(uint8_t ink) { |
TFuchsbichler | 0:fe2cf615c330 | 58 | if (count > 0) |
TFuchsbichler | 0:fe2cf615c330 | 59 | count=count-ink; |
TFuchsbichler | 0:fe2cf615c330 | 60 | else |
TFuchsbichler | 0:fe2cf615c330 | 61 | count = maxCount; |
TFuchsbichler | 0:fe2cf615c330 | 62 | } |
TFuchsbichler | 0:fe2cf615c330 | 63 | |
TFuchsbichler | 0:fe2cf615c330 | 64 | void clCount::setValue(uint8_t set) { |
TFuchsbichler | 0:fe2cf615c330 | 65 | count=set; |
TFuchsbichler | 0:fe2cf615c330 | 66 | } |
TFuchsbichler | 0:fe2cf615c330 | 67 | |
TFuchsbichler | 0:fe2cf615c330 | 68 | void clCount::doCount(uint8_t ink) { |
TFuchsbichler | 0:fe2cf615c330 | 69 | if(direction==UP) |
TFuchsbichler | 0:fe2cf615c330 | 70 | { |
TFuchsbichler | 0:fe2cf615c330 | 71 | if (count < maxCount) |
TFuchsbichler | 0:fe2cf615c330 | 72 | count=count+ink; |
TFuchsbichler | 0:fe2cf615c330 | 73 | else |
TFuchsbichler | 0:fe2cf615c330 | 74 | count = 0; |
TFuchsbichler | 0:fe2cf615c330 | 75 | } |
TFuchsbichler | 0:fe2cf615c330 | 76 | if(direction==DOWN) |
TFuchsbichler | 0:fe2cf615c330 | 77 | { |
TFuchsbichler | 0:fe2cf615c330 | 78 | if (count > 0) |
TFuchsbichler | 0:fe2cf615c330 | 79 | count=count-ink; |
TFuchsbichler | 0:fe2cf615c330 | 80 | else |
TFuchsbichler | 0:fe2cf615c330 | 81 | count = maxCount; |
TFuchsbichler | 0:fe2cf615c330 | 82 | } |
TFuchsbichler | 0:fe2cf615c330 | 83 | } |
TFuchsbichler | 0:fe2cf615c330 | 84 | |
TFuchsbichler | 0:fe2cf615c330 | 85 | void clCount::doCount(int8_t dir, uint8_t ink) { |
TFuchsbichler | 0:fe2cf615c330 | 86 | if(dir==UP) |
TFuchsbichler | 0:fe2cf615c330 | 87 | { |
TFuchsbichler | 0:fe2cf615c330 | 88 | if (count < maxCount) |
TFuchsbichler | 0:fe2cf615c330 | 89 | count=count+ink; |
TFuchsbichler | 0:fe2cf615c330 | 90 | else |
TFuchsbichler | 0:fe2cf615c330 | 91 | count = 0; |
TFuchsbichler | 0:fe2cf615c330 | 92 | } |
TFuchsbichler | 0:fe2cf615c330 | 93 | if(dir==DOWN) |
TFuchsbichler | 0:fe2cf615c330 | 94 | { |
TFuchsbichler | 0:fe2cf615c330 | 95 | if (count > 0) |
TFuchsbichler | 0:fe2cf615c330 | 96 | count=count-ink; |
TFuchsbichler | 0:fe2cf615c330 | 97 | else |
TFuchsbichler | 0:fe2cf615c330 | 98 | count = maxCount; |
TFuchsbichler | 0:fe2cf615c330 | 99 | } |
TFuchsbichler | 0:fe2cf615c330 | 100 | } |
TFuchsbichler | 0:fe2cf615c330 | 101 | // global vars and objects |
TFuchsbichler | 0:fe2cf615c330 | 102 | BusOut boBlueLeds(LED1, LED2, LED3, LED4); |
TFuchsbichler | 0:fe2cf615c330 | 103 | uint8_t inkr; |
TFuchsbichler | 0:fe2cf615c330 | 104 | int richt; |
TFuchsbichler | 0:fe2cf615c330 | 105 | InterruptIn buttondown (p12); |
TFuchsbichler | 0:fe2cf615c330 | 106 | DigitalIn buttonleft (p13); |
TFuchsbichler | 0:fe2cf615c330 | 107 | DigitalIn buttonclick (p14); |
TFuchsbichler | 0:fe2cf615c330 | 108 | InterruptIn buttonup (p15); |
TFuchsbichler | 0:fe2cf615c330 | 109 | DigitalIn buttonright (p16); |
TFuchsbichler | 0:fe2cf615c330 | 110 | |
TFuchsbichler | 0:fe2cf615c330 | 111 | // functions |
TFuchsbichler | 0:fe2cf615c330 | 112 | void init() { |
TFuchsbichler | 0:fe2cf615c330 | 113 | |
TFuchsbichler | 0:fe2cf615c330 | 114 | } |
TFuchsbichler | 0:fe2cf615c330 | 115 | |
TFuchsbichler | 0:fe2cf615c330 | 116 | void down() |
TFuchsbichler | 0:fe2cf615c330 | 117 | { |
TFuchsbichler | 0:fe2cf615c330 | 118 | inkr--; |
TFuchsbichler | 0:fe2cf615c330 | 119 | if(inkr>0) |
TFuchsbichler | 0:fe2cf615c330 | 120 | inkr=-1; |
TFuchsbichler | 0:fe2cf615c330 | 121 | } |
TFuchsbichler | 0:fe2cf615c330 | 122 | |
TFuchsbichler | 0:fe2cf615c330 | 123 | void up() |
TFuchsbichler | 0:fe2cf615c330 | 124 | { |
TFuchsbichler | 0:fe2cf615c330 | 125 | inkr++; |
TFuchsbichler | 0:fe2cf615c330 | 126 | if(inkr<0) |
TFuchsbichler | 0:fe2cf615c330 | 127 | inkr=1; |
TFuchsbichler | 0:fe2cf615c330 | 128 | } |
TFuchsbichler | 0:fe2cf615c330 | 129 | |
TFuchsbichler | 0:fe2cf615c330 | 130 | |
TFuchsbichler | 0:fe2cf615c330 | 131 | int main() { |
TFuchsbichler | 0:fe2cf615c330 | 132 | // local vars and objects |
TFuchsbichler | 0:fe2cf615c330 | 133 | clCount counterForLeds(0, 15); // instantiate object; Objekt instanziieren (ein Exemplar der Klasse clCount erzeugen) |
TFuchsbichler | 0:fe2cf615c330 | 134 | inkr=1; |
TFuchsbichler | 0:fe2cf615c330 | 135 | richt=1; |
TFuchsbichler | 0:fe2cf615c330 | 136 | |
TFuchsbichler | 0:fe2cf615c330 | 137 | buttondown.rise(&down); |
TFuchsbichler | 0:fe2cf615c330 | 138 | buttonup.rise(&up); |
TFuchsbichler | 0:fe2cf615c330 | 139 | |
TFuchsbichler | 0:fe2cf615c330 | 140 | |
TFuchsbichler | 0:fe2cf615c330 | 141 | |
TFuchsbichler | 0:fe2cf615c330 | 142 | init(); |
TFuchsbichler | 0:fe2cf615c330 | 143 | while(1) { |
TFuchsbichler | 0:fe2cf615c330 | 144 | if(buttonright==1) |
TFuchsbichler | 0:fe2cf615c330 | 145 | counterForLeds.changeDir(UP); |
TFuchsbichler | 0:fe2cf615c330 | 146 | |
TFuchsbichler | 0:fe2cf615c330 | 147 | if(buttonleft==1) |
TFuchsbichler | 0:fe2cf615c330 | 148 | counterForLeds.changeDir(DOWN); |
TFuchsbichler | 0:fe2cf615c330 | 149 | |
TFuchsbichler | 0:fe2cf615c330 | 150 | if(buttonclick==1) |
TFuchsbichler | 0:fe2cf615c330 | 151 | counterForLeds.setValue(0); |
TFuchsbichler | 0:fe2cf615c330 | 152 | |
TFuchsbichler | 0:fe2cf615c330 | 153 | boBlueLeds.write(counterForLeds.getValue()); |
TFuchsbichler | 0:fe2cf615c330 | 154 | counterForLeds.doCount(inkr); |
TFuchsbichler | 0:fe2cf615c330 | 155 | wait(0.5); |
TFuchsbichler | 0:fe2cf615c330 | 156 | |
TFuchsbichler | 0:fe2cf615c330 | 157 | } |
TFuchsbichler | 0:fe2cf615c330 | 158 | |
TFuchsbichler | 0:fe2cf615c330 | 159 | } |
TFuchsbichler | 0:fe2cf615c330 | 160 | |
TFuchsbichler | 0:fe2cf615c330 | 161 | |
TFuchsbichler | 0:fe2cf615c330 | 162 | /* Erweiterungen: |
TFuchsbichler | 0:fe2cf615c330 | 163 | - Count-Wert setzen; |
TFuchsbichler | 0:fe2cf615c330 | 164 | - CountUp und CountDown überladen (.. bestimmten Wert rauf oder runter zählen) |
TFuchsbichler | 0:fe2cf615c330 | 165 | - Count mit Zaehlrichtung und Inkrement |
TFuchsbichler | 0:fe2cf615c330 | 166 | - Zählrichtung festlegen / aendern |
TFuchsbichler | 0:fe2cf615c330 | 167 | */ |