Skip to content

Commit

Permalink
implemented space handling in receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowojski committed Jan 17, 2012
1 parent 19be812 commit e3b3764
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define CONFIG_H_

//1 sec ~ 61*64
#define REPEAT_TIME (61*1)
#define REPEAT_TIME (61*2)

#define F_CPU 8000000UL /* 8 MHz */

Expand Down
68 changes: 43 additions & 25 deletions receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
uint16_t EmaThreshold = 0x1FF;

enum ReceiverState {
Idle,
Reading,
CoolingDown,
OverFlow
Idle = 0,
Reading = 1,
Space = 2,
Message = 3,
OverFlow = 4
};

volatile enum ReceiverState _state = Idle;
Expand All @@ -46,6 +47,27 @@ uint8_t _bufferEnd = 0;
uint8_t _bufferWriteBit = 0; // next bit read position is _inputBuffer[_bufferEnd][_bufferWriteBit]
char _inputBuffer[IN_BUFFER_SIZE][MORSE_CODE_SIZE];

uint8_t incrementBuffer() {
_bufferEnd = (_bufferEnd + 1) % IN_BUFFER_SIZE;
_bufferWriteBit = 0;

if (_bufferEnd == _bufferStart) {
_state = OverFlow;
return 0;
}
return 1;
}

void incrementBufferAndHandleState(enum ReceiverState nextState, uint8_t c) {
_inputBuffer[_bufferEnd][_bufferWriteBit] = c;

if (incrementBuffer()) {
_lowRepeatCount = 0;
_state = nextState;
}

}

ISR(TIMER0_COMP_vect) {
}

Expand Down Expand Up @@ -119,30 +141,24 @@ ISR(ADC_vect) {
else if (_state == Reading) {
++_lowRepeatCount;

if (_lowRepeatCount >= _charSpaceRepeat) {

_inputBuffer[_bufferEnd][_bufferWriteBit] = 0;

_bufferEnd = (_bufferEnd + 1) % IN_BUFFER_SIZE;
_bufferWriteBit = 0;

if (_bufferEnd == _bufferStart)
_state = OverFlow;

_lowRepeatCount = 0;
_state = CoolingDown;
if (_lowRepeatCount >= _charSpaceRepeat) {
incrementBufferAndHandleState(Space, 0);
}
}
else if (_state == Space) {
++_lowRepeatCount;

if (_lowRepeatCount >= _wordSpaceRepeat) {
incrementBufferAndHandleState(Message, Space);
}
}
else if (_state == CoolingDown) {
else if (_state == Message) {
++_lowRepeatCount;

if (_lowRepeatCount >= _messageSpaceRepeat) {
_inputBuffer[_bufferEnd][0] = 0;
_bufferEnd = (_bufferEnd + 1) % IN_BUFFER_SIZE;
_bufferWriteBit = 0;
_state = Idle;
incrementBufferAndHandleState(Idle, Message);
}
}
}
}
}

Expand Down Expand Up @@ -184,10 +200,12 @@ int main(void)
//lcdString(_inputBuffer[_bufferStart]);
const char *message = _inputBuffer[_bufferStart];

if (message[0] != 0)
lcdData(getMorseChar(message));
else
if (message[0] == Space)
lcdData(' ');
else if (message[0] == Message)
lcdClear();
else if (message[0] != 0)
lcdData(getMorseChar(message));

_bufferStart = (_bufferStart + 1) % IN_BUFFER_SIZE;
}
Expand Down

0 comments on commit e3b3764

Please sign in to comment.