libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
leds.c
Go to the documentation of this file.
1 /*
2 Copyright (c) 2023 Eindhoven University of Technology
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22 #include <gpio.h>
23 #include <leds.h>
24 #include <log.h>
25 #include <pinmap.h>
26 #include <pwm.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN "leds"
32 
34 static led_mode mode = uninitialized;
35 
36 // LEDs are either on or off
37 void leds_init_onoff(void) {
38  if (mode == binary)
39  return;
40  if (mode != uninitialized) {
41  pynq_error("leds_init_onoff: mode=%d should be uninitialized\n", mode);
42  }
53  mode = binary;
54 }
55 
56 // can change the intensity of LEDs, the onoff parameters are then in the range
57 // 0..255
58 void green_leds_init_pwm(void) {
59  if (mode == pwm_green)
60  return;
61  if (mode != uninitialized) {
62  pynq_error("green_leds_init_pwm: mode=%d should be uninitialized\n", mode);
63  }
64  // initialize switchbox and routing PWM to LEDs
69  // initialize the PWM channels
70  pwm_init(PWM0, 256);
71  pwm_init(PWM1, 256);
72  pwm_init(PWM2, 256);
73  pwm_init(PWM3, 256);
74  mode = pwm_green;
75 }
76 
77 // can change the intensity of LEDs, the onoff parameters are then in the range
78 // 0..255
79 void color_leds_init_pwm(void) {
80  if (mode == pwm_color)
81  return;
82  if (mode != uninitialized) {
83  pynq_error("color_leds_init_pwm: mode=%d should be uninitialized\n", mode);
84  }
85  // initialize switchbox and routing PWM to LEDs
89  // initialize the PWM channels
90  pwm_init(PWM0, 256);
91  pwm_init(PWM1, 256);
92  pwm_init(PWM2, 256);
93  mode = pwm_color;
94 }
95 
96 void leds_destroy(void) {
97  // note that pynq_destroy will also reset all GPIO and switch off all LEDs
98  if (mode == binary) {
99  for (int i = 0; i < NUM_GREEN_LEDS; i++)
100  green_led_off(i);
101  }
102  if (mode == pwm_green || mode == pwm_color) {
103  green_led_off(0);
104  green_led_off(1);
105  green_led_off(2);
106  pwm_destroy(PWM0);
107  pwm_destroy(PWM1);
108  pwm_destroy(PWM2);
109  }
110  if (mode == pwm_green) {
111  green_led_off(3);
112  pwm_destroy(PWM3);
113  }
114  mode = uninitialized;
115 }
116 
117 void green_led_onoff(const int led, const int onoff) {
118  if (led < 0 || led >= NUM_GREEN_LEDS) {
119  pynq_error("green_led_onoff: invalid led=%d, must be 0..%d-1\n",
121  }
122  int oo = onoff;
123  switch (mode) {
124  case binary:
125  gpio_set_level(IO_LD0 + led,
126  (onoff == LED_OFF ? GPIO_LEVEL_LOW : GPIO_LEVEL_HIGH));
127  break;
128  case pwm_green:
129  case pwm_color:
130  if (onoff < 0) {
131  oo = 0;
132  } else {
133  if (onoff > 255) {
134  oo = 255;
135  }
136  }
137  pwm_set_duty_cycle(PWM0 + led, oo);
138  break;
139  default:
140  pynq_error("green_led_onoff: LEDs have not been initialized with "
141  "green_leds_init_pwm\n");
142  break;
143  }
144 }
145 
146 void green_led_on(const int led) { green_led_onoff(led, LED_ON); }
147 void green_led_off(const int led) { green_led_onoff(led, LED_OFF); }
148 void color_led_red_onoff(const int onoff) {
149  int oo = onoff;
150  switch (mode) {
151  case binary:
153  (onoff == LED_OFF ? GPIO_LEVEL_LOW : GPIO_LEVEL_HIGH));
154  break;
155  case pwm_green:
156  case pwm_color:
157  if (onoff < 0) {
158  oo = 0;
159  } else {
160  if (onoff > 255) {
161  oo = 255;
162  }
163  }
165  break;
166  default:
167  pynq_error("color_led_red_onoff: LEDs have not been initialized with "
168  "color_leds_init_pwm\n");
169  }
170 }
171 
172 void color_led_green_onoff(const int onoff) {
173  int oo = onoff;
174  switch (mode) {
175  case binary:
177  (onoff == LED_OFF ? GPIO_LEVEL_LOW : GPIO_LEVEL_HIGH));
178  break;
179  case pwm_color:
180  if (onoff < 0) {
181  oo = 0;
182  } else {
183  if (onoff > 255) {
184  oo = 255;
185  }
186  }
188  break;
189  default:
190  pynq_error("color_led_green_onoff: LEDs have not been initialized with "
191  "color_leds_init_pwm\n");
192  }
193 }
194 
195 void color_led_blue_onoff(const int onoff) {
196  int oo = onoff;
197  switch (mode) {
198  case binary:
200  (onoff == LED_OFF ? GPIO_LEVEL_LOW : GPIO_LEVEL_HIGH));
201  break;
202  case pwm_color:
203  if (onoff < 0) {
204  oo = 0;
205  } else {
206  if (onoff > 255) {
207  oo = 255;
208  }
209  }
211  break;
212  default:
213  pynq_error("color_led_blue_onoff: LEDs have not been initialized with "
214  "color_leds_init_pwm\n");
215  }
216 }
217 
218 void color_led_onoff(const int red_onoff, const int green_onoff,
219  const int blue_onoff) {
220  color_led_red_onoff(red_onoff);
221  color_led_green_onoff(green_onoff);
222  color_led_blue_onoff(blue_onoff);
223 }
224 
GPIO_DIR_OUTPUT
@ GPIO_DIR_OUTPUT
Definition: gpio.h:92
SWB_PWM1
@ SWB_PWM1
Definition: switchbox.h:98
color_leds_init_pwm
void color_leds_init_pwm(void)
Initialize the color LEDs for use with variable intensity. The LED intensity can range from 0....
Definition: leds.c:4
pynq_error
#define pynq_error(...)
Definition: log.h:118
switchbox_set_pin
void switchbox_set_pin(const pin_t pin_number, const uint8_t pin_type)
Definition: switchbox.c:4
pwm_color
@ pwm_color
Definition: leds.c:33
gpio_set_level
void gpio_set_level(const io_t pin, const gpio_level_t level)
Set the level of the output IO pin. If the pin is configured as input, this function does nothing.
Definition: gpio.c:7
LED_ON
#define LED_ON
Definition: leds.h:103
IO_LD4G
@ IO_LD4G
Definition: pinmap.h:108
IO_LD4R
@ IO_LD4R
Definition: pinmap.h:107
SWB_PWM0
@ SWB_PWM0
Definition: switchbox.h:96
IO_LD4B
@ IO_LD4B
The RGB adresses for IO_LD4 and IO_LD5.
Definition: pinmap.h:106
IO_LD1
@ IO_LD1
Definition: pinmap.h:92
pwm_destroy
void pwm_destroy(const int pwm)
Removes the instantiated shared memory system of the PWM channel.
Definition: pwm.c:4
pwm_set_duty_cycle
void pwm_set_duty_cycle(const int pwm, const uint32_t duty)
Sets the duty cycle for the specified PWM channel.
Definition: pwm.c:5
leds.h
green_led_off
void green_led_off(const int led)
Same as green_led_onoff(led, LED_OFF). Works in all modes.
Definition: leds.c:8
leds_destroy
void leds_destroy(void)
Definition: leds.c:5
color_led_blue_onoff
void color_led_blue_onoff(const int onoff)
Switches on/off the blue component of color LED 0.
Definition: leds.c:11
GPIO_LEVEL_LOW
@ GPIO_LEVEL_LOW
Definition: gpio.h:100
color_led_on
void color_led_on(void)
Set color LED 0 to white. Same as color_led_onoff(LED_ON, LED_ON, LED_ON).
Definition: leds.c:14
color_led_onoff
void color_led_onoff(const int red_onoff, const int green_onoff, const int blue_onoff)
Switches on/off the red/green/blue components of color LED 0.
Definition: leds.c:12
IO_LD5G
@ IO_LD5G
Definition: pinmap.h:112
color_led_green_onoff
void color_led_green_onoff(const int onoff)
Switches on/off the green component of color LED 0.
Definition: leds.c:10
_led_mode
_led_mode
Definition: leds.c:33
gpio_set_direction
void gpio_set_direction(const io_t pin, const gpio_direction_t direction)
Set the IO pin as in input or output.
Definition: gpio.c:5
PWM3
@ PWM3
Definition: pwm.h:47
IO_LD5B
@ IO_LD5B
Definition: pinmap.h:110
green_leds_init_pwm
void green_leds_init_pwm(void)
Initialize the green LEDs for use with variable intensity. The LED intensity can range from 0....
Definition: leds.c:3
binary
@ binary
Definition: leds.c:33
led_mode
enum _led_mode led_mode
SWB_PWM3
@ SWB_PWM3
Definition: switchbox.h:102
pwm_green
@ pwm_green
Definition: leds.c:33
green_led_onoff
void green_led_onoff(const int led, const int onoff)
Definition: leds.c:6
IO_LD5R
@ IO_LD5R
Definition: pinmap.h:111
PWM2
@ PWM2
Definition: pwm.h:47
pinmap.h
PWM0
@ PWM0
Definition: pwm.h:47
SWB_PWM2
@ SWB_PWM2
Definition: switchbox.h:100
log.h
NUM_GREEN_LEDS
@ NUM_GREEN_LEDS
Definition: leds.h:85
gpio.h
uninitialized
@ uninitialized
Definition: leds.c:33
LED_OFF
#define LED_OFF
Definition: leds.h:102
IO_LD2
@ IO_LD2
Definition: pinmap.h:93
color_led_off
void color_led_off(void)
Set color LED 0 to black. Same as color_led_onoff(LED_OFF, LED_OFF, LED_OFF).
Definition: leds.c:15
IO_LD3
@ IO_LD3
Definition: pinmap.h:94
pwm_init
void pwm_init(const int pwm, const uint32_t period)
Initializes the PWM channel with the specified period.
Definition: pwm.c:3
IO_LD0
@ IO_LD0
LED output pins.
Definition: pinmap.h:91
pwm.h
GPIO_LEVEL_HIGH
@ GPIO_LEVEL_HIGH
Definition: gpio.h:102
green_led_on
void green_led_on(const int led)
Same as green_led_onoff(led, LED_ON). Works in all modes.
Definition: leds.c:7
color_led_red_onoff
void color_led_red_onoff(const int onoff)
Switches on/off the red component of color LED 0.
Definition: leds.c:9
leds_init_onoff
void leds_init_onoff(void)
Initialize the green LEDs for on/off use.
Definition: leds.c:2
PWM1
@ PWM1
Definition: pwm.h:47