-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
163 lines (123 loc) · 4.41 KB
/
main.c
File metadata and controls
163 lines (123 loc) · 4.41 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
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "hardware/uart.h"
#include "hardware/gpio.h"
#include "bmp280.h"
#include <stdio.h>
#include <math.h>
#define I2C_PORT i2c0
#define BMP280_ADDR 0x76
#define UART_ID uart1
const int BUZZER_INTERVAL_MS = 1000;
absolute_time_t last_buzz_time;
bool buzzer_active = false;
uint setup_pwm(uint gpio, uint freq)
{
gpio_set_function(gpio, GPIO_FUNC_PWM);
uint slice = pwm_gpio_to_slice_num(gpio);
uint channel = pwm_gpio_to_channel(gpio);
uint wrap = 125000000 / freq / 2; // Assuming a 125 MHz clock
pwm_set_wrap(slice, wrap);
pwm_set_chan_level(slice, channel, wrap / 2); // 50% duty cycle
return slice;
}
void update_buzzer(uint slice, uint LED_PIN, uint BUZZER_PIN)
{
static bool buzzing = false;
static absolute_time_t start_time;
if (buzzer_active)
{
if (!buzzing)
{
// Start buzzing
start_time = get_absolute_time();
// Turn on the buzzer
pwm_set_enabled(slice, true);
// Turn on the LED
gpio_put(LED_PIN, true);
buzzing = true;
}
else
{
// Check if 500ms passed
if (absolute_time_diff_us(start_time, get_absolute_time()) >= 500000)
{
// Turn off the buzzer
pwm_set_enabled(slice, false);
gpio_set_function(BUZZER_PIN, GPIO_FUNC_SIO);
gpio_set_dir(BUZZER_PIN, GPIO_OUT);
gpio_put(BUZZER_PIN, false);
// Turn off the LED
gpio_put(LED_PIN, false);
// Reinitialize the buzzer for PWM
gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM);
buzzer_active = false;
buzzing = false;
}
}
}
}
int main()
{
if (!stdio_init_all())
{
printf("Failed to initialize stdio\n");
return 1;
}
printf("Stdio initialized successfully\n");
sleep_ms(1000); // Wait for the serial to be ready
const uint LED_PIN = 25;
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
const uint BUZZER_PIN = 2;
gpio_init(BUZZER_PIN);
const uint slice = setup_pwm(BUZZER_PIN, 1000);
printf("Setting up UART...\n");
const uint BAUD_RATE = 9600;
uart_init(UART_ID, BAUD_RATE);
const uint UART_RX_PIN = 4;
const uint UART_TX_PIN = 5;
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
stdio_uart_init_full(UART_ID, BAUD_RATE, UART_TX_PIN, UART_RX_PIN);
printf("UART initialized successfully\n");
printf("Setting up I2C...\n");
i2c_init(I2C_PORT, 100 * 1000);
printf("I2C initialized successfully\n");
printf("Setting up GPIO for I2C...\n");
const uint SENSOR_SDA = 0;
const uint SENSOR_SCL = 1;
gpio_set_function(SENSOR_SDA, GPIO_FUNC_I2C);
gpio_set_function(SENSOR_SCL, GPIO_FUNC_I2C);
gpio_pull_up(SENSOR_SDA);
gpio_pull_up(SENSOR_SCL);
printf("GPIO for I2C set up successfully\n");
printf("Initializing BMP280...\n");
bmp280_init(I2C_PORT, BMP280_ADDR);
printf("BMP280 initialized successfully\n");
printf("Reading initial pressure...\n");
const int32_t sea_level_pressure = 101325;
const int32_t initial_altitude_m = 44330.0f * (1.0f - pow(bmp280_read_pressure(I2C_PORT, BMP280_ADDR) / sea_level_pressure, 0.1903f));
int32_t last_altitude_m = initial_altitude_m;
printf("Initial altitude: %d\n", initial_altitude_m);
while (true)
{
const int32_t next_pressure = bmp280_read_pressure(I2C_PORT, BMP280_ADDR);
const int32_t next_altitude_m = 44330.0f * (1.0f - pow(next_pressure / sea_level_pressure, 0.1903f));
if (next_altitude_m != last_altitude_m)
{
char buffer_for_altitude[64];
sprintf(buffer_for_altitude, "Altitude changed: %d -> %d\n", last_altitude_m, next_altitude_m);
printf("%s", buffer_for_altitude);
uart_puts(UART_ID, buffer_for_altitude);
last_altitude_m = next_altitude_m;
// Check if enough time passed since last buzz
if (absolute_time_diff_us(last_buzz_time, get_absolute_time()) >= BUZZER_INTERVAL_MS * 1000)
{
buzzer_active = true;
last_buzz_time = get_absolute_time();
}
}
update_buzzer(slice, LED_PIN, BUZZER_PIN);
}
}