-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathledpwm.h
99 lines (78 loc) · 2.54 KB
/
ledpwm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright Ben XO https://github.com/ben-xo All rights reserved.
*/
// LED PWM using interrupts.
// Makes a huge assumption that you're using the entirety of PORTB for LEDs.
// Uses all three GPIOR registers for speed.
// GPIOR0 is used as a flags register. See gpio0.h for bit meanings
// GPIOR1 contains the PORTB value to write
// GPIOR2 contains the a rotating mask used to swap nibbles of GPIOR1 before writing
#ifndef _LEDPWM_H
#define _LEDPWM_H
#include <Arduino.h>
#include <DigitalIO.h>
#include "render.h"
#include "framestate.h"
#include "pwm_constants.h"
#include "gpio0.h"
#define ledpwm_status GPIOR0
#define portb_mask GPIOR1
#define portb_val GPIOR2
void setup_ledpwm();
void disable_ledpwm();
void enable_ledpwm();
extern DigitalPin<BEAT_PIN_1> beat_pin;
extern DigitalPin<BEAT_PIN_2> tempo_pin;
static void inline __attribute__((always_inline)) set_status_leds_and_mask_within_interrupt(uint8_t new_portb_val, uint8_t new_portb_mask)
{
portb_mask = new_portb_mask;
portb_val = new_portb_val;
}
static void inline __attribute__((always_inline)) set_status_leds_within_interrupt(uint8_t new_portb_val)
{
set_status_leds_and_mask_within_interrupt(new_portb_val, MASK_RESET_VAL);
}
static void inline __attribute__((always_inline)) clear_status_leds_within_interrupt()
{
portb_mask = MASK_RESET_VAL;
portb_val = 0;
}
static void inline __attribute__((always_inline)) set_status_leds_and_mask(uint8_t new_portb_val, uint8_t new_portb_mask)
{
cli();
set_status_leds_and_mask_within_interrupt(new_portb_val, new_portb_mask);
sei();
}
static void inline __attribute__((always_inline)) set_status_leds(uint8_t new_portb_val)
{
cli();
set_status_leds_within_interrupt(new_portb_val);
sei();
}
static void inline __attribute__((always_inline)) clear_status_leds()
{
cli();
clear_status_leds_within_interrupt();
sei();
}
static void inline __attribute__((always_inline)) set_status_leds_and_mask_rotate(uint8_t new_portb_val, uint8_t new_portb_mask)
{
cli();
set_status_leds_and_mask_within_interrupt(new_portb_val, new_portb_mask);
sei();
}
static void inline __attribute__((always_inline)) set_status_leds_front_buffer_only(uint8_t new_portb_val)
{
cli();
uint8_t temp = portb_val & 0xF0;
portb_val = temp | (new_portb_val & 0x0F);
sei();
}
static void inline __attribute__((always_inline)) set_status_leds_back_buffer_only(uint8_t new_portb_val)
{
cli();
uint8_t temp = portb_val & 0x0F;
portb_val = temp | (new_portb_val & 0xF0);
sei();
}
#endif /* _LEDPWM_H */