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
|
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <stddef.h>
#include "software_pwm.h"
#include "pins.h"
static struct link_pair {
struct pwm_event *next;
struct pwm_event *prev;
};
//if next is ever zero, node is assumed not part of list/disabled
static struct pwm_event {
struct link_pair active;
struct link_pair twins;
uint8_t time;
uint8_t pins;
bool enabled;
} events[SOFTPWM_PIN_COUNT + 1];
#define LIST_INSERT(new, prev, member) (\
list_insert(&(new.member), &(prev.member), offsetof(struct pwm_event, member)))
#define EVENT_FROM_MEMBER(member_ptr, member_offset) \
((struct pwm_event *)(char *)member_ptr - member_offset)
#define MEMBER_FROM_EVENT(event_ptr, member_offset) \
((struct link_pair *)(char *)event_
static void list_insert(struct link_pair *new_links, struct link_pair *prev_links, size_t parent_offset) {
new_links->next = prev_links->next;
new_links->prev = EVENT_FROM_MEMBER(prev_links, parent_offset);
((struct link_pair *)(char *)prev_links->next + parent_offset)->prev =
EVENT_FROM_MEMBER(new_links, parent_offset); //GYAAAAGH
prev_links->next = EVENT_FROM_MEMBER(prev_links, parent_offset);
}
static void list_remove(struct link_pair *event_link, size_t parent_offset) {
event_link->prev
}
void init_softpwm() {
//this one never moves
events[SOFTPWM_PIN_COUNT].time = 0;
events[SOFTPWM_PIN_COUNT].pins = 0;
events[SOFTPWM_PIN_COUNT].active.next = &events[SOFTPWM_PIN_COUNT];
events[SOFTPWM_PIN_COUNT].active.prev = &events[SOFTPWM_PIN_COUNT];
events[SOFTPWM_PIN_COUNT].enabled = true;
//TODO: move all to zero once init_softpwm tested
for(size_t event_i = 0; event_i < SOFTPWM_PIN_COUNT; event_i++) {
events[event_i].time = event_i;
events[event_i].pins = (1 << (event_i + 1)) - 1;
LIST_INSERT(events[event_i],
events[((int)event_i - 1) % (SOFTPWM_PIN_COUNT + 1)], active);
}
SOFTPWM_DDR = 0xff;
//use external 10mhz clock (we just have one laying around)
//then prescale by 1024. Check at different freqs for brightness
TCCR0A = 0b00000010;
TIMSK0 = 0b00000011;
OCR0A = events[SOFTPWM_PIN_COUNT].time;
TIFR0 = 0b00000010; //interupts enabled!
sei();
}
void softpwm_set(uint8_t pin, uint8_t duty) {
/**
struct pwm_event *event_unmoved = &events[pin]; //TODO describe if you want ig
struct pwm_event *this_event = &events[pin];
this_event->time = duty;
if(duty < events[pin].time) {
while(duty < event_unmoved->time) event_unmoved = event_unmoved->next;
}
else {
}
**/
}
/**
ISR(TIMER0_OVF_vect) {
}
**/
ISR(TIMER0_COMP_vect) {
static struct pwm_event *on_event = &events[SOFTPWM_PIN_COUNT];
SOFTPWM_PORT = on_event->pins;
on_event = on_event->next;
OCR0A = on_event->time;
}
|