file to handle settings created, and global variables better defined

This commit is contained in:
King Kévin 2013-10-17 13:29:01 +02:00
parent 47bf1cd944
commit abcc33cae5
7 changed files with 83 additions and 68 deletions

View File

@ -10,16 +10,12 @@ CFLAGS=-g -Wall -Werror -O3 -std=c99
# the target
DEVICE=atmega328p
F_CPU=18432000UL
# the port to flash
#PORT=/dev/ttyUSB0
# the flasher
#PROGRAMMER=buspirate
PROGRAMMER=usbtiny
# to flash
#AVRDUDE=avrdude -p $(DEVICE) -P $(PORT) -c $(PROGRAMMER)
FLASHER=avrdude -p $(DEVICE) -c $(PROGRAMMER)
# source files to compile
SRC = main.c uart.c ir_nec.c
SRC = main.c uart.c ir_nec.c settings.c
# object files.
OBJ = $(SRC:.c=.o)
# listing files.

View File

@ -24,6 +24,9 @@
#include "ir_nec.h"
const uint16_t MARKS[2]={9000,560};
const uint16_t SPACES[4]={4500,2250,1680,560};
void time2nec(uint16_t* burst, uint8_t pulses)
{

View File

@ -17,8 +17,8 @@
* More information at http://www.sbprojects.com/knowledge/ir/nec.php
*/
static const uint16_t MARKS[2]={9000,560}; /* mark duration in us [start,bit] */
static const uint16_t SPACES[4]={4500,2250,1680,560}; /* space duration in us [start,repeat,1,0] */
extern const uint16_t MARKS[2]; /* mark duration in us [start,bit] */
extern const uint16_t SPACES[4]; /* space duration in us [start,repeat,1,0] */
struct nec {
bool valid;

View File

@ -30,6 +30,7 @@
#include "main.h"
#include "uart.h"
#include "ir_nec.h"
#include "settings.h"
/* global variables */
#define INPUT_MAX 255 /* max length for user input string */
@ -38,10 +39,10 @@ volatile uint8_t input_i = 0; /* user input index */
volatile uint8_t pwr_ok; /* is power ok */
volatile uint8_t fan; /* fan signal state, to measure tachometer */
volatile uint8_t timer2_ovf = 0; /* to measure fan speed using timer 2 */
uint16_t TIMER2_PRESCALE[8] = {0,1,8,32,64,128,256,1024}; /* timer 2 CS2[2:0] values */
const uint16_t TIMER2_PRESCALE[8] = {0,1,8,32,64,128,256,1024}; /* timer 2 CS2[2:0] values */
volatile uint16_t tachometer = 0; /* the tachometer time (from timer) */
volatile uint8_t ir; /* IR signal state, to measure IR code */
uint16_t TIMER1_PRESCALE[8] = {0,1,8,64,256,1024,0,0}; /* timer 1 CS1[2:0] values */
const uint16_t TIMER1_PRESCALE[8] = {0,1,8,64,256,1024,0,0}; /* timer 1 CS1[2:0] values */
volatile uint16_t ir_tick; /* number of counter ticks per millisecond */
volatile uint8_t pulse = 0; /* pulse index within the burst */
#define PULSE_MAX 128 /* maximum number of pulses to save */
@ -49,15 +50,7 @@ uint16_t burst[PULSE_MAX]; /* pulse times forming a burst (from timer) */
/* channel variables */
#define LEVELS 10 /* the number of PWM levels */
uint8_t ch_tick = 0; /* the tick counter for the channel PWM */
#define OUTPUTS_1 5 /* the number of outputs for channel 1 */
#define OUTPUTS_2 5 /* the number of outputs for channel 2 */
volatile uint8_t* PORTS_1[OUTPUTS_1] = {&PORTC,&PORTC,&PORTC,&PORTC,&PORTC}; /* channel 1 output ports */
volatile uint8_t* PORTS_2[OUTPUTS_2] = {&PORTD,&PORTD,&PORTD,&PORTD,&PORTD}; /* channel 2 output ports */
uint8_t BITS_1[OUTPUTS_1] = {PC0,PC1,PC2,PC3,PC4}; /* channel 1 output bits */
uint8_t BITS_2[OUTPUTS_2] = {PD2,PD3,PD4,PD5,PD7}; /* channel 2 output bits */
uint8_t ch_1[OUTPUTS_1]; /* the level value for the channel 1 outputs */
uint8_t ch_2[OUTPUTS_2]; /* the level value for the channel 2 outputs */
volatile uint8_t ch_tick = 0; /* the tick counter for the channel PWM */
/* flags, set in the interrupts and handled in the main program */
volatile bool uart_flag = false; /* an incoming activity on the UART */
@ -132,7 +125,7 @@ ISR(TIMER0_COMPA_vect) { /* timer 0 OCR0A match interrupt vector */
pwm_flag = true;
}
static void ioinit(void)
void ioinit(void)
{
/* configure power */
DDRB |= (1<<nPS_ON); /* nPS_ON is output */
@ -223,64 +216,38 @@ int main(void)
ir_data.command = 0;
uint8_t ir_repeat = 0; /* number of times the IR data has been repeated */
/* initialize channel output PWM values */
for (uint8_t i=0; i<OUTPUTS_1; i++) {
ch_1[i] = 0;
}
for (uint8_t i=0; i<OUTPUTS_2; i++) {
ch_2[i] = 0;
}
uint8_t on_1[5] = {0,0,0,0,0}; /* times when to switch on the outputs for channel 1 */
uint8_t on_2[5] = {0,0,0,0,0}; /* times when to switch on the outputs for channel 2 */
uint8_t off_1[5] = {0,0,0,0,0}; /* times when to switch off the outputs for channel 1 */
uint8_t off_2[5] = {0,0,0,0,0}; /* times when to switch off the outputs for channel 2 */
uint8_t on[CHANNELS_1+CHANNELS_2]; /* times when to switch on the output on the channels */
uint8_t off[CHANNELS_1+CHANNELS_2]; /* times when to switch off the output on the channels */
channel_flag = true; /* calculate above values later */
puts("LED dimmer up & running");
verify_settings();
return 0;
while (true) {
/* calculated PWM values */
while (channel_flag) {
uint8_t start = 0;
for (uint8_t i=0; i<OUTPUTS_1; i++) {
on_1[i] = (start)%LEVELS;
off_1[i] = (on_1[i]+ch_1[i])%LEVELS;
start = (start+ch_1[i])%LEVELS;
}
start = 0;
for (uint8_t i=0; i<OUTPUTS_2; i++) {
on_2[i] = (start)%LEVELS;
off_2[i] = (on_2[i]+ch_2[i])%LEVELS;
start = (start+ch_2[i])%LEVELS;
for (uint8_t i=0; i<CHANNELS_1+CHANNELS_2; i++) {
on[i] = (start)%LEVELS;
off[i] = (on[i]+brightness[mode][i])%LEVELS;
start = (start+brightness[mode][i])%LEVELS;
}
channel_flag = false;
}
/* generate PWM for channel */
while (pwm_flag) {
if (pwr_ok) {
for (int i=0; i<OUTPUTS_1; i++) {
if (on_1[i]==ch_tick) {
if (on_1[i]!=off_1[i]) {
*(PORTS_1[i]) |= (1<<BITS_1[i]);
} else if (ch_1[i]==0) {
*(PORTS_1[i]) &= ~(1<<BITS_1[i]);
for (int i=0; i<CHANNELS_1+CHANNELS_2; i++) {
if (on[i]==ch_tick) {
if (on[i]!=off[i]) {
*(PORTS[i]) |= (1<<BITS[i]);
} else if (brightness[mode][i]==0) {
*(PORTS[i]) &= ~(1<<BITS[i]);
} else {
*(PORTS_1[i]) |= (1<<BITS_1[i]);
*(PORTS[i]) |= (1<<BITS[i]);
}
} else if (off_1[i]==ch_tick) {
*(PORTS_1[i]) &= ~(1<<BITS_1[i]);
}
}
for (int i=0; i<OUTPUTS_2; i++) {
if (on_2[i]==ch_tick) {
if (on_2[i]!=off_2[i]) {
*(PORTS_2[i]) |= (1<<BITS_2[i]);
} else if (ch_2[i]==0) {
*(PORTS_2[i]) &= ~(1<<BITS_2[i]);
} else {
*(PORTS_2[i]) |= (1<<BITS_2[i]);
}
} else if (off_2[i]==ch_tick) {
*(PORTS_2[i]) &= ~(1<<BITS_2[i]);
} else if (off[i]==ch_tick) {
*(PORTS[i]) &= ~(1<<BITS[i]);
}
}
PIND |= (1<<LED);
@ -337,7 +304,7 @@ int main(void)
return 0;
}
static void uart_action(char c)
void uart_action(char c)
{
switch (c) {
case 'l':
@ -367,7 +334,7 @@ static void uart_action(char c)
}
break;
case '1':
ch_1[0] = (ch_1[0]+1)%(LEVELS+1);
//ch_1[0] = (ch_1[0]+1)%(LEVELS+1);
channel_flag = true;
break;
case '2':
@ -483,7 +450,7 @@ static void uart_action(char c)
}
}
static void ir_action(uint8_t address, uint8_t command)
void ir_action(uint8_t address, uint8_t command)
{
if (0==address && 72==command) {
printf("switching power supply ");

View File

@ -34,6 +34,6 @@
#define CH2_4 PD5
#define CH2_5 PD7
static void ioinit(void);
static void uart_action(char c);
static void ir_action(uint8_t address, uint8_t command);
void ioinit(void);
void uart_action(char c);
void ir_action(uint8_t address, uint8_t command);

22
firmware/settings.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdint.h> /* Standard Integer Types */
#include <stdio.h> /* Standard IO facilities */
#include <stdlib.h> /* General utilities */
#include <stdbool.h> /* Boolean */
#include "settings.h"
#include <string.h> /* Strings */
#include "uart.h"
/* initialize variable */
volatile uint8_t* PORTS[CHANNELS_1+CHANNELS_2] = {&PORTC,&PORTC,&PORTC,&PORTC,&PORTC,&PORTD,&PORTD,&PORTD,&PORTD,&PORTD};
const uint8_t BITS[CHANNELS_1+CHANNELS_2] = {PC0,PC1,PC2,PC3,PC4};
uint8_t mode = 0;
uint8_t brightness[MODES][CHANNELS_1+CHANNELS_2];
void verify_settings(void)
{
uint8_t checksum = 0;
printf("brightness: %u\n",sizeof(brightness));
printf("checksum: %u\n",checksum);
}

27
firmware/settings.h Normal file
View File

@ -0,0 +1,27 @@
#include <avr/io.h> /* AVR device-specific IO definitions */
/* channel definitions */
#define CHANNELS_1 5 /* the number of outputs for channel 1 */
#define CHANNELS_2 5 /* the number of outputs for channel 2 */
extern volatile uint8_t* PORTS[CHANNELS_1+CHANNELS_2]; /* channel ports */
extern const uint8_t BITS[CHANNELS_1+CHANNELS_2]; /* channel bits */
/* mode settings */
#define MODES 3 /* the number of modes */
extern uint8_t mode; /* the current mode */ //TODO load the mode
extern uint8_t brightness[MODES][CHANNELS_1+CHANNELS_2]; /* the mode brightness settings for the channels */
/* IR settings */
enum IR_ACTIONS { /* the actions for the infrared remote control */
POWER = 0,
MODE,
BRIGHTNESS_UP,
BRIGHTNESS_DOWN,
CHANNEL_UP,
CHANNEL_DOWN,
IR_ACTION_END
};
uint8_t ir_keys[IR_ACTION_END][2];
/* function to load/save the settings */
void verify_settings(void);