PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
led_pwm.c
1 #include "simpletools.h"
2 #include "badgewxtools.h"
3 
4 
5 volatile int led_brightness[] = {0, 0};
6 static int led_pwm_cog_id;
7 
8 
9 void led_pwm() {
10  int shiftmask = ~(1 << 31);
11  int mask = (1 << LED_PIN);
12  int led_pin_state = 0;
13  while(1)
14  {
15  led_pin_state = !led_pin_state;
16 
17  int led_bit = led_brightness[led_pin_state] & 1;
18 
19  led_brightness[led_pin_state] = ((led_brightness[led_pin_state] >> 1) & shiftmask) | (led_brightness[led_pin_state] << 31);
20 
21  DIRA = DIRA & ~mask;
22 
23  // Toggle the state of the LED_PIN
24  OUTA ^= (-led_pin_state ^ OUTA) & mask;
25  // set_output(LED_PIN, led_pin_state);
26 
27  // Set the LED_PIN as input or output
28  set_direction(LED_PIN, led_bit);
29  // usleep(10);
30 
31  }
32 }
33 
34 void led_pwm_set(char side, char level) {
35 
36  int led_levels[] = {0b0,
37  0b1,
38  0b11,
39  0b111,
40  0b1111,
41  0b11111,
42  0b111111,
43  0b1111111,
44  0b111111111,
45  0b11111111111,
46  0b1111111111111,
47  0b1111111111111111,
48  0b1111111111111111111,
49  0b1111111111111111111111,
50  0b1111111111111111111111111,
51  0b1111111111111111111111111111,
52  0b11111111111111111111111111111111 };
53 
54  // Make sure the entries are not out of range (0-15)
55  level = level > 15 ? 15 : level;
56  level = level < 0 ? 0 : level;
57 
58  led_brightness[(int) side] = led_levels[(int) level];
59 }
60 
61 void led_pwm_start() {
62  led_pwm_cog_id = cog_run(led_pwm, 24);
63 }
64 
65 void led_pwm_stop() {
66  if(led_pwm_cog_id) {
67  cogstop(led_pwm_cog_id);
68  }
69 }
badgewxtools.h
This library provides convenient functions for a variety of Parallax eBadge operations.
OUTA
#define OUTA
Use to set output pin states when corresponding DIRA bits are 1.
Definition: propeller1.h:157
led_pwm_start
void led_pwm_start(void)
Start the PWM driver for the discrete LEDs. Uses a cog.
Definition: led_pwm.c:61
led_pwm_stop
void led_pwm_stop(void)
Stop the PWM driver for the discrete LEDs. Frees the cog used by the driver.
Definition: led_pwm.c:65
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
led_pwm_set
void led_pwm_set(char side, char level)
Set the brightness of the two discrete LEDs.
Definition: led_pwm.c:34
cogstop
#define cogstop(a)
Stop a COG.
Definition: propeller.h:100
set_direction
void set_direction(int pin, int direction)
Set an I/O pin to a given direction.
Definition: setDirection.c:19
cog_run
int * cog_run(void(*function)(void *par), int stacksize)
Run a function's code in the next available cog (processor).
Definition: cogrun.c:15
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161