Subversion Repositories HomeAutomation

Rev

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

Rev 1473 Rev 1479
Line 2... Line 2...
2
#include "sns_irTransceive.h"
2
#include "sns_irTransceive.h"
3
 
3
 
-
 
4
struct {
-
 
5
    uint8_t             state;
-
 
6
    uint8_t             newData;
4
uint8_t state = sns_irTransceive_STATE_IDLE;
7
    uint16_t            rxbuf[MAX_NR_TIMES];
-
 
8
    uint8_t             rxlen;
-
 
9
    uint8_t             timerNum;
5
Ir_Protocol_Data_t proto;
10
    Ir_Protocol_Data_t  proto;
6
uint8_t len=0;
-
 
7
uint16_t rxbuffer[MAX_NR_TIMES];
11
} irRxChannel[sns_irTransceive_SUPPORTED_NUM_CHANNELS];
-
 
12
 
8
StdCan_Msg_t irTxMsg;
13
StdCan_Msg_t        irTxMsg;
9
 
-
 
10
 
14
 
11
#if (sns_irTransceive_SEND_DEBUG==1)
15
#if (sns_irTransceive_SEND_DEBUG==1)
12
void send_debug(uint16_t *buffer, uint8_t len) {
16
void send_debug(uint16_t *buffer, uint8_t len) {
13
 
-
 
-
 
17
    StdCan_Msg_t dbgIrTxMsg;
14
    /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
18
    /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
15
 
19
 
16
    StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
20
    StdCan_Set_class(dbgIrTxMsg.Header, CAN_MODULE_CLASS_SNS);
17
    StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
21
    StdCan_Set_direction(dbgIrTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
18
    irTxMsg.Length = 8;
22
    dbgIrTxMsg.Length = 8;
19
    irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
23
    dbgIrTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
20
    irTxMsg.Header.ModuleId = sns_irTransceive_ID;
24
    dbgIrTxMsg.Header.ModuleId = sns_irTransceive_ID;
21
    irTxMsg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRRAW;
25
    dbgIrTxMsg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRRAW;
22
    for (uint8_t i = 0; i < len>>2; i++) {
26
    for (uint8_t i = 0; i < len>>2; i++) {
23
        uint8_t index = i<<2;
27
        uint8_t index = i<<2;
24
 
28
 
25
        irTxMsg.Data[0] = (buffer[index]>>8)&0xff;
29
        dbgIrTxMsg.Data[0] = (buffer[index]>>8)&0xff;
26
        irTxMsg.Data[1] = (buffer[index]>>0)&0xff;
30
        dbgIrTxMsg.Data[1] = (buffer[index]>>0)&0xff;
27
        irTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
31
        dbgIrTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
28
        irTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
32
        dbgIrTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
29
        irTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
33
        dbgIrTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
30
        irTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
34
        dbgIrTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
31
        irTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
35
        dbgIrTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
32
        irTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
36
        dbgIrTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
33
               
37
               
34
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
38
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
35
        while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
39
        while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
36
        _delay_ms(1);
40
        _delay_ms(1);
37
    }
41
    }
38
   
42
   
39
    uint8_t lastpacketcnt = len&0x03;
43
    uint8_t lastpacketcnt = len&0x03;
40
    if (lastpacketcnt > 0) {
44
    if (lastpacketcnt > 0) {
41
        irTxMsg.Length = lastpacketcnt<<1;
45
        dbgIrTxMsg.Length = lastpacketcnt<<1;
42
        for (uint8_t i = 0; i < lastpacketcnt; i++) {
46
        for (uint8_t i = 0; i < lastpacketcnt; i++) {
43
            irTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
47
            dbgIrTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
44
            irTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
48
            dbgIrTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
45
        }
49
        }
46
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
50
        /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
47
        while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
51
        while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
48
        _delay_ms(1);
52
        _delay_ms(1);
49
    }
53
    }
50
 
54
 
51
}
55
}
52
#endif
56
#endif
53
 
57
 
-
 
58
void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t* buffer, uint8_t len)
-
 
59
{
54
void sns_irTransceive_Init(void)
60
    if (channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS)
55
{
61
    {
56
    ////////IrTransceiver_Init();
62
        irRxChannel[channel].newData = TRUE;
57
    proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
63
        irRxChannel[channel].rxlen = len;
58
 
64
    }
59
}
65
}
60
 
66
 
61
void sns_irTransceive_Process(void)
67
void sns_irTransceive_Init(void)
62
{
68
{
63
    uint8_t res;
69
gpio_set_out(EXP_A);
64
    switch (state)
70
gpio_set_pin(EXP_A);
65
    {
-
 
66
    case sns_irTransceive_STATE_IDLE:
-
 
67
        ////////////IrTransceiver_Receive_Start(rxbuffer);
-
 
68
        state = sns_irTransceive_STATE_START_RECEIVE;
-
 
69
        break;
-
 
70
 
-
 
71
    case sns_irTransceive_STATE_START_RECEIVE:
-
 
72
        state = sns_irTransceive_STATE_RECEIVING;
-
 
73
        break;
-
 
74
 
-
 
75
    case sns_irTransceive_STATE_RECEIVING:
-
 
76
        ///////////////res = IrTransceiver_Receive_Poll(&len);
-
 
77
        if ((res == IR_OK) && (len > 0)) {
-
 
78
            //låt protocols parsa och skicka sen på can
-
 
79
/*          uint8_t res2 = parseProtocol(rxbuffer, len, &proto);
-
 
80
            if (res2 == IR_OK && proto.protocol != IR_PROTO_UNKNOWN) {
-
 
81
                StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
71
    StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
82
                StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
72
    StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
83
                irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
73
    irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
84
                irTxMsg.Header.ModuleId = sns_irTransceive_ID;
74
    irTxMsg.Header.ModuleId = sns_irTransceive_ID;
85
                irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
75
    irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
86
                irTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
-
 
87
                irTxMsg.Data[1] = proto.protocol;
-
 
88
                irTxMsg.Data[2] = (proto.data>>24)&0xff;
-
 
89
                irTxMsg.Data[3] = (proto.data>>16)&0xff;
-
 
90
                irTxMsg.Data[4] = (proto.data>>8)&0xff;
-
 
91
                irTxMsg.Data[5] = proto.data&0xff;
-
 
92
                irTxMsg.Length = 6;
76
    irTxMsg.Length = 6;
93
                StdCan_Put(&irTxMsg);
-
 
94
            } else if (proto.protocol == IR_PROTO_UNKNOWN) {
-
 
95
 
77
 
-
 
78
    for (uint8_t i = 0; i < sns_irTransceive_SUPPORTED_NUM_CHANNELS; i++)
-
 
79
    {
-
 
80
        irRxChannel[i].state = sns_irTransceive_STATE_RECEIVING;
-
 
81
        irRxChannel[i].newData = FALSE;
-
 
82
        irRxChannel[i].rxlen = 0;
-
 
83
        irRxChannel[i].proto.timeout=0;
-
 
84
        irRxChannel[i].proto.data=0;
-
 
85
        irRxChannel[i].proto.repeats=0;
-
 
86
        irRxChannel[i].proto.protocol=0;
-
 
87
    }
-
 
88
    irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
-
 
89
    irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
-
 
90
    irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
-
 
91
   
-
 
92
    IrTransceiver_Init();
-
 
93
   
-
 
94
    IrTransceiver_Init_TX_Channel(0, irRxChannel[0].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
-
 
95
}
-
 
96
 
96
#if (sns_irTransceive_SEND_DEBUG==1)
97
void sns_irTransceive_Process(void)
-
 
98
{
-
 
99
    uint8_t res;
-
 
100
    for (uint8_t channel=0; channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS; channel++)
-
 
101
    {
-
 
102
//gpio_toggle_pin(EXP_A);
-
 
103
        switch (irRxChannel[channel].state)
-
 
104
        {
97
                send_debug(rxbuffer, len);
105
        case sns_irTransceive_STATE_IDLE:
-
 
106
            irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
-
 
107
            break;
-
 
108
 
-
 
109
        case sns_irTransceive_STATE_START_RECEIVE:
98
                proto.timeout=300;
110
            IrTransceiver_Reset(channel);
-
 
111
            cli();
-
 
112
            irRxChannel[channel].newData = FALSE;
99
#endif
113
            sei();
-
 
114
            irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
-
 
115
            break;
-
 
116
       
-
 
117
        case sns_irTransceive_STATE_RECEIVING:
-
 
118
            if (irRxChannel[channel].newData == TRUE) {
-
 
119
                IrTransceiver_Disable(channel);
-
 
120
                cli();
-
 
121
                irRxChannel[channel].newData = FALSE;
-
 
122
                sei();
-
 
123
 
-
 
124
                /* Let protocol driver parse and then send on CAN */
-
 
125
                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) {
-
 
127
                    irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
-
 
128
                    irTxMsg.Data[0] |= channel<<4;
-
 
129
                    irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
-
 
130
                    irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
-
 
131
                    irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
-
 
132
                    irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
-
 
133
                    irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
-
 
134
 
-
 
135
                    StdCan_Put(&irTxMsg);
100
            }
136
                }
-
 
137
                else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
101
*/         
138
                {
102
#if (sns_irTransceive_SEND_DEBUG==1)
139
#if (sns_irTransceive_SEND_DEBUG==1)
103
                send_debug(rxbuffer, len);
140
                    send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
104
                proto.timeout=300;
141
                    irRxChannel[channel].proto.timeout=300;
105
#endif
142
#endif
106
 
-
 
107
            state = sns_irTransceive_STATE_START_PAUSE;
-
 
108
        }
143
                }
-
 
144
               
-
 
145
                /* Enable the receiver again */
-
 
146
                IrTransceiver_Enable(channel);
-
 
147
               
-
 
148
                irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
-
 
149
            }
109
        break;
150
            break;
110
 
151
 
111
    case sns_irTransceive_STATE_START_PAUSE:
152
        case sns_irTransceive_STATE_START_PAUSE:
112
        //////////////IrTransceiver_Receive_Start(rxbuffer);
153
            /* set a timer so we can send release button event when no new IR is arriving */
113
        Timer_SetTimeout(sns_irTransceive_REPEATE_TIMER, proto.timeout, TimerTypeOneShot, 0);
154
            Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
114
        state = sns_irTransceive_STATE_PAUSING;
155
            irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
115
        break;
156
            break;
116
 
157
 
117
    case sns_irTransceive_STATE_PAUSING:
158
        case sns_irTransceive_STATE_PAUSING:
118
        //resetta timebase-var om ir finns
159
            /* reset timer if new IR arrived */
119
        ////////////////res = IrTransceiver_Receive_Poll(&len);
-
 
120
        if (res == IR_NOT_FINISHED || res == IR_OK) {
-
 
121
            Timer_SetTimeout(sns_irTransceive_REPEATE_TIMER, proto.timeout, TimerTypeOneShot, 0);
160
            if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnable(channel) == TRUE) {
122
        }
161
                cli();
123
        if (res == IR_OK) {
162
                irRxChannel[channel].newData = FALSE;
124
            /* start a new reception */
163
                sei();
125
            ///////////IrTransceiver_Receive_Start(rxbuffer);
164
                Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
126
        }
165
            }
127
       
166
       
128
        if (Timer_Expired(sns_irTransceive_REPEATE_TIMER)) {
167
            if (Timer_Expired(irRxChannel[channel].timerNum)) {
129
            state = sns_irTransceive_STATE_START_IDLE;
168
                irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
130
        }
169
            }
131
        break;
170
            break;
132
 
171
 
133
    case sns_irTransceive_STATE_START_IDLE:
172
        case sns_irTransceive_STATE_START_IDLE:
134
        if (proto.protocol != IR_PROTO_UNKNOWN) {
173
            if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
135
            //skicka på can
174
                /* Send button release command on CAN */
136
            irTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
175
                irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
-
 
176
                irTxMsg.Data[0] |= channel<<4;
-
 
177
                irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
-
 
178
                irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
-
 
179
                irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
-
 
180
                irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
-
 
181
                irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
-
 
182
 
137
            StdCan_Put(&irTxMsg);
183
                StdCan_Put(&irTxMsg);
138
        }
184
            }
139
        state = sns_irTransceive_STATE_IDLE;
185
            irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
140
        break;
186
            break;
141
 
-
 
142
    }
187
        }
143
 
188
    }
144
}
189
}
145
 
190
 
146
void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
191
void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
147
{
192
{
148
    /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
193
    /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&