reformat
This commit is contained in:
95
main.cpp
95
main.cpp
@ -29,6 +29,8 @@
|
||||
#define welcomeString "HELOJANA"
|
||||
static constexpr bool bdayMsg = false;
|
||||
|
||||
static constexpr int timeOffsetSeconds = 6;
|
||||
|
||||
void buttonHandler(uint8_t index, uint8_t type, void* data);
|
||||
SVector<Sensor, MAX_SENSORS> sensors;
|
||||
ShiftReg<16> shiftReg(&PORTB, PB3, PB2, PB1);
|
||||
@ -55,20 +57,28 @@ volatile bool ringging = false;
|
||||
|
||||
char buffer[SNPRINTF_BUFFER_SIZE];
|
||||
|
||||
volatile bool sensorsPaused = true;
|
||||
volatile bool sensorsPaused = false;
|
||||
|
||||
volatile bool relaySetting = false;
|
||||
|
||||
volatile uint8_t timer = 0;
|
||||
volatile uint8_t displayDevider = 1;
|
||||
|
||||
ISR(TIMER1_COMPA_vect)
|
||||
{
|
||||
W433DataReciver::staticInterrupt();
|
||||
if(displayDevider > 1)
|
||||
{
|
||||
TIMSK2 = 0;
|
||||
W433DataReciver::staticInterrupt();
|
||||
writePin(&PORTB, PB5, !readPin(&PORTB, PB5));
|
||||
TIMSK2 = 1;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER2_OVF_vect)
|
||||
{
|
||||
display.tick();
|
||||
if(timer % displayDevider == 0)
|
||||
display.tick();
|
||||
buttons.tick();
|
||||
if(ringging && ((timer % 4 == 0 && timer < 128) || (timer > 128 && timer % 16 == 0)))
|
||||
writePin(&PORTD, PD4, true);
|
||||
@ -90,7 +100,7 @@ void buttonHandler(uint8_t index, uint8_t type, void* data)
|
||||
{
|
||||
relaySetting = !relaySetting;
|
||||
}
|
||||
else if(index == 1 && type == Buttons::RELEASED )
|
||||
else if(index == 1 && type == Buttons::RELEASED)
|
||||
{
|
||||
if(!ringging)
|
||||
{
|
||||
@ -122,9 +132,12 @@ void printSensor(const Sensor& sensor, Serial* serial)
|
||||
serial->write_p(PSTR(" ID: "));
|
||||
serial->write(sensor.id);
|
||||
|
||||
if(sensor.type == 1) serial->write_p(PSTR(" TEMPERATURE: "));
|
||||
else if(sensor.type == 2) serial->write_p(PSTR(" HUMIDITY: "));
|
||||
else serial->write_p(PSTR(" FIELD: "));
|
||||
if(sensor.type == 1)
|
||||
serial->write_p(PSTR(" TEMPERATURE: "));
|
||||
else if(sensor.type == 2)
|
||||
serial->write_p(PSTR(" HUMIDITY: "));
|
||||
else
|
||||
serial->write_p(PSTR(" FIELD: "));
|
||||
serial->write(sensor.field);
|
||||
serial->putChar('\n');
|
||||
}
|
||||
@ -148,9 +161,11 @@ void packetHandler(uint32_t packet, void* data)
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if(!found) sensors.push_back(sensor);
|
||||
if(!found)
|
||||
sensors.push_back(sensor);
|
||||
|
||||
if(!sensorsPaused) printSensor(sensor, serial);
|
||||
if(!sensorsPaused)
|
||||
printSensor(sensor, serial);
|
||||
}
|
||||
|
||||
void reciverError(uint8_t code, void* userData)
|
||||
@ -166,16 +181,16 @@ void reciverError(uint8_t code, void* userData)
|
||||
|
||||
inline static void printHelp(Serial* serial)
|
||||
{
|
||||
serial->write_p(PSTR("Available Commands: \n\
|
||||
help : Show this prompt.\n\
|
||||
date : Show current date and time.\n\
|
||||
set [yyyy] [mm] [dd] [hh] [mm] [ss] : Show current date and time.\n\
|
||||
pause : pause sensor output.\n\
|
||||
resume : resume sensor output.\n\
|
||||
dump : Dump epprom.\n\
|
||||
free : Show free ram.\n\
|
||||
beep : Test buzzer.\n\
|
||||
list : List sensors.\n"));
|
||||
serial->write_p(PSTR("Available Commands: \n"
|
||||
"help : Show this prompt.\n"
|
||||
"date : Show current date and time.\n"
|
||||
"set [yyyy] [mm] [dd] [hh] [mm] [ss] : Show current date and time.\n"
|
||||
"pause : pause sensor output.\n"
|
||||
"resume : resume sensor output.\n"
|
||||
"dump : Dump epprom.\n"
|
||||
"free : Show free ram.\n"
|
||||
"beep : Test buzzer.\n"
|
||||
"list : List sensors.\n"));
|
||||
}
|
||||
|
||||
|
||||
@ -232,8 +247,11 @@ void serialDispatch(Serial* serial, SVector<Sensor, MAX_SENSORS>* sensors)
|
||||
}
|
||||
else if(strcmp(token, "list") == 0)
|
||||
{
|
||||
serial->write(sensors->count());
|
||||
serial->putChar(' ');
|
||||
serial->write_p(PSTR("Sensors:\n"));
|
||||
for(uint8_t i = 0; i < sensors->count(); ++i) printSensor(sensors->at(i), serial);
|
||||
for(uint8_t i = 0; i < sensors->count(); ++i)
|
||||
printSensor(sensors->at(i), serial);
|
||||
serial->write('\n');
|
||||
}
|
||||
else if(strcmp(token, "erase") == 0)
|
||||
@ -268,7 +286,10 @@ void serialDispatch(Serial* serial, SVector<Sensor, MAX_SENSORS>* sensors)
|
||||
{
|
||||
printHelp(serial);
|
||||
}
|
||||
else serial->write_p(PSTR("Not a valid command\n"));
|
||||
else
|
||||
{
|
||||
serial->write_p(PSTR("Not a valid command\n"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -338,11 +359,12 @@ void setAlarm(DS1302::Timeval* alarm, uint8_t leadingSegment = 0)
|
||||
|
||||
int main()
|
||||
{
|
||||
DDRB = (1 << PB1) | ( 1 << PB2) | ( 1 << PB3) | ( 1 << PB4) | ( 1 << PB5);
|
||||
DDRB |= (1 << PB1) | ( 1 << PB2) | ( 1 << PB3);
|
||||
DDRB |= ( 1 << PB4) | ( 1 << PB5);
|
||||
DDRD = (1<<PD4) | (1<<PD2);
|
||||
PORTD |= (1<<PD6) | (1<<PD7);
|
||||
|
||||
TCCR2B = 1<<CS22;
|
||||
TCCR2B = 1<<CS22 | 1<<CS20;
|
||||
TIMSK2 = 1;
|
||||
|
||||
TCCR1B = 1<<CS10;
|
||||
@ -366,12 +388,12 @@ int main()
|
||||
#endif
|
||||
|
||||
display.setString(welcomeString);
|
||||
_delay_ms(1000);
|
||||
_delay_ms(500);
|
||||
|
||||
#ifdef HAS_DHT
|
||||
Dht11 sensor(&PORTD, &PIND, &DDRD, PD2);
|
||||
sensor.read();
|
||||
_delay_ms(1000);
|
||||
_delay_ms(500);
|
||||
sensor.read();
|
||||
#endif
|
||||
|
||||
@ -434,14 +456,14 @@ int main()
|
||||
displayItems(time, 0, 0);
|
||||
#endif
|
||||
|
||||
if(time.hour == alarmA.hour && time.min == alarmA.min && time.sec == 0)
|
||||
if((alm & almA) && time.hour == alarmA.hour && time.min == alarmA.min && time.sec == 0)
|
||||
{
|
||||
ringging = true;
|
||||
}
|
||||
|
||||
if(time.hour == alarmB.hour && time.min == alarmB.min && time.sec == 0)
|
||||
if((alm & almB) && time.hour == alarmB.hour && time.min == alarmB.min && time.sec == 0)
|
||||
{
|
||||
#ifdef HAS_TRANSMITTER
|
||||
#ifdef HAS_TRANSMITTER
|
||||
relay.setValue(true);
|
||||
#else
|
||||
ringging = true;
|
||||
@ -449,6 +471,7 @@ int main()
|
||||
}
|
||||
|
||||
serialDispatch(&serial, &sensors);
|
||||
|
||||
#ifdef HAS_RECIVER
|
||||
if(deleteDate != time.day)
|
||||
{
|
||||
@ -456,9 +479,23 @@ int main()
|
||||
sensors.clear();
|
||||
deleteDate = time.day;
|
||||
display.setString("CLEAR ");
|
||||
_delay_ms(1000);
|
||||
_delay_ms(500);
|
||||
}
|
||||
#endif
|
||||
|
||||
if(time.min > 45)
|
||||
displayDevider = 16;
|
||||
else
|
||||
displayDevider = 1;
|
||||
|
||||
if(time.hour == 0 && time.min == 0 && time.sec == timeOffsetSeconds+10)
|
||||
{
|
||||
DS1302::Timeval timeSet = time;
|
||||
timeSet.sec = 10;
|
||||
clock.setTime(timeSet);
|
||||
display.setString("CAL ");
|
||||
_delay_ms(500);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
|
156
ringbuffer.h
Executable file → Normal file
156
ringbuffer.h
Executable file → Normal file
@ -23,103 +23,103 @@ class RingBuffer
|
||||
{
|
||||
private:
|
||||
|
||||
volatile uint_fast16_t _headIndex = 0;
|
||||
volatile uint_fast16_t _tailIndex = 0;
|
||||
volatile uint_fast16_t _headIndex = 0;
|
||||
volatile uint_fast16_t _tailIndex = 0;
|
||||
volatile bool _overrun = false;
|
||||
volatile T _buffer[BUFFER_SIZE];
|
||||
volatile T _buffer[BUFFER_SIZE];
|
||||
|
||||
public:
|
||||
|
||||
RingBuffer()
|
||||
{
|
||||
flush();
|
||||
}
|
||||
RingBuffer()
|
||||
{
|
||||
flush();
|
||||
}
|
||||
|
||||
uint_fast16_t remaining() const volatile
|
||||
{
|
||||
return (_headIndex-_tailIndex);
|
||||
}
|
||||
uint_fast16_t remaining() const volatile
|
||||
{
|
||||
return (_headIndex-_tailIndex);
|
||||
}
|
||||
|
||||
uint_fast16_t remainingCapacity() const volatile
|
||||
{
|
||||
return BUFFER_SIZE - (_headIndex-_tailIndex);
|
||||
}
|
||||
uint_fast16_t remainingCapacity() const volatile
|
||||
{
|
||||
return BUFFER_SIZE - (_headIndex-_tailIndex);
|
||||
}
|
||||
|
||||
bool isOverun() volatile
|
||||
{
|
||||
bool returnVal = _overrun;
|
||||
_overrun = false;
|
||||
return returnVal;
|
||||
}
|
||||
bool isOverun() volatile
|
||||
{
|
||||
bool returnVal = _overrun;
|
||||
_overrun = false;
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
bool isEmpty() const volatile
|
||||
{
|
||||
return _tailIndex >= _headIndex;
|
||||
}
|
||||
bool isEmpty() const volatile
|
||||
{
|
||||
return _tailIndex >= _headIndex;
|
||||
}
|
||||
|
||||
T read() volatile
|
||||
{
|
||||
if(!isEmpty())
|
||||
{
|
||||
_tailIndex++;
|
||||
return _buffer[(_tailIndex - 1) % BUFFER_SIZE];
|
||||
}
|
||||
else return '\0';
|
||||
}
|
||||
T read() volatile
|
||||
{
|
||||
if(!isEmpty())
|
||||
{
|
||||
_tailIndex++;
|
||||
return _buffer[(_tailIndex - 1) % BUFFER_SIZE];
|
||||
}
|
||||
else return '\0';
|
||||
}
|
||||
|
||||
unsigned int read( T* buffer, unsigned int length ) volatile
|
||||
{
|
||||
unsigned int i = 0;
|
||||
for(; i < length && !isEmpty(); i++)
|
||||
{
|
||||
buffer[i] = read();
|
||||
}
|
||||
return i;
|
||||
}
|
||||
unsigned int read( T* buffer, unsigned int length ) volatile
|
||||
{
|
||||
unsigned int i = 0;
|
||||
for(; i < length && !isEmpty(); i++)
|
||||
{
|
||||
buffer[i] = read();
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
void write( T in ) volatile
|
||||
{
|
||||
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
|
||||
{
|
||||
_headIndex -= BUFFER_SIZE;
|
||||
_tailIndex -= BUFFER_SIZE;
|
||||
}
|
||||
_buffer[_headIndex % BUFFER_SIZE] = in;
|
||||
_headIndex++;
|
||||
void write( T in ) volatile
|
||||
{
|
||||
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
|
||||
{
|
||||
_headIndex -= BUFFER_SIZE;
|
||||
_tailIndex -= BUFFER_SIZE;
|
||||
}
|
||||
_buffer[_headIndex % BUFFER_SIZE] = in;
|
||||
_headIndex++;
|
||||
if(remaining() > BUFFER_SIZE)
|
||||
{
|
||||
_overrun = true;
|
||||
_tailIndex = _headIndex - BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void write( T* buffer, const unsigned int length ) volatile
|
||||
{
|
||||
for(unsigned int i = 0; i < length; i++) write(buffer[i]);
|
||||
}
|
||||
void write( T* buffer, const unsigned int length ) volatile
|
||||
{
|
||||
for(unsigned int i = 0; i < length; i++) write(buffer[i]);
|
||||
}
|
||||
|
||||
void flush(T flushCharacter = ' ') volatile
|
||||
{
|
||||
_headIndex = 0;
|
||||
_tailIndex = 0;
|
||||
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
|
||||
}
|
||||
void flush(T flushCharacter = ' ') volatile
|
||||
{
|
||||
_headIndex = 0;
|
||||
_tailIndex = 0;
|
||||
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
|
||||
}
|
||||
|
||||
unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
|
||||
{
|
||||
unsigned int i = 0;
|
||||
for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++);
|
||||
unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
|
||||
{
|
||||
unsigned int i = 0;
|
||||
for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++);
|
||||
|
||||
if( i < remaining() && i > 0)
|
||||
{
|
||||
if(i > bufferLength-1) i = bufferLength-1;
|
||||
read(buffer, i);
|
||||
buffer[i]='\0';
|
||||
_tailIndex++;
|
||||
}
|
||||
else if(i == 0) _tailIndex++;
|
||||
else i = 0;
|
||||
if( i < remaining() && i > 0)
|
||||
{
|
||||
if(i > bufferLength-1) i = bufferLength-1;
|
||||
read(buffer, i);
|
||||
buffer[i]='\0';
|
||||
_tailIndex++;
|
||||
}
|
||||
else if(i == 0) _tailIndex++;
|
||||
else i = 0;
|
||||
|
||||
return i;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
};
|
||||
|
129
serial.cpp
129
serial.cpp
@ -5,7 +5,7 @@ volatile RingBuffer<SERIAL_BUFFER_SIZE, volatile uint8_t> rxBuffer;
|
||||
|
||||
bool stopped = false;
|
||||
|
||||
ISR(USART_RX_vect) //I have seen worse interrupt sintax
|
||||
ISR(USART_RX_vect)
|
||||
{
|
||||
rxBuffer.write(UDR0);
|
||||
if(serialFlowControl && !stopped && rxBuffer.remainingCapacity() < 32)
|
||||
@ -18,111 +18,114 @@ ISR(USART_RX_vect) //I have seen worse interrupt sintax
|
||||
|
||||
Serial::Serial()
|
||||
{
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX
|
||||
UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX
|
||||
UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt
|
||||
sei();
|
||||
}
|
||||
|
||||
void Serial::putChar(const char c)
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = c;
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = c;
|
||||
}
|
||||
|
||||
void Serial::write(const char* in, const unsigned int length)
|
||||
{
|
||||
for(unsigned int i = 0; i < length && in[i] != '\0'; i++)
|
||||
{
|
||||
putChar(in[i]);
|
||||
}
|
||||
for(unsigned int i = 0; i < length && in[i] != '\0'; i++)
|
||||
{
|
||||
putChar(in[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Serial::write_p(const char in[])
|
||||
{
|
||||
cli();
|
||||
char ch = pgm_read_byte(in);
|
||||
while (ch != '\0')
|
||||
{
|
||||
putChar(ch);
|
||||
in++;
|
||||
ch = pgm_read_byte(in);
|
||||
}
|
||||
sei();
|
||||
cli();
|
||||
char ch = pgm_read_byte(in);
|
||||
while (ch != '\0')
|
||||
{
|
||||
putChar(ch);
|
||||
in++;
|
||||
ch = pgm_read_byte(in);
|
||||
}
|
||||
sei();
|
||||
}
|
||||
|
||||
void Serial::write(const char in[])
|
||||
{
|
||||
for(unsigned int i = 0; i < strlen(in); i++)
|
||||
{
|
||||
putChar(in[i]);
|
||||
}
|
||||
for(unsigned int i = 0; i < strlen(in); i++)
|
||||
{
|
||||
putChar(in[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Serial::write(int32_t in)
|
||||
{
|
||||
if(in == 0)
|
||||
{
|
||||
putChar('0');
|
||||
}
|
||||
else
|
||||
{
|
||||
bool flag = false;
|
||||
char str[64] = { 0 };
|
||||
int16_t i = 62;
|
||||
if (in < 0)
|
||||
{
|
||||
flag = true;
|
||||
in = abs(in);
|
||||
}
|
||||
if(in == 0)
|
||||
{
|
||||
putChar('0');
|
||||
}
|
||||
else
|
||||
{
|
||||
bool flag = false;
|
||||
char str[64] = { 0 };
|
||||
int16_t i = 62;
|
||||
if (in < 0)
|
||||
{
|
||||
flag = true;
|
||||
in = abs(in);
|
||||
}
|
||||
|
||||
while (in != 0 && i > 0)
|
||||
{
|
||||
str[i--] = (in % 10) + '0';
|
||||
in /= 10;
|
||||
}
|
||||
while (in != 0 && i > 0)
|
||||
{
|
||||
str[i--] = (in % 10) + '0';
|
||||
in /= 10;
|
||||
}
|
||||
|
||||
if (flag) str[i--] = '-';
|
||||
write(str + i + 1, 64-(i+1));
|
||||
}
|
||||
if (flag) str[i--] = '-';
|
||||
write(str + i + 1, 64-(i+1));
|
||||
}
|
||||
}
|
||||
|
||||
bool Serial::dataIsWaiting()
|
||||
{
|
||||
return !rxBuffer.isEmpty();
|
||||
return !rxBuffer.isEmpty();
|
||||
}
|
||||
|
||||
char Serial::getChar()
|
||||
{
|
||||
if(!rxBuffer.isEmpty())
|
||||
{
|
||||
if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 32 )
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = 0x11;
|
||||
stopped = false;
|
||||
}
|
||||
return rxBuffer.read();
|
||||
}
|
||||
else return '\0';
|
||||
if(!rxBuffer.isEmpty())
|
||||
{
|
||||
if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 32 )
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = 0x11;
|
||||
stopped = false;
|
||||
}
|
||||
return rxBuffer.read();
|
||||
}
|
||||
else return '\0';
|
||||
}
|
||||
|
||||
unsigned int Serial::getString(char* buffer, const int bufferLength)
|
||||
{
|
||||
return rxBuffer.getString(_terminator, (uint8_t*)buffer, bufferLength);
|
||||
return rxBuffer.getString(_terminator, (uint8_t*)buffer, bufferLength);
|
||||
}
|
||||
|
||||
unsigned int Serial::read(char* buffer, const unsigned int length )
|
||||
{
|
||||
return rxBuffer.read((uint8_t*)buffer, length);
|
||||
return rxBuffer.read((uint8_t*)buffer, length);
|
||||
}
|
||||
|
||||
void Serial::flush()
|
||||
{
|
||||
rxBuffer.flush();
|
||||
rxBuffer.flush();
|
||||
}
|
||||
|
||||
void Serial::setTerminator(char terminator){_terminator = terminator;}
|
||||
void Serial::setTerminator(char terminator)
|
||||
{
|
||||
_terminator = terminator;
|
||||
}
|
||||
|
26
serial.h
26
serial.h
@ -16,21 +16,21 @@ const bool serialFlowControl = false;
|
||||
class Serial
|
||||
{
|
||||
private:
|
||||
char _terminator = '\n';
|
||||
char _terminator = '\n';
|
||||
|
||||
public:
|
||||
Serial();
|
||||
void putChar(const char c);
|
||||
void write(const char* in, const unsigned int length);
|
||||
void write(const char in[]);
|
||||
void write_p(const char in[]); //for flash space strigns
|
||||
void write(const int32_t in);
|
||||
unsigned int read( char* buffer, const unsigned int length );
|
||||
bool dataIsWaiting();
|
||||
char getChar();
|
||||
unsigned int getString(char* buffer, const int bufferLength);
|
||||
void flush();
|
||||
void setTerminator(const char terminator);
|
||||
Serial();
|
||||
void putChar(const char c);
|
||||
void write(const char* in, const unsigned int length);
|
||||
void write(const char in[]);
|
||||
void write_p(const char in[]); //for flash space strigns
|
||||
void write(const int32_t in);
|
||||
unsigned int read( char* buffer, const unsigned int length );
|
||||
bool dataIsWaiting();
|
||||
char getChar();
|
||||
unsigned int getString(char* buffer, const int bufferLength);
|
||||
void flush();
|
||||
void setTerminator(const char terminator);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user