Subversion Repositories HomeAutomation

Rev

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

Rev 1266 Rev 1726
Line 7... Line 7...
7
};
7
};
8
 
8
 
9
struct chn_ChnMaster_listener_t chn_ChnMaster_listeners[ chn_ChnMaster_MAX_LISTENERS ];
9
struct chn_ChnMaster_listener_t chn_ChnMaster_listeners[ chn_ChnMaster_MAX_LISTENERS ];
10
uint8_t chn_ChnMaster_listenCount = 0;
10
uint8_t chn_ChnMaster_listenCount = 0;
11
 
11
 
-
 
12
struct chn_ChnMaster_buffer_t {
-
 
13
    uint16_t channel_id;
-
 
14
    uint16_t value;
-
 
15
} chn_ChnMaster_buffer[ chn_ChnMaster_BUFFER_SIZE ];
12
 
16
 
-
 
17
volatile uint8_t chn_ChnMaster_buf_in_pos = 0; // next to write
-
 
18
volatile uint8_t chn_ChnMaster_buf_out_pos = 0; // next to read
13
 
19
 
14
#ifdef chn_ChnMaster_USEEEPROM
20
#ifdef chn_ChnMaster_USEEEPROM
15
#include "chn_ChnMaster_eeprom.h"
21
#include "chn_ChnMaster_eeprom.h"
16
struct eeprom_chn_ChnMaster EEMEM eeprom_chn_ChnMaster =
22
struct eeprom_chn_ChnMaster EEMEM eeprom_chn_ChnMaster =
17
{
23
{
Line 21... Line 27...
21
        0x1234    // y
27
        0x1234    // y
22
    },
28
    },
23
    0    // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
29
    0    // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
24
};
30
};
25
#endif
31
#endif
26
 
32
 
27
void chn_ChnMaster_RegisterListener( uint16_t channel_id, void (*update_func)(uint16_t, uint16_t) ) {
33
void chn_ChnMaster_RegisterListener( uint16_t channel_id, void (*update_func)(uint16_t, uint16_t) ) {
28
    chn_ChnMaster_listeners[ chn_ChnMaster_listenCount ].update_func = update_func;
34
    chn_ChnMaster_listeners[ chn_ChnMaster_listenCount ].update_func = update_func;
29
    chn_ChnMaster_listeners[ chn_ChnMaster_listenCount ].channel_id = channel_id;
35
    chn_ChnMaster_listeners[ chn_ChnMaster_listenCount ].channel_id = channel_id;
30
    chn_ChnMaster_listenCount++;
36
    chn_ChnMaster_listenCount++;
31
}
37
}
Line 41... Line 47...
41
    txMsg.Length = 2;
47
    txMsg.Length = 2;
42
    txMsg.Data[0] = (value     >>8)&0xFF;
48
    txMsg.Data[0] = (value     >>8)&0xFF;
43
    txMsg.Data[1] = (value        )&0xFF;
49
    txMsg.Data[1] = (value        )&0xFF;
44
 
50
 
45
    while( StdCan_Put(&txMsg) != StdCan_Ret_OK );
51
    while( StdCan_Put(&txMsg) != StdCan_Ret_OK );
46
}
52
}
47
 
53
 
48
 
54
 
49
void chn_ChnMaster_Init(void)
55
void chn_ChnMaster_Init(void) {
50
{
-
 
51
#ifdef chn_ChnMaster_USEEEPROM
-
 
52
    if (EEDATA_OK)
-
 
53
    {
-
 
54
      ///TODO: Use stored data to set initial values for the module
-
 
55
      blablaX = eeprom_read_byte(EEDATA.x);
-
 
56
      blablaY = eeprom_read_word(EEDATA.y);
-
 
57
    } else
-
 
58
    {    //The CRC of the EEPROM is not correct, store default values and update CRC
-
 
59
      eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
-
 
60
      eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC);
-
 
61
      EEDATA_UPDATE_CRC;
-
 
62
    }
-
 
63
#endif  
-
 
64
    ///TODO: Initialize hardware etc here
-
 
65
 
-
 
66
    // to use PCINt lib, call this function: (the callback function look as a timer callback function)
-
 
67
    // Pcint_SetCallbackPin(chn_ChnMaster_PCINT, EXP_C , &chn_ChnMaster_pcint_callback);
-
 
68
 
-
 
69
}
56
}
70
 
57
 
71
void chn_ChnMaster_Process(void)
58
void chn_ChnMaster_Process(void)
72
{
59
{
-
 
60
    uint16_t value;
-
 
61
    uint16_t channel_id;
-
 
62
    uint8_t i;
-
 
63
    while( chn_ChnMaster_buf_out_pos != chn_ChnMaster_buf_in_pos ) {
-
 
64
        value      = chn_ChnMaster_buffer[chn_ChnMaster_buf_out_pos].value;
-
 
65
        channel_id = chn_ChnMaster_buffer[chn_ChnMaster_buf_out_pos].channel_id;
-
 
66
 
-
 
67
        for( i=0; i<chn_ChnMaster_listenCount; i++ ) {
-
 
68
            if( chn_ChnMaster_listeners[i].channel_id == channel_id ) {
-
 
69
                (*chn_ChnMaster_listeners[i].update_func)( channel_id, value );
-
 
70
            }
-
 
71
        }
-
 
72
 
-
 
73
        chn_ChnMaster_buf_out_pos = (chn_ChnMaster_buf_out_pos+1)%chn_ChnMaster_BUFFER_SIZE;
-
 
74
    }
73
    ///TODO: Stuff that needs doing is done here
75
    ///TODO: Stuff that needs doing is done here
74
}
76
}
75
 
77
 
76
void chn_ChnMaster_HandleMessage(StdCan_Msg_t *rxMsg)
78
void chn_ChnMaster_HandleMessage(StdCan_Msg_t *rxMsg)
77
{
79
{
78
    uint8_t i,j;
80
    uint8_t j;
79
    uint16_t channel_id;
81
    uint16_t channel_id;
80
    uint16_t value;
82
    uint16_t value;
81
    if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_CHN)
83
    if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_CHN)
82
    {
84
    {
83
        if( (rxMsg->Id & (1L<<24)) == 0 ) { /* Value */
-
 
84
            channel_id = (rxMsg->Id >> 8) & 0xFFFF;
85
        channel_id = (rxMsg->Id >> 8) & 0xFFFF;
85
 
-
 
-
 
86
        if( (rxMsg->Id & (1L<<24)) == 0 ) { /* Value */
86
            for( j=0; j<(rxMsg->Length&~1); j+=2 ) {
87
            for( j=0; j<(rxMsg->Length&~1); j+=2 ) {
87
                value      = (rxMsg->Data[j]<<8) | rxMsg->Data[j+1];
88
                value      = (rxMsg->Data[j]<<8) | rxMsg->Data[j+1];
-
 
89
/*
88
                for( i=0; i<chn_ChnMaster_listenCount; i++ ) {
90
                for( i=0; i<chn_ChnMaster_listenCount; i++ ) {
89
                    if( chn_ChnMaster_listeners[i].channel_id == channel_id ) {
91
                    if( chn_ChnMaster_listeners[i].channel_id == channel_id ) {
90
                        (*chn_ChnMaster_listeners[i].update_func)( channel_id, value );
92
                        (*chn_ChnMaster_listeners[i].update_func)( channel_id, value );
91
                    }
93
                    }
92
                }
94
                }
-
 
95
                */
-
 
96
                chn_ChnMaster_buffer[chn_ChnMaster_buf_in_pos].channel_id = channel_id;
-
 
97
                chn_ChnMaster_buffer[chn_ChnMaster_buf_in_pos].value = value;
-
 
98
                chn_ChnMaster_buf_in_pos = (chn_ChnMaster_buf_in_pos+1)%chn_ChnMaster_BUFFER_SIZE;
93
                channel_id++;
99
                channel_id++;
94
            }
100
            }
95
        } else { /* Metadata */
101
        } else { /* Metadata */
96
        }
102
        }
97
    }
103
    }