-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
179 lines (157 loc) · 3.62 KB
/
main.c
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
///*
//* test_draft.c
//*
//* Created: 2023/11/17 14:12:28
//* Author : Chen Chen
//*/
//
//
#include <avr/io.h>
#include <util/delay.h>
#include "lib/common.h"
#include "lib/uart.h"
#include "lib/IR.h"
#include "lib/stepper.h"
#include "lib/servo.h"
#include "lib/BH1750.h"
#include "lib/ST7735.h"
#include "lib/LCD_GFX.h"
#include "lib/SHT30.h"
#define SLAVE_ADDR 0x09
enum mode control_mode=DEFAULT_MODE;
void write_to_slave(char* buf){
TWI_start();
TWI_write(SLAVE_ADDR << 1);
int i=0;
while (buf[i] != '\0'){
TWI_write(buf[i++]);
}
TWI_stop();
}
int main(void) {
UART_init(BAUD_PRESCALER);
init_stepper();
init_servo();
BH1750_init();
init_IR();
TWI_init();
UART_putstring("initialized\n");
Open_window_to(0);
char buf[30];
while (1){
// auto control
if (control_mode == AUTO){
// check illumination
uint16_t lux = BH1750_read();
sprintf(buf, "lux:%u\n", lux);
UART_putstring(buf);
if (lux > LUX_THRESHOLD_HIGH)
curtain_roll2top();
else if (lux < LUX_THRESHOLD_LOW)
curtain_roll2bottom();
// get temperature and humidity
triggerMeasurement();
char t_h[6];
readTH(t_h);
// check humidity
uint16_t hum = atoi(t_h) % 100;
if (hum > HUM_THRESHOLD_HIGH){
Open_window_to(0);
}
else if (hum < HUM_THRESHOLD_LOW){
Open_window_to(48);
}
write_to_slave(t_h);
}
_delay_ms(20000);
}
}
//#include <avr/io.h>
//#include <util/delay.h>
//#include <avr/interrupt.h>
//#include <util/twi.h>
//
//
//#include "lib/common.h"
//#include "lib/uart.h"
//#include "lib/ST7735.h"
//#include "lib/LCD_GFX.h"
//#include "lib/IR.h"
//
//// Define I2C Slave Address
//#define SLAVE_ADDR 0x09
//
//// Buffer for storing received data
//volatile char i2c_buffer[10];
//enum mode control_mode=AUTO;
//
//// Color definitions
//uint16_t white = 0xffff;
//uint16_t red = 0xf800;
//uint16_t blue = 0x041f;
//uint16_t grey = 0xce79;
//uint16_t green = 0x2668;
//uint16_t black = 0x0000;
//
//
//void I2C_init(uint8_t slave_address) {
//TWAR = (slave_address << 1);
//TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
//}
//
//ISR(TWI_vect) {
//static volatile uint8_t i2c_index = 0;
//switch(TW_STATUS) {
//case TW_SR_DATA_ACK:
//// Received data from master, ACK sent
//i2c_buffer[i2c_index++] = TWDR;
//TWCR |= (1 << TWINT) | (1 << TWEA);
//break;
//case TW_SR_STOP:
//// Stop or repeated start condition received
//i2c_buffer[i2c_index] = '\0'; // Null-terminate the string
//i2c_index = 0; // Reset buffer index
//TWCR |= (1 << TWINT) | (1 << TWEA);
//break;
//default:
//TWCR |= (1 << TWINT);
//break;
//}
//}
//
//void Initialize() {
//UART_init(BAUD_PRESCALER); // Initialize UART
//lcd_init(); // Initialize LCD
//I2C_init(SLAVE_ADDR); // Initialize I2C
//sei(); // Enable global interrupts
//}
//
//void screen_init() {
//LCD_setScreen(white); // Set background to white
//LCD_drawStringLarge(5, 5, "Temperature:", red, white, 2);
//LCD_drawStringLarge(45, 25, "~C", red, white, 2);
//LCD_drawStringLarge(5, 50, "Humidity:", blue, white, 2);
//LCD_drawStringLarge(45, 75, "%", blue, white, 2);
//}
//
//
//int main(void) {
//Initialize();
//screen_init();
//
//while (1) {
//if (i2c_buffer[0] != '\0') { // Check if buffer has data
//char buf1[3];
//char buf2[3];
//buf1[0] = i2c_buffer[0];
//buf1[1] = i2c_buffer[1];
//buf1[2] = '\0';
//buf2[0] = i2c_buffer[2];
//buf2[1] = i2c_buffer[3];
//buf2[2] = '\0';
//LCD_drawStringLarge(10, 25, buf1, red, white, 2); // Display the temperature
//LCD_drawStringLarge(10, 75, buf2, blue, white, 2); // Display the humidity
//}
//_delay_ms(3000);
//}
//}