Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
179 eqlazer 1
/**
2
 * CAN LCD.
3
 * For HD44780- and KS0073-based LCD displays
4
 * This is the early version that only prints temperature sensor data.
5
 *
6
 * @date    2006-12-11
7
 * @author  Erik Larsson
8
 *
9
 * TODO Fix more types of output: relay status, mail recieved etc.
10
 * Make it configurable to use UART, because it's not necessary on the final PCB
11
 *
12
 */
13
 
256 eqlazer 14
#define DEBUG 0
179 eqlazer 15
 
16
/*-----------------------------------------------------------------------------
17
 * Includes
18
 *---------------------------------------------------------------------------*/
19
/* system files */
20
#include <avr/io.h>
21
#include <avr/interrupt.h>
22
#include <avr/wdt.h>
23
#include <stdio.h>
24
/* lib files */
25
#include <can.h>
26
#include <serial.h>
27
#include <timebase.h>
28
#include <lcd_HD44780.h>
196 eqlazer 29
#include <clcd20x4.h>
30
/* funcdefs */
31
#include <global_funcdefs.h>
32
#include <eqlazer_funcdefs.h>
179 eqlazer 33
 
34
/* defines */
196 eqlazer 35
#define BUFFER_SIZE 20
179 eqlazer 36
 
256 eqlazer 37
#define LCD_CMD_GET 0x1600
38
#define VIEW_LEFT 0x01
39
#define VIEW_RIGHT 0x02
40
#define VIEW_EDIT 0x03
41
 
42
 
196 eqlazer 43
uint8_t currentView = MAIN_VIEW;
179 eqlazer 44
 
196 eqlazer 45
// TODO ISR() för att använda optoencoders
46
ISR( PCINT2_vect ){
47
    // TODO do the magic stuff
48
    // vriden medurs currentView++
49
    // moturs currentView--
50
} // TODO lägga denna koden i ett eget lib?
179 eqlazer 51
 
52
/*-----------------------------------------------------------------------------
53
 * Main Program
54
 *---------------------------------------------------------------------------*/
55
int main(void) {
56
 
57
    char buffer[ BUFFER_SIZE ];
256 eqlazer 58
    uint8_t m, temperatureData[8], servoPosition[4], relayStatus[4], dimmerStatus[4],
59
            temperatureLength, servoLength;
179 eqlazer 60
 
196 eqlazer 61
    /*
62
     * Initialize optoencoders and buttons
63
     */
64
    /* Set as input and enable pull-up */ // TODO fixa rätt ingångar
65
    DDRD &= ~( (1<<DDD6)|(1<<DDD7) );
66
    PORTD |= (1<<PD6)|(1<<PD7);
67
    /* Enable IO-pin interrupt */
68
    PCICR |= (1<<PCIE1);
69
    /* Unmask PD6 and PD7 */
70
    PCMSK2 |= (1<<PCINT22)|(1<<PCINT23);
179 eqlazer 71
 
72
 
196 eqlazer 73
    Timebase_Init();
74
#if DEBUG
75
    Serial_Init();
76
#endif
77
    sei();
78
    lcd_init( LCD_DISP_ON );
179 eqlazer 79
    lcd_clrscr();
80
    lcd_puts("CAN LCD test\n");
81
    lcd_puts("CanInit... ");
196 eqlazer 82
    if (Can_Init() != CAN_OK) {
179 eqlazer 83
        lcd_puts("FAILED!\n");
196 eqlazer 84
    }else{
85
        lcd_puts("OK!\n");
86
    }
256 eqlazer 87
 
196 eqlazer 88
    uint32_t timeStamp = 0;
179 eqlazer 89
 
196 eqlazer 90
    Can_Message_t txMsg;
91
    Can_Message_t rxMsg;
92
    txMsg.Id = 0;
93
    txMsg.RemoteFlag = 0;
94
    txMsg.ExtendedFlag = 1;
95
 
179 eqlazer 96
    lcd_clrscr();
256 eqlazer 97
    printLCDview( MAIN_VIEW );
179 eqlazer 98
 
99
    /* main loop */
100
    while (1) {
101
        /* service the CAN routines */
102
        Can_Service();
103
 
104
        /* check if any messages have been received and print to uart */
105
        while (Can_Receive(&rxMsg) == CAN_OK) {
196 eqlazer 106
#if DEBUG
179 eqlazer 107
            printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
108
            printf("data={ ");
109
 
110
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
111
                printf("%x ", rxMsg.Data.bytes[i]);
112
            }
113
            printf("}\n");
196 eqlazer 114
#endif
115
            /* Get data from bus and store */
256 eqlazer 116
            if( rxMsg.Id == 0x1502 ){
196 eqlazer 117
                /* Temperature sensor data */
118
                for(m=0; m<rxMsg.DataLength; m++){
119
                    temperatureData[m] = rxMsg.Data.bytes[m];
256 eqlazer 120
                    temperatureLength = rxMsg.DataLength;
196 eqlazer 121
                }
122
            }else if( rxMsg.Id == 0x1201 ){
123
                /* Relay status (ON/OFF) */
124
                relayStatus[1] = rxMsg.Data.bytes[2];
256 eqlazer 125
            }else if( rxMsg.Id == 0x1301){
126
                /* Servo and blinds status */
127
                for( m=3; m<rxMsg.DataLength; m++ ){
128
                    servoPosition[m-3] = rxMsg.Data.bytes[m];
129
                    servoLength=rxMsg.DataLength-3;
130
                }
131
            }
132
            /* TODO Add extra messages here */
133
 
134
            /* Incoming command to change view */
135
            if(rxMsg.Id == LCD_CMD_GET){
136
                if(rxMsg.Data.bytes[0]== VIEW_LEFT){
137
                    if(currentView>MAIN_VIEW){
138
                        currentView--;
139
                    }
140
                }else if(rxMsg.Data.bytes[0]== VIEW_RIGHT){
141
                    if(currentView<SERVO_VIEW){ /* TODO increase value if you add views */
142
                        currentView++;
143
                    }
144
                }
145
 
146
                /* Print views skeleton and if existing its data */
147
                printLCDview( currentView );
148
                if(currentView == TEMPSENS_VIEW){
149
                    printLCDviewData( TEMPSENS_VIEW, temperatureData, temperatureLength);
150
                }else if(currentView == SERVO_VIEW){
151
                    printLCDviewData( SERVO_VIEW, servoPosition, servoLength);
152
                }
153
            }
196 eqlazer 154
        }
179 eqlazer 155
 
196 eqlazer 156
        // TODO använd optoencoders för att växla view
256 eqlazer 157
        // också med fjärrkontroll
179 eqlazer 158
 
196 eqlazer 159
    }
179 eqlazer 160
 
256 eqlazer 161
 
179 eqlazer 162
}