Subversion Repositories HomeAutomation

Rev

Rev 448 | Rev 481 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
179 eqlazer 1
/**
349 eqlazer 2
 * CanSimpleCLCD. Simple version.
179 eqlazer 3
 * For HD44780- and KS0073-based LCD displays
4
 *
349 eqlazer 5
 * Contains no intelligence. It just prints what it is told to.
6
 *
179 eqlazer 7
 * @date    2006-12-11
8
 * @author  Erik Larsson
9
 *
10
 */
11
 
12
/*-----------------------------------------------------------------------------
13
 * Includes
14
 *---------------------------------------------------------------------------*/
15
/* system files */
16
#include <avr/io.h>
17
#include <avr/interrupt.h>
18
#include <stdio.h>
19
/* lib files */
332 eqlazer 20
#include <bios.h>
179 eqlazer 21
#include <lcd_HD44780.h>
196 eqlazer 22
/* funcdefs */
348 eqlazer 23
#include <funcdefs.h>
179 eqlazer 24
 
349 eqlazer 25
/*-----------------------------------------------------------------------------
26
 * Defines
27
 *---------------------------------------------------------------------------*/
344 eqlazer 28
#define SIZE_X	20
29
#define SIZE_Y	4
348 eqlazer 30
#define SIZE_LCD LCD_SIZE_20x4
344 eqlazer 31
 
348 eqlazer 32
#define BUFFER_SIZE SIZE_X
332 eqlazer 33
#define TRUE 1
34
#define FALSE 0
179 eqlazer 35
 
344 eqlazer 36
#define LEFT	0x01
37
#define RIGHT	0x02
38
 
39
Can_Message_t txMsg;
256 eqlazer 40
 
348 eqlazer 41
char incoming_text[ BUFFER_SIZE ];
42
uint8_t msg_received = FALSE;
335 eqlazer 43
 
44
uint8_t rot_lastdir = 0,
45
		rot_laststate = 0;
46
 
349 eqlazer 47
char buffer[ BUFFER_SIZE ];
344 eqlazer 48
 
348 eqlazer 49
void send_dimensions();
50
 
344 eqlazer 51
void can_receive(Can_Message_t *msg){
463 eqlazer 52
Can_Message_t txMsg;
348 eqlazer 53
	uint32_t act;
54
	uint8_t m, act_type, lcd_action, lcd_size;
55
	if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
56
		act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
57
		act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
58
		lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
59
		lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
60
	}
61
 
62
	if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
63
		switch( lcd_action ){
64
			case LCD_ACTION_CLR:
65
					// Clear LCD
66
					lcd_clrscr();
67
				break;
68
			case LCD_ACTION_CURS:
69
					// Move cursor
70
					lcd_gotoxy( msg->Data.bytes[0],msg->Data.bytes[1] );
71
				break;
72
			case LCD_ACTION_GET_SIZE:
73
					// Request LCD dimension
74
					send_dimensions();
75
				break;
76
			case LCD_ACTION_TEXT:
77
				// Print text to LCD
78
				for(m=0;m<msg->DataLength;m++){
79
					lcd_putc( (char)msg->Data.bytes[m] );
80
				}
81
				break;
82
			case LCD_ACTION_SET_CONT:
83
				// Set contrast
84
				if(msg->DataLength == 1){
85
					OCR0B = msg->Data.bytes[0];
86
				}
87
				break;
88
			case LCD_ACTION_SET_BLIGHT:
89
				// Set backlight
90
				if(msg->DataLength == 1){
91
					OCR1AL = msg->Data.bytes[0];
92
				}
93
				break;
94
			case LCD_ACTION_GET_CONT:
95
				// Get contrast
96
				txMsg.DataLength = 1;
97
				txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
98
				txMsg.Data.bytes[0] = OCR0B;
463 eqlazer 99
				BIOS_CanSend(&txMsg);
348 eqlazer 100
				break;
101
			case LCD_ACTION_GET_BLIGHT:
102
				// Get backlight
103
				txMsg.DataLength = 1;
104
				txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
105
				txMsg.Data.bytes[0] = OCR1AL;
463 eqlazer 106
				BIOS_CanSend(&txMsg);
348 eqlazer 107
				break;
108
			default:
109
				// Take over the world
110
				break;
111
		}
112
	}
344 eqlazer 113
}
114
 
463 eqlazer 115
ISR( PCINT2_vect ){ // For ROTENC_A and ROTENC_B
116
/*	uint8_t rot_data = 0;
256 eqlazer 117
 
348 eqlazer 118
	txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
344 eqlazer 119
	txMsg.DataLength=2;
120
 
463 eqlazer 121
	if(PINB&(1<<PB0)){
335 eqlazer 122
		rot_data |= 0x01;
123
	}
463 eqlazer 124
	if(PIND&(1<<PD2)){
125
		rot_data |= 0x02;
126
	}
127
 
128
	if( rot_data==0 || rot_data==3 ){
129
		if( rot_data==0 && rot_laststate!=rot_data ){
130
			if( rot_lastdir&0x01 ){
131
				// Moving right
132
				txMsg.Data.bytes[0]=RIGHT;
133
				txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
134
				BIOS_CanSend(&txMsg);
135
				// For testing TODO remove
136
				if(OCR1A<0xFF){
137
					OCR1A++;
138
				}
139
				if(OCR0B<0xFF){
140
					OCR0B++;
141
				}
142
			}else{
143
				// Moving left
144
				txMsg.Data.bytes[0]=LEFT;
145
				txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
146
				BIOS_CanSend(&txMsg);
147
				// For testing TODO remove
148
				if(OCR1A>0){
149
					OCR1A--;
150
				}
151
				if(OCR0B>0){
152
					OCR0B--;
153
				}
154
			}
155
		}
156
		rot_laststate = rot_data;
157
	}else{
158
		rot_lastdir = rot_data;
159
	}*/
160
	rotenc();
161
}
162
 
163
ISR( PCINT0_vect ){ // For ROTENC_BTN
164
//	txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x02<<SNS_ID_BITS )| NODE_ID;
165
//	txMsg.Data.bytes[0] = (PINB&(1<<PB7))?0:1; // Check if buton is pressed or not
166
//	txMsg.DataLength=1;
167
//	BIOS_CanSend(&txMsg);
168
	rotenc();
169
}
170
 
171
void rotenc(){
172
	uint8_t rot_data = 0;
173
Can_Message_t txMsg;
174
	txMsg.ExtendedFlag=1;
175
	txMsg.RemoteFlag=0;
176
	txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
177
	txMsg.DataLength=2;
178
 
344 eqlazer 179
	if(PINB&(1<<PB0)){
463 eqlazer 180
		rot_data |= 0x01;
181
	}
182
	if(PIND&(1<<PD2)){
335 eqlazer 183
		rot_data |= 0x02;
184
	}
185
 
344 eqlazer 186
	if( rot_data==0 || rot_data==3 ){
187
		if( rot_data==0 && rot_laststate!=rot_data ){
188
			if( rot_lastdir&0x01 ){
189
				// Moving right
190
				txMsg.Data.bytes[0]=RIGHT;
463 eqlazer 191
				txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
192
	txMsg.DataLength=2;
193
				BIOS_CanSend(&txMsg);
348 eqlazer 194
				// For testing TODO remove
195
				if(OCR1A<0xFF){
196
					OCR1A++;
197
				}
463 eqlazer 198
				if(OCR0B<0xFF){
199
					OCR0B++;
200
				}
344 eqlazer 201
			}else{
202
				// Moving left
203
				txMsg.Data.bytes[0]=LEFT;
463 eqlazer 204
				txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
205
	txMsg.DataLength=2;
206
				BIOS_CanSend(&txMsg);
348 eqlazer 207
				// For testing TODO remove
208
				if(OCR1A>0){
209
					OCR1A--;
210
				}
463 eqlazer 211
				if(OCR0B>0){
212
					OCR0B--;
213
				}
335 eqlazer 214
			}
215
		}
344 eqlazer 216
		rot_laststate = rot_data;
217
	}else{
335 eqlazer 218
		rot_lastdir = rot_data;
219
	}
332 eqlazer 220
}
179 eqlazer 221
 
332 eqlazer 222
 
179 eqlazer 223
/*-----------------------------------------------------------------------------
224
 * Main Program
225
 *---------------------------------------------------------------------------*/
226
int main(void) {
227
 
335 eqlazer 228
	/*
229
	 * Initialize rotaryencoders and buttons
230
	 */
231
	/* Set as input */
344 eqlazer 232
	DDRD &= ~(1<<DDD2);
233
	DDRB &= ~((1<<DDB7)|(1<<DDB0));
234
	/* Enable pull-up */
235
	PORTD |= (1<<PD2);
236
	PORTB |= (1<<PB7)|(1<<PB0);
335 eqlazer 237
	/* Enable IO-pin interrupt */
344 eqlazer 238
	PCICR |= (1<<PCIE2)|(1<<PCIE0);
239
	/* Unmask PB7, PB0 and PD2 */
240
	PCMSK2 |= (1<<PCINT18);
241
	PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
179 eqlazer 242
 
348 eqlazer 243
	/* Contrast */
244
	TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
245
	TCCR0B |= (1<<CS00);
246
	OCR0B = 0x20;
247
	DDRD |= (1<<DDD5);
248
 
249
	/* Backlight */
250
	TCCR1A |= (1<<COM1A1)|(1<<WGM11);
251
	TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
252
	ICR1 = 0xFF; // Make it 8 bits
253
	OCR1A = 0x20;
254
	DDRB |= (1<<DDB1);
255
 
463 eqlazer 256
	Can_Message_t txMsg;
344 eqlazer 257
	txMsg.ExtendedFlag=1;
258
	txMsg.RemoteFlag=0;
259
 
335 eqlazer 260
	sei();
463 eqlazer 261
	BIOS_CanCallback = &can_receive;
256 eqlazer 262
 
332 eqlazer 263
	lcd_init( LCD_DISP_ON );
264
	lcd_clrscr();
348 eqlazer 265
	lcd_puts("HomeAutomation\n");
179 eqlazer 266
 
344 eqlazer 267
	send_dimensions();
348 eqlazer 268
	lcd_puts("Very nice!\n");
179 eqlazer 269
 
270
    /* main loop */
332 eqlazer 271
	while(1){
272
        /* check if any messages have been received */
273
		if(msg_received){
344 eqlazer 274
 
334 eqlazer 275
			msg_received = FALSE;
276
		}
463 eqlazer 277
		lcd_puts("test");
332 eqlazer 278
	}
179 eqlazer 279
}
344 eqlazer 280
 
281
void send_dimensions(){
463 eqlazer 282
	Can_Message_t txMsg;
283
	txMsg.ExtendedFlag=1;
284
	txMsg.RemoteFlag=0;
344 eqlazer 285
	txMsg.Id=0xA000001;
286
	txMsg.Data.bytes[0] = SIZE_X;
287
	txMsg.Data.bytes[1] = SIZE_Y;
288
	txMsg.DataLength = 2;
463 eqlazer 289
	BIOS_CanSend(&txMsg);
344 eqlazer 290
}