libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
elum.h
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <propeller.h>
4 
5 
6 #ifndef LIBPROPELLER_ELUM_H_
7 #define LIBPROPELLER_ELUM_H_
8 
27 class Elum {
28 public:
29 
35  enum elumColor {
37  };
38 
39  enum patternType {
41  };
42 
50  Elum(int RedPin, int GreenPin, int ButtonPin) {
51  Start(RedPin, GreenPin, ButtonPin);
52  }
53 
54  Elum() {
55  }
56 
57  void Start(int RedPin, int GreenPin, int ButtonPin) {
58  b_mask = 1 << ButtonPin;
59  DIRA &= ~b_mask; //Set button to input
60 
61  pin_r = RedPin;
62  pin_g = GreenPin;
63 
64  DIRA |= (1 << pin_r);
65  DIRA |= (1 << pin_g);
66 
67  _clockfreq = CLKFREQ;
68  }
69 
70  inline ~Elum() {
71  Off();
72  }
73 
77  inline bool GetButton() {
78  return (INA & b_mask) == 0;
79  }
80 
81  inline void Slowclock(void) {
82  _clockfreq = 20000;
83  }
84 
85  void On(elumColor whichColor) {
86 
87  CTRA = (CTRB = 0);
88  if (whichColor == RED) {
89  OUTA |= (1 << pin_g);
90  OUTA &= ~(1 << pin_r);
91  } else {
92  OUTA &= ~(1 << pin_g);
93  OUTA |= (1 << pin_r);
94  }
95  }
96 
97  void Off(void) {
98  CTRA = (CTRB = 0);
99  OUTA &= ~(1 << pin_r);
100  OUTA &= ~(1 << pin_g);
101  }
102 
115  void Flash(int color, int period_ms, int flash_ms) {
116  CTRA = (CTRB = 0); //Stop counters while updating
117 
118  if (color == RED) {
119  OUTA |= 1 << pin_g; //Make second pin high
120  Pwm(pin_r, period_ms, flash_ms);
121  } else if (color == GREEN) {
122  OUTA |= 1 << pin_r; //Make second pin high
123  Pwm(pin_g, period_ms, flash_ms);
124 
125  }
126  }
127 
142  void Pattern(patternType pattern) {
143  Off();
144  switch (pattern) {
145  case kSingleSlow:
146  Pattern(5, 10, 0);
147  break;
148  case kSingle:
149  Pattern(20, 60, 0);
150  break;
151  case kSingleSyncopated:
152  Pattern(20, 60, 50);
153  break;
154  case kDouble:
155  Pattern(20, 100, 0);
156  break;
157  case kTriple:
158  Pattern(10, 50, 50);
159  break;
160  case kManyFast:
161  Pattern(20, 400, 0);
162  break;
163  case kJitterFast:
164  Pattern(300, 400, 0);
165  break;
166  default:
167  Pattern(0, 0, 0);
168  }
169  }
170 
176  void Fade(int frequency) {
177  //Source: http://forums.parallax.com/showthread.php/143483-Make-an-LED-pulse-(smoothly-go-on-and-off)-without-any-code-interaction?p=1160777#post1160777
178  Off();
179  CTRA = CTRB = 0; //Stop counters while updating.
180  FRQA = (((1000) << 2) * (0x40000000 / (CLKFREQ / 1000))) / 1000; // 1000 Hz base frequency.
181  CTRA = (0b00100000 << 23) + pin_r;
182  FRQB = (((10000 + frequency) << 2) * (0x40000000 / (CLKFREQ / 1000))) / 10000;
183  CTRB = (0b00100000 << 23) + pin_g;
184  }
185 
186 
187 private:
188  int pin_r;
189  int pin_g;
190  int _clockfreq;
191  unsigned int b_mask;
192 
193  int Fraction(int Y, int X, int B) {
194  int F = 0;
195  {
196  int _idx__0000;
197  _idx__0000 = B;
198  do {
199  Y = (Y << 1);
200  F = (F << 1);
201  if (Y >= X) {
202  Y = (Y - X);
203  (F++);
204  }
205  _idx__0000 = (_idx__0000 + -1);
206  } while (_idx__0000 >= 1);
207  }
208  return F;
209  }
210 
211  int Pwm(int Pin, int Period_ms, int Flash_ms) {
212  int Phsx, Frqx;
213  FRQA = (FRQB = 0);
214  Flash_ms = ((Period_ms / 2) - (Min__(Flash_ms, (Period_ms / 2))));
215  Phsx = Fraction(Flash_ms, Period_ms, 32);
216  Frqx = Fraction(1, ((_clockfreq / 1000) * Period_ms), 32);
217  PHSB = (PHSA + Phsx);
218  FRQA = (FRQB = Frqx);
219  _clockfreq = CLKFREQ;
220  OUTA &= ~(1 << Pin);
221  CTRA = (0x10000000 + Pin);
222  CTRB = (0x10000000 + Pin);
223  return Phsx;
224  }
225 
226  void Pattern(int tRed, int tGreen, int phsGreen) {
227  PHSB = (0x7FFFFFFF / 100 * phsGreen) << 1;
228  FRQA = tRed;
229  FRQB = tGreen;
230  CTRA = (0b00100000 << 23) + pin_r;
231  CTRB = (0b00100000 << 23) + pin_g;
232  }
233 
234 
235  //TODO(SRLM): Get rid of this min.
236  static inline int32_t Min__(int32_t a, int32_t b) {
237  return a < b ? a : b;
238  }
239 };
240 
241 
242 
243 #endif // SRLM_PROPGCC_ELUM_H_
244 
245 
246 
247 
248 
249 
250