-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroom_beta.c
More file actions
565 lines (481 loc) · 10.2 KB
/
groom_beta.c
File metadata and controls
565 lines (481 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <string.h>
#include <groom/button.h>
#include "groom/usart.h"
#include "groom/com.h"
#define FOSC 9830400 // Clock frequency = Oscillator freq.
#define BAUD 9600 // UART0 baud rate
#define MYUBRR FOSC/16/BAUD-1 // Value for UBRR0 register
#define BDIV (FOSC / 100000 - 16) / 2 + 1 // Puts I2C rate just below 100kHz
volatile int i=0;
volatile uint8_t buffer[20];
volatile uint8_t StrRxFlag=0;
volatile char c='0';
char buf[256]; //string for thermometer
volatile uint8_t interruptstate=0; //interrupt state
volatile uint8_t buttonstate=0;
//Output Relay Swap Buffers
int thermoBuf[3] = {0, 0, 0};
int lightBuf[3] = {0, 0, 0};
int blindBuf[3] = {0, 0, 0};
int *currentBuf = thermoBuf;
//
//I/O init
void io_pin_init()
{
usart_outstring("Initializing I/O pins...\r\n");
DDRC |= 1 << DDC0; // Set PORTC bit 0 for output
DDRC |= 1 << DDC3; // Set PORTC bit 3 for output
DDRC |= 1 << DDC4; // Set PORTC bit 4 for output
DDRC |= 1 << DDC5; // Set PORTC bit 5 for output
DDRC = 0xff;
DDRB |= 1 << DDB1;
DDRB |= 1 << DDB2;
DDRC |= 1 << DDC2;
}
//adc
void adc_init(void){
DDRC &= ~(1 << DDC1);
ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); //16Mhz/128 = 125Khz the ADC reference clock
ADMUX |= (1<<REFS0); //Voltage reference from Avcc (5v)
ADCSRA |= (1<<ADEN); //Turn on ADC
ADCSRA |= (1<<ADSC); //Do an initial conversion because this one is the slowest and to ensure that everything is up and running
}
uint16_t read_adc(uint8_t channel){
ADMUX &= 0xF0; //Clear the older channel that was read
ADMUX |= channel; //Defines the new ADC channel to be read
ADCSRA |= (1<<ADSC); //Starts a new conversion
while(ADCSRA & (1<<ADSC)); //Wait until the conversion is done
return ADCW; //Returns the ADC value of the chosen channel
}
static uint8_t button_pressed = 0;
static uint8_t button_pressed_stack = 0;
void button_init(void)
{
/* set pin C1 as an input */
DDRB &= ~(1 << 0);
PORTC |= 1 << PC3; // Set PC3 to a 1
/* pull C1 up */
PORTB |= (0 << 0);
/* enable pin change interrupt */
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0);
}
uint8_t button_was_pressed(void)
{
if (button_pressed_stack) {
button_pressed_stack = 0;
return 1;
}
return 0;
}
uint8_t button_val(void)
{
if (PINB & (1 << 0)) {
return 1;
}
return 0;
}
//Buffer Output Swapping
void buffersUpdated()
{
showRelayBuffer(currentBuf);
}
void relaysOff()
{
PORTC &= ~(1 << PC3); //Set PC3 to 0
PORTC &= ~(1 << PC4); //Set PC4 to 0
PORTC &= ~(1 << PC5); //Set PC5 to 0
}
void showRelayBuffer(int buf[3])
{
relaysOff();
if(buf == NULL)
{
return;
}
PORTC &= ~(buf[0] << PC3);
PORTC &= ~(buf[1] << PC4);
PORTC &= ~(buf[2] << PC5);
PORTC |= (buf[0] << PC3);
PORTC |= (buf[1] << PC4);
PORTC |= (buf[2] << PC5);
}
void showNone()
{
currentBuf = NULL;
buffersUpdated();
}
void showThermo()
{
currentBuf = thermoBuf;
buffersUpdated();
}
void showBlinds()
{
currentBuf = blindBuf;
buffersUpdated();
}
void showLights()
{
currentBuf = lightBuf;
buffersUpdated();
}
//===
//LED control
void redLedOn()
{
PORTB |= 1 << PB1;
}
void greenLedOn()
{
PORTB |= 1 << PB2;
}
void blueLedOn()
{
PORTC |= 1 << PC2;
}
void allLedOff()
{
PORTB &= ~(1 << PB1);
PORTB &= ~(1 << PB2);
PORTC &= ~(1 << PC2);
}
//
ISR(PCINT0_vect)
{
if (button_val() == 0) {
button_pressed = 0;
}
else {
button_pressed = 1;
switch(buttonstate)
{
default:
usart_printf("unknown buttonstate: %d", buttonstate);
case 0:
allLedOff();
showNone();
break;
case 1:
allLedOff();
redLedOn();
showThermo();
break;
case 2:
allLedOff();
greenLedOn();
showLights();
break;
case 3:
allLedOff();
blueLedOn();
showBlinds();
break;
}
buttonstate = buttonstate + 1;
if(buttonstate == 4)
{
buttonstate = 0;
}
}
//usart_printf("INTERRUPTED thing buttonpressed = %d, button_val = %d, buttonstate = %d \r\n", button_pressed, button_val(), buttonstate);
}
void relayInit()
{
currentBuf = NULL;
buttonstate = 0;
buffersUpdated();
allLedOff();
}
//Thermostat controls
//PC5: Cool - Blue Wire - Light Three
//PC4: Heat - Green Wire - Light Two
//PC3: Fan - Yellow Wire - Light One
//
void thermo_fan_on()
{
usart_printf("Turning fan on\r\n");
//PORTC |= (1 << PC3); // Set PC3 to a 1
thermoBuf[0] = 1;
buffersUpdated();
}
void thermo_fan_off()
{
usart_printf("Turning fan off\r\n");
//PORTC &= ~(1 << PC3); //Set PC3 to 0
thermoBuf[0] = 0;
buffersUpdated();
}
void thermo_turn_off()
{
usart_printf("Turning all thermostat systems off\r\n");
//PORTC &= ~(1 << PC4); //Set PC4 to 0
//PORTC &= ~(1 << PC5); //Set PC5 to 0
thermoBuf[1] = 0;
thermoBuf[2] = 0;
buffersUpdated();
}
void thermo_call_for_heat()
{
thermo_turn_off();
usart_printf("Turning heat on\r\n");
//PORTC |= (1 << PC4); //Set PC4 to 1
thermoBuf[1] = 1;
buffersUpdated();
}
void thermo_call_for_cool()
{
thermo_turn_off();
usart_printf("Turning cool on\r\n");
//PORTC |= (1 << PC5); //Set PC5 to 1
thermoBuf[2] = 1;
buffersUpdated();
}
//Light controls
void lights_off()
{
usart_printf("Turning lights off\r\n");
lightBuf[0] = 0;
lightBuf[1] = 0;
lightBuf[2] = 0;
buffersUpdated();
}
void lights_full_power()
{
usart_printf("Turning lights on full power\r\n");
lightBuf[0] = 1;
lightBuf[1] = 1;
lightBuf[2] = 1;
buffersUpdated();
}
void lights_low_power()
{
usart_printf("Turning lights on low power\r\n");
lightBuf[0] = 1;
lightBuf[1] = 0;
lightBuf[2] = 0;
buffersUpdated();
}
//Blind Controls
void blinds_up()
{
usart_printf("Putting blinds up\r\n");
blindBuf[0] = 0;
blindBuf[1] = 1;
blindBuf[2] = 0;
buffersUpdated();
}
void blinds_down()
{
usart_printf("Putting blinds down\r\n");
blindBuf[0] = 0;
blindBuf[1] = 0;
blindBuf[2] = 1;
buffersUpdated();
}
void blinds_stop()
{
usart_printf("Stopping blinds\r\n");
blindBuf[0] = 1;
blindBuf[1] = 0;
blindBuf[2] = 0;
buffersUpdated();
}
// data sender general
void senddata(){
uint16_t result = read_adc(1);
char buf[128];
sprintf(buf, "%d\r", result);
usart_outstring(buf);
c=DEFAULT;
}
//write the control you want to do reacting to commands form master in this function
void control(char *command){
unsigned int numCommands = strlen(command);
int i = 0;
for(i = 0; i < numCommands; i++)
{
char cmd = command[i];
usart_out(cmd);
switch(cmd)
{
case HEAT_ON:
thermo_call_for_heat();
break;
case HEAT_OFF:
thermo_turn_off();
break;
case COOL_ON:
thermo_call_for_cool();
break;
case COOL_OFF:
thermo_turn_off();
break;
case FAN_ON:
thermo_fan_on();
break;
case FAN_OFF:
thermo_fan_off();
break;
case BLINDS_UP:
blinds_up();
break;
case BLINDS_DOWN:
blinds_down();
break;
case LIGHTS_FULL:
lights_full_power();
break;
case LIGHTS_HALF:
lights_low_power();
break;
case LIGHTS_OFF:
lights_off();
break;
default:
//usart_printf("Invalid command character received: %d from command string: %s", cmd, command);
break;
}
}
}
void receivecommand(){
int count=0;
while (1) {
_delay_ms(10);
count++;
if(StrRxFlag || count>500){ //time_out
if(StrRxFlag){
StrRxFlag=0; // Reset String received flag
count=0;
}else{
sprintf(buffer,"TIME_OUT");
count=0;
i=0;
interruptstate=COMMAND_MODE; //Reset buffer index
}
break;
}
}
control(buffer);
}
int main(void)
{
int count = -1;
/* for 9600 baud on with 9.304MHz clock */
usart_init();
io_pin_init();
/* adc_init must happen after io_pin_init because reasons */
adc_init();
button_init();
relayInit();
/*while(1)
{
if(command == 0) { allLedOff(); }
else if(command == 1) { allLedOff(); redLedOn(); }
else if(command == 2) { allLedOff(); greenLedOn(); }
else if(command == 3) { allLedOff(); blueLedOn(); }
command++;
if(command == 4) { command = 0; }
_delay_ms(500);
}*/
while(1)
{
switch(interruptstate){
case TRANSMIT_MODE:
receivecommand();
break;
default:
break;
}
if(count % 2 == 0)
{
PORTC |= 1 << PC0;
}
else
{
PORTC &= ~(1 << PC0);
}
switch(count)
{
case 0:
lights_low_power();
thermo_call_for_cool();
blinds_up();
break;
case 1:
lights_off();
thermo_fan_on();
blinds_stop();
break;
case 2:
lights_full_power();
thermo_call_for_heat();
blinds_down();
break;
case 3:
lights_off();
thermo_fan_off();
blinds_stop();
break;
default:
case 4:
lights_off();
thermo_turn_off();
blinds_stop();
break;
case -1:
//Do Nothing
break;
}
if(count != -1)
{
count = count + 1;
if(count == 5)
{
count = 0;
}
}
}
}
//interrpt handler
ISR(USART_RX_vect)
{
switch(interruptstate){
case COMMAND_MODE:
while (!(UCSR0A & (1<<RXC0)));
c=UDR0;
//Read USART data register
switch (c) {
case HB_BETA:
usart_out(ACTIVE_RESPONSE); //no motion detected
break;
case READ_BETA:
senddata(); //receive data request
break;
case SEND_BETA:
interruptstate=1;
usart_out(ACK);
break;
default:
break;
}
break;
case TRANSMIT_MODE:
while (!(UCSR0A & (1<<RXC0)));
buffer[i]=UDR0; //Read USART data register
if(buffer[i++]=='\r') //check for carriage return terminator and increment buffer index
{
// if terminator detected
StrRxFlag=1; //Set String received flag
buffer[i-1]=0x00; //Set string terminator to 0x00
i=0;
interruptstate=COMMAND_MODE; //Reset buffer index
}
break;
}
}