My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
///////////////////////////////////////////////////////////////////////////////
// protocol.c - Home automation library
//
// Copyright (C) 2009 Jochen Toppe <jochen@jtoee.com>. All right reserved.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
///////////////////////////////////////////////////////////////////////////////


//
// relay receiver hardware setup:
//
// 16MHz external clocked attiny2313
// RX: data in of rf receiver @ 1200 baud
//
// PORTB:
// PB0 - relay 0
// PB1 - relay 1
// PB2 - relay 2
// PB3 - relay 3
// PB4 - lock status LED
// PB5 - command received LED
//
// Device id select via PD2,3,4,5 by pulling them to ground (they're pulled
// up to high via the internal avr pull-up resistors)


#ifdef TEST
#include <stdio.h>
#else
#include <avr/io.h>
#include <util/delay.h>
#endif

#include "../common/config.h"
#include "../common/uart.h"
#include "../common/commands.h"
#include "../common/protocol.h"
#include "../common/commandFilter.h"
#include "../common/util.h"

// MAX number of ports (0..3) for relays
#define MAX_PORT 4

#define COMMAND_RECEIVED_LED 5

#ifndef TEST
void doHandleCommand(struct Command *aCmd)
{
switch (aCmd->command)
{
case COMMAND_RESET:
// reset all four ports (not! the lock LED)
PORTB &= 0xF0;
break;
case COMMAND_LOCK:
lock();
PORTB |= 0x10;
break;
case COMMAND_UNLOCK:
unlock();
PORTB &= 0xEF;
break;
case COMMAND_ON:
// only execute if data outlining port actually present
if (aCmd->dataSize != 0 && aCmd->data[0] < MAX_PORT)
PORTB |= (1<<aCmd->data[0]);
break;
case COMMAND_OFF:
// only execute if data outlining port actually present
if (aCmd->dataSize != 0 && aCmd->data[0] < MAX_PORT)
PORTB &= ~(1<<aCmd->data[0]);
break;
case COMMAND_TOGGLE:
// only execute if data outlining port actually present
if (aCmd->dataSize != 0 && aCmd->data[0] < MAX_PORT)
PORTB ^= 1 << aCmd->data[0];
break;
case COMMAND_PULSE:
// only execute if data outlining port actually present
if (aCmd->dataSize != 0 && aCmd->data[0] < MAX_PORT)
{
PORTB |= (1<<aCmd->data[0]);
if (aCmd->dataSize !=2)
sleep_rf(100); // default to 100ms
else
sleep_rf(aCmd->data[1]);
PORTB &= ~(1<<aCmd->data[0]);
}
break;
}
}

// determine the device id
void determine_device_id(void)
{
// set pins 2,3,4,5 to IN by turning the bits off
DDRD &= 0xC3;
// enable pull-ups
PORTD |= 0x3C;
// settle
_delay_ms(10);
// get bits 2-5 into one number
setDeviceId((PIND >> 2) & 0xF);
}

//
// code for the relay receiver module.
//

int main(void)
{
// register the command handler
setCommandHandler(doHandleCommand);
// set port B to output only
DDRB = 0xFF;
// turn off all pins on port b
PORTB = 0x0;

// determine device id
determine_device_id();

// turn on rf
uart_initialize(1,1);

// handle commands
while (1)
{
if (uart_has_bytes())
{
uint8_t havePackage = parse_incoming(uart_receive());
if (havePackage)
{
enable_bit(PORTB, COMMAND_RECEIVED_LED);
// process command
filterCommand(&_incomingCommand);
//
_delay_ms(50);
disable_bit(PORTB, COMMAND_RECEIVED_LED);
}
}
}
return 0;
}

#else
// unit tests
// ////////////////

extern int commonSuite();
int main(void)
{
return commonSuite();
}

#endif

Change log

r126 by jochen.toppe on Jan 7, 2010   Diff
refactor
Go to: 
Project members, sign in to write a code review

Older revisions

r111 by jochen.toppe on Jan 1, 2010   Diff
[No log message]
r108 by jochen.toppe on Jan 1, 2010   Diff
damn, this is working
r105 by jochen.toppe on Jan 1, 2010   Diff
[No log message]
All revisions of this file

File info

Size: 4559 bytes, 168 lines
Powered by Google Project Hosting