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