libpynq (release 5EWC0-2023 version 0.2.6 of 2023-10-24 17:28)
Loading...
Searching...
No Matches
leds.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
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
34static led_mode mode = uninitialized;
35
36// LEDs are either on or off
55
56// can change the intensity of LEDs, the onoff parameters are then in the range
57// 0..255
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
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
96void 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);
109 }
110 if (mode == pwm_green) {
111 green_led_off(3);
113 }
114 mode = uninitialized;
115}
116
117void 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:
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
146void green_led_on(const int led) { green_led_onoff(led, LED_ON); }
147void green_led_off(const int led) { green_led_onoff(led, LED_OFF); }
148void 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
172void 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
195void 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
218void 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
void gpio_set_direction(const io_t pin, const gpio_direction_t dir)
Set the IO pin as in input or output.
Definition gpio.c:81
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:104
@ GPIO_DIR_OUTPUT
Definition gpio.h:92
@ GPIO_LEVEL_LOW
Definition gpio.h:100
@ GPIO_LEVEL_HIGH
Definition gpio.h:102
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:218
void leds_init_onoff(void)
Initialize the green LEDs for on/off use.
Definition leds.c:37
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:58
void green_led_off(const int led)
Same as green_led_onoff(led, LED_OFF). Works in all modes.
Definition leds.c:147
void color_led_red_onoff(const int onoff)
Switches on/off the red component of color LED 0.
Definition leds.c:148
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:226
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:225
void green_led_on(const int led)
Same as green_led_onoff(led, LED_ON). Works in all modes.
Definition leds.c:146
void color_led_blue_onoff(const int onoff)
Switches on/off the blue component of color LED 0.
Definition leds.c:195
void leds_destroy(void)
Definition leds.c:96
#define LED_OFF
Definition leds.h:102
void color_led_green_onoff(const int onoff)
Switches on/off the green component of color LED 0.
Definition leds.c:172
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:79
void green_led_onoff(const int led, const int onoff)
Definition leds.c:117
#define LED_ON
Definition leds.h:103
@ NUM_GREEN_LEDS
Definition leds.h:85
#define pynq_error(...)
Definition log.h:118
@ IO_LD0
LED output pins.
Definition pinmap.h:91
@ IO_LD4B
The RGB adresses for IO_LD4 and IO_LD5.
Definition pinmap.h:106
@ IO_LD5G
Definition pinmap.h:112
@ IO_LD3
Definition pinmap.h:94
@ IO_LD2
Definition pinmap.h:93
@ IO_LD4G
Definition pinmap.h:108
@ IO_LD1
Definition pinmap.h:92
@ IO_LD5R
Definition pinmap.h:111
@ IO_LD5B
Definition pinmap.h:110
@ IO_LD4R
Definition pinmap.h:107
void pwm_init(const int pwm, const uint32_t period)
Initializes the PWM channel with the specified period.
Definition pwm.c:61
void pwm_destroy(const int pwm)
Removes the instantiated shared memory system of the PWM channel.
Definition pwm.c:72
void pwm_set_duty_cycle(const int pwm, const uint32_t duty)
Sets the duty cycle for the specified PWM channel.
Definition pwm.c:93
@ PWM0
Definition pwm.h:47
@ PWM1
Definition pwm.h:47
@ PWM3
Definition pwm.h:47
@ PWM2
Definition pwm.h:47
void switchbox_set_pin(const io_t pin_number, const io_configuration_t io_type)
Set the type of a switch pin.
Definition switchbox.c:126
@ SWB_PWM0
Definition switchbox.h:96
@ SWB_PWM2
Definition switchbox.h:100
@ SWB_PWM3
Definition switchbox.h:102
@ SWB_PWM1
Definition switchbox.h:98
enum _led_mode led_mode
_led_mode
Definition leds.c:33
@ pwm_color
Definition leds.c:33
@ pwm_green
Definition leds.c:33
@ binary
Definition leds.c:33
@ uninitialized
Definition leds.c:33