This repository was archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmhvdoor.c
More file actions
175 lines (140 loc) · 3.18 KB
/
mhvdoor.c
File metadata and controls
175 lines (140 loc) · 3.18 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
#include <math.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "ht1632.h"
#include "mhvdoor.h"
#include "colour.h"
volatile double OCCUPY = 0;
volatile float j = 0;
// Timmer routine gets called 100 times a second.
ISR(TIMER1_COMPA_vect) {
if( SENSOR ) { LED_ON; }
else { LED_OFF; }
OCCUPY += SENSOR ? 0.01 : -0.01;
if( OCCUPY < 0 ) OCCUPY = 0;
if( OCCUPY > 84600 ) OCCUPY = 84600;
j+= (18 + ((360-18)*(OCCUPY/84600)))*0.01;
if( j > 360 ) j -= 360;
}
int main(void) {
DDRB |= _BV(DDB5); // debug LED
// PIR Sensor input
// make sure C0 is set to input and it's pullup is on
DDRC &= ~_BV(DDC0);
PORTC |= _BV(PORTC0);
// HT1632 data bus
DDRB |= _BV(DDB0);
DDRB |= _BV(DDB1);
DDRB |= _BV(DDB2);
// HT1632 Chip Selects
DDRD |= _BV(DDD3); //cs1
DDRD |= _BV(DDD5); //cs2
DDRD |= _BV(DDD6); //cs3
DDRD |= _BV(DDD7); //cs4
// Pull all the Chip Selects on the HT1632s high
PORTD |= _BV(PORTD3) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7);
// LPD8806 data bus
DDRC |= _BV(DDC4);
DDRC |= _BV(DDC5);
// Setup each of the HT1632 displays
CS1_LOW;
init();
CS1_HIGH;
CS2_LOW;
init();
CS2_HIGH;
CS3_LOW;
init();
CS3_HIGH;
CS4_LOW;
init();
CS4_HIGH;
CS1_LOW;
blank();
CS1_HIGH;
CS2_LOW;
blank();
CS2_HIGH;
CS3_LOW;
blank();
CS3_HIGH;
CS4_LOW;
blank();
CS4_HIGH;
// Set up the timer to tick every second
cli();
TCCR1A = 0;
TCCR1B = 0;
// set compare match register to desired timer count:
OCR1A = 156;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS10) | (1 << CS12); // 1024 perscaler
TIMSK1 |= (1 << OCIE1A);
sei();
// Setup the data for the LPD8806 based string of LEDs.
uint8_t buffer[32*3];
uint16_t i;
for(i=0;i<32*3;i++) { buffer[i] = 0; }
LPD8806_write(buffer,32*3);
while(1) {
for(i = 0; i < 32; ++i ) {
HSVtoRGB(
(int)((360/32)*i+j) % 360, 255, 255,
buffer+1+i*3, buffer+0+i*3, buffer+2+i*3
);
}
LPD8806_write(buffer,32*3);
uint16_t leds = lrint(163.292 * log(22*OCCUPY/3600+1));
uint16_t row;
for( row = 0; row < 128; row ++ ) {
switch( row ){
case 0:
CS4_HIGH;
CS1_LOW;
set_mode(write_mode);
send_address(0);
break;
case 32:
CS1_HIGH;
CS2_LOW;
set_mode(write_mode);
send_address(0);
break;
case 64:
CS2_HIGH;
CS3_LOW;
set_mode(write_mode);
send_address(0);
break;
case 96:
CS3_HIGH;
CS4_LOW;
set_mode(write_mode);
send_address(0);
break;
}
if( (row+1) * 8 <= leds ) {
send_data(0xFF);
send_data(0xFF);
} else if( row * 8 > leds ) {
send_data(0x0);
send_data(0x0);
} else {
uint8_t temp = _BV(7);
uint8_t lights_left = leds % 8;
while( lights_left > 0 ) {
--lights_left;
temp >>= 1;
temp |= 128;
}
send_data( temp );
swap(temp);
send_data( temp );
}
}
CS4_HIGH;
}
return 0;
}