Reorganisation du code

This commit is contained in:
arnaud.houdelette 2019-10-22 11:56:33 +02:00
parent 89f9ffae7e
commit 2e8904e8b0
10 changed files with 263 additions and 52 deletions

View file

@ -55,7 +55,7 @@ PORT = /dev/serial/by-id/usb-Arduino_LLC_Arduino_Micro-if00
TARGET = pyrorf
SRC = main.c lcd.c spi.c rf24.c Hamming.c HammingCalculateParitySmallAndFast.c tools.c
SRC = main.c lcd.c spi.c rf24.c Hamming.c HammingCalculateParitySmallAndFast.c tools.c radio.c analog.c ui.c
CXXSRC =
ASRC = i2cmaster.S
MCU = atmega32a

58
analog.c Normal file
View file

@ -0,0 +1,58 @@
#include "analog.h"
#include "status.h"
#include <avr/io.h>
uint8_t analog_cpt=0;
#define BATT 9
#define WAIT 10
uint8_t st = WAIT;
void start(){
//setup adc
ADCSRA = _BV(ADEN)|_BV(ADPS2)|_BV(ADPS1); // ADC Clock : 1/64 (125KHz)
ADMUX = _BV(REFS1)|_BV(REFS0); // Reference 2.56V
st=0;
ADCSRA |= _BV(ADSC); // start first conversion
}
void stop(){
ADCSRA=0;
}
void analog_run()
{
if(st==WAIT){
if (analog_cpt)
return;
start();
return;
}
if(!( ADCSRA & _BV(ADSC) )) // ADC still running
return;
if(st >= BATT){
uint16_t battval= ADC;
//TODO => lowbatt detection;
st=WAIT;
stop();
return;
}
lines[st] = ADC;
st++;
if(st<=7){
ADMUX = _BV(REFS1)|_BV(REFS0)| st;
ADCSRA |= _BV(ADSC);
return;
}
if(st==BATT){
ADMUX = _BV(REFS0)| 0b11110; // mesure la ref 1.22 / VCC
ADCSRA |= _BV(ADSC);
return;
}
}

11
analog.h Normal file
View file

@ -0,0 +1,11 @@
#include <stdint.h>
#include <stdlib.h>
#ifndef ANALOG_h
#define ANALOG_h
extern uint8_t analog_cpt;
void analog_run();
#endif

115
main.c
View file

@ -10,38 +10,44 @@
#include <stdbool.h>
#include <stdio.h>
#include "defines.h"
#include "lcd.h"
#include "spi.h"
/*#include "spi.h"
#include "rf24.h"
#include "Hamming.h"
#include "tools.h"
*/
#include "status.h"
#include "analog.h"
#include "radio.h"
#include "ui.h"
int main (void);
void setup (void);
void loop (void);
void _lcd();
void _radio();
void master_loop (void);
uint8_t radiobuffer[32];
uint8_t patch[16];
uint16_t lines[16];
bool TIR_ON=0;
volatile uint8_t skip_sleep;
volatile uint8_t t_lcd=0,t_radio=0;
uint8_t p=0;
int main(void)
{
setup();
while (1)
loop();
master_loop();
}
void setup(void)
{
#if defined(USBCON)
USBCON=0;
#endif
// Setup ports
@ -57,52 +63,62 @@ void setup(void)
// Port D => Output : BATT_ENABLE (PD6), pullup on (PD2-4)
PORTD= _BV(PD2)|_BV(PD3)|_BV(PD4);
lcd_init(20,4);
lcd_backlight(1);
lcd_clear();
lcd_home();
lcd_display();
lcd_setCursor(1,1);
lcdprint("** Pyro RF ! **");
spi_init_master();
_delay_ms(500);
lcd_setCursor(3,2);
lcdprint("Init");
GICR|=_BV(INT2);
// Timer1 => 5ms
#if F_CPU == 8000000UL
#define TIMERLOOP 5000
#else
#define TIMERLOOP 10000
#endif
DDRD= _BV(PD6);
#if defined(TIMSK1)
// Timer1 => 1 tick toutes les 5ms
TCCR1A = 0;
TCNT1=0;
OCR1A = TIMERLOOP;
TIMSK1 = _BV(OCIE1A);
TCCR1B = _BV(WGM12)|_BV(CS11);
#else
TCCR1A = 0;
TCNT1=0;
OCR1A = TIMERLOOP;
TIMSK = _BV(OCIE1A);
TCCR1B = _BV(WGM12)|_BV(CS11);
#endif
OCR1A = 5000; // Compare A => 5000
TIMSK = _BV(OCIE1A); // Interuption sur Compare A
TCCR1B = _BV(WGM12)|_BV(CS11); // Mode CTC (retour a zero sur CompA) et prescaler = 1/8 (1MHz)
sei();
}
void loop(void)
enum master_states state = INIT;
void master_loop(void)
{
skip_sleep=0;
analog_run();
radio_run();
ui_run();
// Automate
switch(state){
case INIT:
state=STD;
break;
case STD:
if(RADIO_ASSOC==1)
state= ASSOC;
if(TIR_ON==1)
state= STD_TIR;
break;
case STD_TIR:
if(TIR_ON==0)
state= STD;
break;
case ASSOC:
if (RADIO_ASSOC==0)
state=STD;
if (TIR_ON==1)
state=ASSOC_TIR;
break;
case ASSOC_TIR:
if (RADIO_ASSOC==0)
state=STD;
if (TIR_ON==0)
state= ASSOC;
break;
}
if(!skip_sleep){
@ -114,10 +130,11 @@ void loop(void)
ISR(TIMER1_COMPA_vect){
skip_sleep=1;
}
ISR(INT2_vect){
skip_sleep=1;
if(analog_cpt) analog_cpt--;
if(radio_cpt) radio_cpt--;
if(ui_cpt) ui_cpt--;
}

20
radio.c Normal file
View file

@ -0,0 +1,20 @@
#include "radio.h"
#include "status.h"
#include <avr/io.h>
#include <avr/interrupt.h>
bool RADIO_ASSOC=false;
uint8_t radio_cpt;
void init()
{
GICR|=_BV(INT2);
}
ISR(INT2_vect){
skip_sleep=1;
}
void radio_run(){
}

13
radio.h Normal file
View file

@ -0,0 +1,13 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#ifndef RADIO_h
#define RADIO_h
extern uint8_t radio_cpt;
extern bool RADIO_ASSOC;
void radio_run();
#endif

Binary file not shown.

17
status.h Normal file
View file

@ -0,0 +1,17 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#ifndef STATUS_h
#define STATUS_h
extern uint8_t patch[16];
extern uint16_t lines[16];
extern bool TIR_ON;
extern volatile uint8_t skip_sleep;
enum master_states {INIT, STD, STD_TIR, ASSOC, ASSOC_TIR };
extern enum master_states state;
#endif

63
ui.c Normal file
View file

@ -0,0 +1,63 @@
#include "ui.h"
#include "lcd.h"
#include "tools.h"
#include "status.h"
uint16_t ui_cpt=0;
/*
lcd_init(20,4);
lcd_backlight(1);
lcd_clear();
lcd_home();
lcd_display();
lcd_setCursor(1,1);
lcdprint("** Pyro RF ! **");
spi_init_master();
_delay_ms(500);
lcd_setCursor(3,2);
lcdprint("Init");
*/
void ui_setup()
{
lcd_init(20,4);
lcd_backlight(1);
lcd_clear();
lcd_home();
lcd_display();
};
void ui_splash(){
lcd_setCursor(1,1);
lcdprint("** Pyro RF ! **");
lcd_setCursor(2,2);
lcdprint("A. Houdelette");
lcd_setCursor(3,4);
lcdprint("E. Langlois");
}
void ui_main(){
}
enum ui_states { UI_START, UI_INIT, UI_MAIN} ui_state = UI_START;
void ui_run(){
switch(ui_state){
case UI_START:
ui_setup();
ui_splash();
ui_state = UI_INIT;
ui_cpt=400; // 2s;
break;
case UI_INIT:
if(ui_cpt) return;
if(state==INIT) return;
lcd_clear();
ui_state=UI_MAIN;
case UI_MAIN:
ui_main();
break;
}
}

12
ui.h Normal file
View file

@ -0,0 +1,12 @@
#include <stdint.h>
#include <stdlib.h>
#ifndef UI_h
#define UI_h
extern uint16_t ui_cpt;
void ui_run();
#endif