Subversion Repositories HomeAutomation

Rev

Rev 1479 | Rev 1484 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1479 Rev 1483
Line 2... Line 2...
2
#include "sns_irTransceive.h"
2
#include "sns_irTransceive.h"
3
 
3
 
-
 
4
#if IR_RX_ENABLE==1
4
struct {
5
struct {
5
    uint8_t             state;
6
    uint8_t             state;
6
    uint8_t             newData;
7
    uint8_t             newData;
7
    uint16_t            rxbuf[MAX_NR_TIMES];
8
    uint16_t            *rxbuf;
8
    uint8_t             rxlen;
9
    uint8_t             rxlen;
9
    uint8_t             timerNum;
10
    uint8_t             timerNum;
10
    Ir_Protocol_Data_t  proto;
11
    Ir_Protocol_Data_t  proto;
11
} irRxChannel[sns_irTransceive_SUPPORTED_NUM_CHANNELS];
12
} irRxChannel[sns_irTransceive_SUPPORTED_NUM_CHANNELS];
-
 
13
#endif
-
 
14
 
-
 
15
uint16_t    buf0[MAX_NR_TIMES];
-
 
16
uint16_t    buf1[MAX_NR_TIMES];
-
 
17
uint16_t    buf2[MAX_NR_TIMES];
-
 
18
 
12
 
19
 
13
StdCan_Msg_t        irTxMsg;
20
StdCan_Msg_t        irTxMsg;
14
 
21
 
15
#if (sns_irTransceive_SEND_DEBUG==1)
22
#if (sns_irTransceive_SEND_DEBUG==1)
16
void send_debug(uint16_t *buffer, uint8_t len) {
23
void send_debug(uint16_t *buffer, uint8_t len) {
Line 49... Line 56...
49
        }
56
        }
50
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
57
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
51
        while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
58
        while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
52
        _delay_ms(1);
59
        _delay_ms(1);
53
    }
60
    }
54
 
61
 
55
}
62
}
56
#endif
63
#endif
57
 
64
 
-
 
65
#if IR_RX_ENABLE==1
58
void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t* buffer, uint8_t len)
66
void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
59
{
67
{
60
    if (channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS)
68
    if (channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS)
61
    {
69
    {
62
        irRxChannel[channel].newData = TRUE;
70
        irRxChannel[channel].newData = TRUE;
63
        irRxChannel[channel].rxlen = len;
71
        irRxChannel[channel].rxlen = len;
64
    }
72
    }
-
 
73
}
-
 
74
#endif
-
 
75
 
-
 
76
void sns_irTransceive_TX_done_callback(uint8_t channel)
-
 
77
{
-
 
78
 
65
}
79
}
66
 
80
 
67
void sns_irTransceive_Init(void)
81
void sns_irTransceive_Init(void)
68
{
82
{
69
gpio_set_out(EXP_A);
83
gpio_set_out(EXP_A);
Line 75... Line 89...
75
    irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
89
    irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
76
    irTxMsg.Length = 6;
90
    irTxMsg.Length = 6;
77
 
91
 
78
    for (uint8_t i = 0; i < sns_irTransceive_SUPPORTED_NUM_CHANNELS; i++)
92
    for (uint8_t i = 0; i < sns_irTransceive_SUPPORTED_NUM_CHANNELS; i++)
79
    {
93
    {
-
 
94
#if IR_RX_ENABLE==1
80
        irRxChannel[i].state = sns_irTransceive_STATE_RECEIVING;
95
        irRxChannel[i].state = sns_irTransceive_STATE_RECEIVING;
81
        irRxChannel[i].newData = FALSE;
96
        irRxChannel[i].newData = FALSE;
82
        irRxChannel[i].rxlen = 0;
97
        irRxChannel[i].rxlen = 0;
83
        irRxChannel[i].proto.timeout=0;
98
        irRxChannel[i].proto.timeout=0;
84
        irRxChannel[i].proto.data=0;
99
        irRxChannel[i].proto.data=0;
85
        irRxChannel[i].proto.repeats=0;
100
        irRxChannel[i].proto.repeats=0;
86
        irRxChannel[i].proto.protocol=0;
101
        irRxChannel[i].proto.protocol=0;
-
 
102
#endif
87
    }
103
    }
-
 
104
   
-
 
105
#if IR_RX_ENABLE==1
88
    irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
106
    irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
89
    irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
107
    irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
90
    irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
108
    irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
-
 
109
#endif
91
   
110
   
92
    IrTransceiver_Init();
111
    IrTransceiver_Init();
93
   
112
 
-
 
113
#if IR_RX_ENABLE==1
-
 
114
    irRxChannel[0].rxbuf = buf0;
94
    IrTransceiver_Init_TX_Channel(0, irRxChannel[0].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
115
    IrTransceiver_InitRxChannel(0, irRxChannel[0].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
-
 
116
#endif
-
 
117
 
-
 
118
#if IR_TX_ENABLE==1
-
 
119
    IrTransceiver_InitTxChannel(0, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
-
 
120
#endif
-
 
121
 
95
}
122
}
96
 
123
 
97
void sns_irTransceive_Process(void)
124
void sns_irTransceive_Process(void)
98
{
125
{
99
    uint8_t res;
126
    uint8_t res;
-
 
127
   
100
    for (uint8_t channel=0; channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS; channel++)
128
    for (uint8_t channel=0; channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS; channel++)
101
    {
129
    {
102
//gpio_toggle_pin(EXP_A);
130
//gpio_toggle_pin(EXP_A);
-
 
131
 
-
 
132
#if IR_RX_ENABLE==1
103
        switch (irRxChannel[channel].state)
133
        switch (irRxChannel[channel].state)
104
        {
134
        {
105
        case sns_irTransceive_STATE_IDLE:
135
        case sns_irTransceive_STATE_IDLE:
106
            irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
136
            irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
107
            break;
137
            break;
108
 
138
 
109
        case sns_irTransceive_STATE_START_RECEIVE:
139
        case sns_irTransceive_STATE_START_RECEIVE:
110
            IrTransceiver_Reset(channel);
140
            IrTransceiver_ResetRx(channel);
111
            cli();
141
            cli();
112
            irRxChannel[channel].newData = FALSE;
142
            irRxChannel[channel].newData = FALSE;
113
            sei();
143
            sei();
114
            irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
144
            irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
115
            break;
145
            break;
116
       
146
       
117
        case sns_irTransceive_STATE_RECEIVING:
147
        case sns_irTransceive_STATE_RECEIVING:
118
            if (irRxChannel[channel].newData == TRUE) {
148
            if (irRxChannel[channel].newData == TRUE) {
-
 
149
                //TODO: move this line to the RX callback
119
                IrTransceiver_Disable(channel);
150
                IrTransceiver_DisableRx(channel);
120
                cli();
151
                cli();
121
                irRxChannel[channel].newData = FALSE;
152
                irRxChannel[channel].newData = FALSE;
122
                sei();
153
                sei();
123
 
154
 
124
                /* Let protocol driver parse and then send on CAN */
155
                /* Let protocol driver parse and then send on CAN */
125
                uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
156
                uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
126
                if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
157
                if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
Line 137... Line 168...
137
                else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
168
                else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
138
                {
169
                {
139
#if (sns_irTransceive_SEND_DEBUG==1)
170
#if (sns_irTransceive_SEND_DEBUG==1)
140
                    send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
171
                    send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
141
                    irRxChannel[channel].proto.timeout=300;
172
                    irRxChannel[channel].proto.timeout=300;
142
#endif
173
#endif
143
                }
174
                }
144
               
175
               
145
                /* Enable the receiver again */
176
                /* Enable the receiver again */
146
                IrTransceiver_Enable(channel);
177
                IrTransceiver_EnableRx(channel);
147
               
178
               
148
                irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
179
                irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
149
            }
180
            }
150
            break;
181
            break;
151
 
182
 
152
        case sns_irTransceive_STATE_START_PAUSE:
183
        case sns_irTransceive_STATE_START_PAUSE:
153
            /* set a timer so we can send release button event when no new IR is arriving */
184
            /* set a timer so we can send release button event when no new IR is arriving */
154
            Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
185
            Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
155
            irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
186
            irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
156
            break;
187
            break;
157
 
188
 
158
        case sns_irTransceive_STATE_PAUSING:
189
        case sns_irTransceive_STATE_PAUSING:
159
            /* reset timer if new IR arrived */
190
            /* reset timer if new IR arrived */
160
            if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnable(channel) == TRUE) {
191
            if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
161
                cli();
192
                cli();
162
                irRxChannel[channel].newData = FALSE;
193
                irRxChannel[channel].newData = FALSE;
163
                sei();
194
                sei();
164
                Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
195
                Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
165
            }
196
            }
166
       
197
       
167
            if (Timer_Expired(irRxChannel[channel].timerNum)) {
198
            if (Timer_Expired(irRxChannel[channel].timerNum)) {
168
                irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
199
                irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
169
            }
200
            }
170
            break;
201
            break;
171
 
202
 
172
        case sns_irTransceive_STATE_START_IDLE:
203
        case sns_irTransceive_STATE_START_IDLE:
173
            if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
204
            if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
174
                /* Send button release command on CAN */
205
                /* Send button release command on CAN */
Line 177... Line 208...
177
                irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
208
                irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
178
                irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
209
                irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
179
                irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
210
                irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
180
                irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
211
                irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
181
                irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
212
                irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
182
 
213
 
183
                StdCan_Put(&irTxMsg);
214
                StdCan_Put(&irTxMsg);
184
            }
215
            }
185
            irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
216
            irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
186
            break;
217
            break;
187
        }
218
        }
-
 
219
#endif
-
 
220
 
-
 
221
 
188
    }
222
    }
189
}
223
}
190
 
224
 
191
void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
225
void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
192
{
226
{