Commit 769b26c7 authored by Torbjørn Viem Ness's avatar Torbjørn Viem Ness

Implementerte software-PWM for charlieplexing

parents
#define A 8
#define B 9
#define C 10
#define D 11
#define E 12
#define X 0 //dont show
// Microseconds
#define TOTAL_PERIOD 10000
#define MAX_DUTY 99
#define NUM_LEDS 20
// TODO: sjekk at disse periodene faktisk gir mening og ikke forsvinner i avrunding
#define LED_PERIOD (TOTAL_PERIOD/NUM_LEDS)
#define CYCLE_PERIOD (LED_PERIOD/MAX_DUTY)
// TODO: const PROGMEM
uint8_t leds_1d[NUM_LEDS][2] = {
{A, E}, {E, A}, {A, D}, {D, A},
{A, C}, {C, A}, {A, B}, {B, A},
{B, C}, {C, B}, {B, D}, {D, B},
{E, B}, {B, E}, {C, D}, {D, C},
{C, E}, {E, C}, {E, D}, {D, E}
};
void allIn(){
pinMode(A, INPUT);
pinMode(B, INPUT);
pinMode(C, INPUT);
pinMode(D, INPUT);
pinMode(E, INPUT);
}
uint8_t led_pattern[NUM_LEDS][MAX_DUTY] = {
// TODO: autogen and const PROGMEM
};
void init_patterns() {
for (uint8_t i = 0; i<NUM_LEDS;i++) {
for (uint8_t j = 0; j<MAX_DUTY;j++) {
led_pattern[i][j] = j;
}
}
}
void light( uint8_t pins[2] ){
allIn();
if(pins[0]==X){ return;}
pinMode( pins[0], OUTPUT );
digitalWrite( pins[0], HIGH );
pinMode( pins[1], OUTPUT );
digitalWrite( pins[1], LOW );
}
void setup(){
allIn();
init_patterns();
}
void pwm_process_led(uint8_t index, uint8_t duty) {
int onPeriod = (duty*CYCLE_PERIOD);
light(leds_1d[index]);
delayMicroseconds(onPeriod);
allIn();
delayMicroseconds(LED_PERIOD-onPeriod);
}
//bool rising = true;
//uint16_t tick = 0;
void loop(){
// for (int8_t led = 0;led < NUM_LEDS;led++) {
// pwm_process_led(led, led_pattern[led][tick]);
// }
// if (tick == 0) rising = true;
// else if (tick == MAX_DUTY) rising = false;
// if (rising)
// tick++;
// else
// tick--;
//
for (int16_t tick = 0;tick < MAX_DUTY;tick++) {
for (int8_t led = 0;led < NUM_LEDS;led++) {
pwm_process_led(led, led_pattern[led][tick]);
}
}
for (int16_t tick = MAX_DUTY-1; tick >= 0;tick--) {
for (int8_t led = 0;led < NUM_LEDS;led++) {
pwm_process_led(led, led_pattern[led][tick]);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment