Subversion Repositories HomeAutomation

Rev

Rev 587 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 587 Rev 588
1
#include <inttypes.h>
1
#include <inttypes.h>
2
#include <bios.h>
2
#include <bios.h>
3
#include <config.h>
3
#include <config.h>
4
#include "channel.h"
4
#include "channel.h"
5
 
5
 
6
Channel_Info_t channel_defs[ CHANNEL_COUNT ];
6
Channel_Info_t channel_defs[ CHANNEL_COUNT ];
7
 
7
 
8
uint8_t Channel_Init( void ) {
8
uint8_t Channel_Init( void ) {
9
    Can_Message_t txMsg;
9
    Can_Message_t txMsg;
10
 
10
 
11
    uint8_t i;
11
    uint8_t i;
12
 
12
 
13
    /* Reset channel_defs */
13
    /* Reset channel_defs */
14
    for(i=0;i<CHANNEL_COUNT;i++) {
14
    for(i=0;i<CHANNEL_COUNT;i++) {
15
        channel_defs[i].channel = 0;
15
        channel_defs[i].channel = 0;
16
        channel_defs[i].value = 0;
16
        channel_defs[i].value = 0;
17
    }
17
    }
18
 
18
 
19
    /* CHANNEL_SUBCLASS_IDENTIFY */
19
    /* CHANNEL_SUBCLASS_IDENTIFY */
20
 
20
 
-
 
21
    txMsg.Id    = (CAN_CHANNEL                  << CAN_SHIFT_CLASS)
21
    txMsg.Id = (CAN_CHANNEL<<CAN_SHIFT_CLASS) | (CHANNEL_SUBCLASS_IDENTIFY<<CHANNEL_SHIFT_SUBCLASS) | NODE_ID;
22
                | (CHANNEL_SUBCLASS_IDENTIFY    << CHANNEL_SHIFT_SUBCLASS)
-
 
23
                | (NODE_ID                      << CHANNEL_SHIFT_NODEID);
22
    txMsg.ExtendedFlag = 1;
24
    txMsg.ExtendedFlag = 1;
23
    txMsg.RemoteFlag = 0;
25
    txMsg.RemoteFlag = 0;
24
 
26
 
25
    txMsg.DataLength = 1;
27
    txMsg.DataLength = 1;
26
    txMsg.Data.bytes[0] = CHANNEL_COUNT;
28
    txMsg.Data.bytes[0] = CHANNEL_COUNT;
27
 
29
 
28
    BIOS_CanSend( &txMsg );
30
    BIOS_CanSend( &txMsg );
29
 
31
 
30
 
32
 
31
 
33
 
32
    return CHANNEL_OK;
34
    return CHANNEL_OK;
33
}
35
}
34
 
36
 
35
uint8_t Channel_HandleMsg(Can_Message_t *msg) {
37
uint8_t Channel_HandleMsg(Can_Message_t *msg) {
36
    uint8_t class;
38
    uint8_t class;
37
    uint8_t subclass;
39
    uint8_t subclass;
38
    uint8_t node_id;
40
    uint8_t node_id;
39
 
41
 
40
    uint16_t channel;
42
    uint16_t channel;
41
    uint16_t channel_mask;
43
    uint16_t channel_mask;
42
 
44
 
43
    uint8_t i,j;
45
    uint8_t i,j;
44
 
46
 
45
    if( msg->ExtendedFlag!=1) {
47
    if( msg->ExtendedFlag!=1) {
46
        /* Channels only availible in Extended mode */
48
        /* Channels only availible in Extended mode */
47
        return CHANNEL_FAIL;
49
        return CHANNEL_FAIL;
48
    }
50
    }
49
    class = ( msg->Id & CAN_MASK_CLASS ) >> CAN_SHIFT_CLASS;
51
    class = ( msg->Id & CAN_MASK_CLASS ) >> CAN_SHIFT_CLASS;
50
    if( class != CAN_CHANNEL ) {
52
    if( class != CAN_CHANNEL ) {
51
        /* Is not a channel-message */
53
        /* Is not a channel-message */
52
        return CHANNEL_FAIL;
54
        return CHANNEL_FAIL;
53
    }
55
    }
54
 
56
 
55
    subclass    = ( msg->Id & CHANNEL_MASK_SUBCLASS ) >> CHANNEL_SHIFT_SUBCLASS;
57
    subclass    = ( msg->Id & CHANNEL_MASK_SUBCLASS ) >> CHANNEL_SHIFT_SUBCLASS;
56
    channel     = ( msg->Id & CHANNEL_MASK_CHANNEL  ) >> CHANNEL_SHIFT_CHANNEL;
58
    channel     = ( msg->Id & CHANNEL_MASK_CHANNEL  ) >> CHANNEL_SHIFT_CHANNEL;
57
    node_id     = ( msg->Id & CHANNEL_MASK_NODEID   ) >> CHANNEL_SHIFT_NODEID;
59
    node_id     = ( msg->Id & CHANNEL_MASK_NODEID   ) >> CHANNEL_SHIFT_NODEID;
58
 
60
 
59
    switch( subclass ) {
61
    switch( subclass ) {
60
        case CHANNEL_SUBCLASS_DATA:
62
        case CHANNEL_SUBCLASS_DATA:
61
            channel_mask = 0x0FFF & ~((msg->DataLength>>1)-1);
63
            channel_mask = 0x0FFF & ~((msg->DataLength>>1)-1);
62
            for( i=0; i<CHANNEL_COUNT; i++ ) {
64
            for( i=0; i<CHANNEL_COUNT; i++ ) {
63
                if( ( (channel_defs[i].channel) & channel_mask ) == channel ) {
65
                if( ( (channel_defs[i].channel) & channel_mask ) == channel ) {
64
                    channel_defs[i].value = msg->Data.words[ channel_defs[i].channel - channel ];
66
                    channel_defs[i].value = msg->Data.words[ channel_defs[i].channel - channel ];
65
                    Channel_Callback_Value( i, channel_defs[i].value );
67
                    Channel_Callback_Value( i, channel_defs[i].value );
66
                }
68
                }
67
            }
69
            }
68
            return CHANNEL_OK;
70
            return CHANNEL_OK;
69
 
71
 
70
 
72
 
71
 
73
 
72
/* Node-to-macro
74
/* Node-to-macro
73
        case CHANNEL_SUBCLASS_IDENTIFY:
75
        case CHANNEL_SUBCLASS_IDENTIFY:
74
            break;
76
            break;
75
*/
77
*/
76
 
78
 
77
        case CHANNEL_SUBCLASS_CHN_BIND:
79
        case CHANNEL_SUBCLASS_CHN_BIND:
78
            if( NODE_ID == node_id && msg->Data.bytes[0] < CHANNEL_COUNT ) {
80
            if( NODE_ID == node_id && msg->Data.bytes[0] < CHANNEL_COUNT ) {
79
                channel_defs[ msg->Data.bytes[0] ].channel = channel;
81
                channel_defs[ msg->Data.bytes[0] ].channel = channel;
80
                return CHANNEL_OK;
82
                return CHANNEL_OK;
81
            }
83
            }
82
            return CHANNEL_FAIL;
84
            return CHANNEL_FAIL;
83
 
85
 
84
 
86
 
85
 
87
 
86
 
-
 
-
 
88
#if CHANNEL_ENABLE_NAME
87
        case CHANNEL_SUBCLASS_CHN_NAME:
89
        case CHANNEL_SUBCLASS_CHN_NAME:
88
            for( i=0; i<CHANNEL_COUNT; i++ ) {
90
            for( i=0; i<CHANNEL_COUNT; i++ ) {
89
                if( channel_defs[i].channel == channel ) {
91
                if( channel_defs[i].channel == channel ) {
90
                    for( j=0; j<msg->DataLength; j++) {
92
                    for( j=0; j<msg->DataLength; j++) {
91
                        channel_defs[i].name[j]=msg->Data.bytes[j];
93
                        channel_defs[i].name[j]=msg->Data.bytes[j];
92
                    }
94
                    }
93
                    channel_defs[i].name[j]='\0';
95
                    channel_defs[i].name[j]='\0';
94
                }
96
                }
95
            }
97
            }
96
            return CHANNEL_OK;
98
            return CHANNEL_OK;
-
 
99
#endif
97
 
100
 
98
 
-
 
99
 
-
 
-
 
101
#if CHANNEL_ENABLE_TYPE
100
        case CHANNEL_SUBCLASS_CHN_TYPE:
102
        case CHANNEL_SUBCLASS_CHN_TYPE:
101
            for( i=0; i<CHANNEL_COUNT; i++ ) {
103
            for( i=0; i<CHANNEL_COUNT; i++ ) {
102
                if( channel_defs[i].channel == channel ) {
104
                if( channel_defs[i].channel == channel ) {
103
                    channel_defs[i].type_k = msg->Data.words[0];
105
                    channel_defs[i].type_k = msg->Data.words[0];
104
                    channel_defs[i].type_m = msg->Data.words[1];
106
                    channel_defs[i].type_m = msg->Data.words[1];
105
 
-
 
-
 
107
#if CHANNEL_ENABLE_TYPE_NAME
106
                    for( j=0; j<msg->DataLength-4; j++) {
108
                    for( j=0; j<msg->DataLength-4; j++) {
107
                        channel_defs[i].type[j]=msg->Data.bytes[j+4];
109
                        channel_defs[i].type[j]=msg->Data.bytes[j+4];
108
                    }
110
                    }
109
                    channel_defs[i].type[j]='\0';
111
                    channel_defs[i].type[j]='\0';
-
 
112
#endif
110
                }
113
                }
111
            }
114
            }
112
            break;
115
            break;
-
 
116
#endif
113
    }
117
    }
114
 
118
 
115
    return CHANNEL_FAIL;
119
    return CHANNEL_FAIL;
116
}
120
}
117
 
121
 
118
void Channel_Bind( uint8_t id, uint8_t channel ) {
122
void Channel_Bind( uint8_t id, uint8_t channel ) {
119
    channel_defs[id].channel = channel;
123
    channel_defs[id].channel = channel;
120
}
124
}
121
 
125
 
122
uint16_t Channel_GetValue( uint8_t id ) {
126
uint16_t Channel_GetValue( uint8_t id ) {
123
    return channel_defs[id].value;
127
    return channel_defs[id].value;
-
 
128
}
-
 
129
 
-
 
130
uint8_t Channel_SetValue( uint8_t id, uint16_t value ) {
-
 
131
    uint16_t channel;
-
 
132
    Can_Message_t txMsg;
-
 
133
 
-
 
134
    channel = channel_defs[id].channel;
-
 
135
    if( channel == 0 ) return CHANNEL_FAIL;
-
 
136
 
-
 
137
    txMsg.Id    = (CAN_CHANNEL              << CAN_SHIFT_CLASS)
-
 
138
                | (CHANNEL_SUBCLASS_DATA    << CHANNEL_SHIFT_SUBCLASS)
-
 
139
                | (channel                  << CHANNEL_SHIFT_CHANNEL)
-
 
140
                | (NODE_ID                  << CHANNEL_SHIFT_NODEID);
-
 
141
 
-
 
142
    txMsg.ExtendedFlag = 1;
-
 
143
    txMsg.RemoteFlag = 0;
-
 
144
    txMsg.DataLength = 2;
-
 
145
    txMsg.Data.words[0] = value;
-
 
146
 
-
 
147
    BIOS_CanSend( &txMsg );
-
 
148
 
-
 
149
    channel_defs[id].value = value;
-
 
150
 
-
 
151
#if CHANNEL_SET_SELF
-
 
152
    Channel_Callback_Value( id, value );
-
 
153
#endif
-
 
154
 
-
 
155
    return CHANNEL_OK;
124
}
156
}
125
 
157