Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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