Subversion Repositories HomeAutomation

Rev

Rev 190 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 190 Rev 192
Line 13... Line 13...
13
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
13
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
14
 *
14
 *
15
 *
15
 *
16
 */
16
 */
17
 
17
 
18
#define DEBUG   0 /* Prints debug messages to uart */
18
#define DEBUG           1 /* Prints debug messages to uart */
-
 
19
#define TWO_BUTTON_MODE 1 /* If using two (or just one but nothing more) auxiliary buttons */
19
 
20
 
20
/*-----------------------------------------------------------------------------
21
/*-----------------------------------------------------------------------------
21
 * Includes
22
 * Includes
22
 *---------------------------------------------------------------------------*/
23
 *---------------------------------------------------------------------------*/
23
/* system files */
24
/* system files */
Line 44... Line 45...
44
#define RELAY_CMD_OFF       0x02
45
#define RELAY_CMD_OFF       0x02
45
#define RELAY_CMD_TOGGLE    0x03
46
#define RELAY_CMD_TOGGLE    0x03
46
#define RELAY_CMD_STATUS    0x04
47
#define RELAY_CMD_STATUS    0x04
47
#define RELAY_CMD_RESET     0x05
48
#define RELAY_CMD_RESET     0x05
48
#define STATUS_SEND_PERIOD  10000 /* milliseconds */
49
#define STATUS_SEND_PERIOD  10000 /* milliseconds */
49
#define FAILSAFE_SEND_PERIOD  10000 /* milliseconds */
50
#define FAILSAFE_SEND_PERIOD    10000 /* milliseconds */
-
 
51
#define BOUNCE_TIME         10 /* milliseconds */
-
 
52
#define BUTTON_ID           0x1202
-
 
53
#define BUTTON1             1
-
 
54
#define BUTTON2             2
-
 
55
 
-
 
56
/*-------------------------------------------------------------------------
-
 
57
 * Global variables
-
 
58
 * -----------------------------------------------------------------------*/
-
 
59
#if TWO_BUTTON_MODE
-
 
60
Can_Message_t txMsg_Button;
-
 
61
#endif
50
 
62
 
51
/*----------------------------------------------------------------------------
63
/*----------------------------------------------------------------------------
52
 * Functions
64
 * Functions
53
 * -------------------------------------------------------------------------*/
65
 * -------------------------------------------------------------------------*/
54
uint8_t relayOff();
66
uint8_t relayOff();
55
uint8_t relayOn();
67
uint8_t relayOn();
56
void relayFailsafe();
68
void relayFailsafe();
-
 
69
 
-
 
70
/*----------------------------------------------------------------------------
-
 
71
 * Interrupt routines
-
 
72
 * -------------------------------------------------------------------------*/
-
 
73
#if TWO_BUTTON_MODE
-
 
74
ISR( PCINT2_vect )
-
 
75
{
-
 
76
    /*
-
 
77
     * Auxiliary button pressed and released
-
 
78
     * Check time between press and release to eliminate bounces
-
 
79
     */
-
 
80
    static uint32_t buttonTime[2];
-
 
81
 
-
 
82
    txMsg_Button.RemoteFlag = 0;
-
 
83
    txMsg_Button.ExtendedFlag = 1;
-
 
84
 
-
 
85
    if( (PIND & (1<<PD6)) == 0){
-
 
86
        /* Button pressed */
-
 
87
        buttonTime[0] = Timebase_CurrentTime();
-
 
88
#if DEBUG
-
 
89
        printf("Button 1 pressed\n");
-
 
90
#endif
-
 
91
    }else if( (PIND & (1<<PD6)) == 1){
-
 
92
        if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){
-
 
93
            /* No bounce, send message */
-
 
94
            txMsg_Button.Id = BUTTON_ID;
-
 
95
            txMsg_Button.DataLength = 1;
-
 
96
            txMsg_Button.Data.bytes[0] = BUTTON1;
-
 
97
#if DEBUG
-
 
98
            printf("Button 1 pressed: message sent\n");
-
 
99
#endif
-
 
100
            return;
-
 
101
        }else{
-
 
102
            /* Just bounces, ignore it */
-
 
103
#if DEBUG
-
 
104
            printf("Button 1 just bounced\n");
-
 
105
#endif
-
 
106
            return;
-
 
107
        }
-
 
108
    }
-
 
109
    if( (PIND & (1<<PD7)) == 0){
-
 
110
        /* Button pressed */
-
 
111
        buttonTime[1] = Timebase_CurrentTime();
-
 
112
#if DEBUG
-
 
113
        printf("Button 2 pressed\n");
-
 
114
#endif
-
 
115
    }else if( (PIND & (1<<PD7)) == 1){
-
 
116
        if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){
-
 
117
            /* No bounce, send message */
-
 
118
            txMsg_Button.Id = BUTTON_ID;
-
 
119
            txMsg_Button.DataLength = 1;
-
 
120
            txMsg_Button.Data.bytes[0] = BUTTON2;
-
 
121
#if DEBUG
-
 
122
            printf("Button 2 pressed: message sent\n");
-
 
123
#endif
-
 
124
            return;
-
 
125
        }else{
-
 
126
            /* Just bounces, ignore it */
-
 
127
#if DEBUG
-
 
128
            printf("Button 2 just bounced\n");
-
 
129
#endif
-
 
130
            return;
-
 
131
        }
-
 
132
    }
-
 
133
}
-
 
134
#endif
-
 
135
 
-
 
136
//ISR( ADC_vect )
-
 
137
//{
-
 
138
    /*
-
 
139
     * ADC conversion completed
-
 
140
     */
-
 
141
    // TODO använd denna rutin för att läsa värde när omvandlingen är klar
-
 
142
 
-
 
143
//}
57
 
144
 
58
/*-----------------------------------------------------------------------------
145
/*-----------------------------------------------------------------------------
59
 * Main Program
146
 * Main Program
60
 *---------------------------------------------------------------------------*/
147
 *---------------------------------------------------------------------------*/
61
int main(void) {
148
int main(void) {
Line 67... Line 154...
67
 
154
 
68
    Timebase_Init();
155
    Timebase_Init();
69
#if DEBUG
156
#if DEBUG
70
    Serial_Init();
157
    Serial_Init();
71
#endif
158
#endif
-
 
159
 
-
 
160
#if TWO_BUTTON_MODE
-
 
161
    /*
-
 
162
     * Initialize buttons
-
 
163
     * It is possible to use ie. sensors instead
-
 
164
     * but then change this
-
 
165
     */
-
 
166
    /* Set as input and enable pull-up */
-
 
167
    DDRD &= ~( (1<<DDD6)|(1<<DDD7) );
-
 
168
    PORTD |= (1<<PD6)|(1<<PD7);
-
 
169
 
-
 
170
    /* Enable IO-pin interrupt */
-
 
171
    PCICR |= (1<<PCIE1);
-
 
172
    /* Unmask PD6 and PD7 */
-
 
173
    PCMSK2 |= (1<<PCINT22) | (1<<PCINT23);
-
 
174
        /* Button pressed */
-
 
175
#endif
-
 
176
 
-
 
177
 
72
    sei();
178
    sei();
73
 
179
 
74
    /* Turn relay off */
180
    /* Turn relay off */
75
    relayStatus = relayOff();
181
    relayStatus = relayOff();
76
 
182
 
Line 215... Line 321...
215
        while (Can_Receive(&rxMsg) == CAN_OK) {
321
        while (Can_Receive(&rxMsg) == CAN_OK) {
216
            /* This relay is adressed*/
322
            /* This relay is adressed*/
217
            if( rxMsg.Id == RELAY_CMD_GET ){
323
            if( rxMsg.Id == RELAY_CMD_GET ){
218
                if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){
324
                if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){
219
                    /* Return from failsafe mode */
325
                    /* Return from failsafe mode */
220
                    return 0;
326
                    return;
221
                }
327
                }
222
            }
328
            }
223
        }
329
        }
224
 
330
 
225
        if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){
331
        if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){