92 lines
2.2 KiB
C
Executable File
92 lines
2.2 KiB
C
Executable File
/*
|
|
Code pour lire la data du I2C decibel meter
|
|
Pour compiler le code: gcc -o sound_meter sound_meter.c
|
|
Connexion I2C:
|
|
SDA to Pi Pico GPIO 2
|
|
SCL to Pi Pico GPIO 3
|
|
|
|
Device connected on addr 0x48
|
|
|
|
to start the script:
|
|
sudo /var/www/nebuleair_pro_4g/sound_meter/sound_meter
|
|
|
|
0x00 : VERSION register
|
|
0x01 : Device ID (byte 1)
|
|
0x02 : Device ID
|
|
0x03 : Device ID
|
|
0x04 : Device ID (byte 4)
|
|
0x05 : SKRATCH register
|
|
0x06 : CONTROL register
|
|
0x07 : TAVG high register
|
|
0x08 : TAVG low register
|
|
0x09 : RESET register
|
|
|
|
0x0A : Decibel register (Latest sound intensity value in decibels, averaged over the last Tavg time period.)
|
|
|
|
0x0B : Min register (Minimum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
|
0x0C : Max register (Maximum value of decibel reading captured since power-up or manual reset of MIN/MAX registers.)
|
|
|
|
#Le Tavg est programmé sur 2 octets: le high byte et le low byte.
|
|
0x07 #Averaging time (high byte) in milliseconds for calculating sound intensity levels. (default 0x03 -> 3)
|
|
0x08 #Averaging time (low byte) in milliseconds for calculating sound intensity levels. (default 0xE8 -> 232)
|
|
valeur = (Tavg_high x 256) + Tavg_low
|
|
= (3 * 256) + 232
|
|
= 1000 ms
|
|
= 1 s
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <linux/i2c-dev.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
int main()
|
|
{
|
|
int file;
|
|
char data;
|
|
|
|
// Open I2C1 for reading the sound meter module registers
|
|
if ((file = open("/dev/i2c-1", O_RDWR)) < 0)
|
|
{
|
|
perror("Failed to open I2C1!");
|
|
exit(1);
|
|
}
|
|
|
|
// 0x48 is the decibel meter I2C address
|
|
if (ioctl(file, I2C_SLAVE, 0x48) < 0)
|
|
{
|
|
perror("db Meter module is not connected/recognized at I2C addr = 0x48");
|
|
close(file);
|
|
exit(1);
|
|
}
|
|
|
|
// Decibel value is stored in register 0x0A
|
|
data = 0x0A;
|
|
|
|
// Write the register address (0x0A) to the device
|
|
if (write(file, &data, 1) != 1)
|
|
{
|
|
perror("Failed to write register 0x0A");
|
|
close(file);
|
|
exit(1);
|
|
}
|
|
|
|
// Read one byte from the device
|
|
if (read(file, &data, 1) != 1)
|
|
{
|
|
perror("Failed to read register 0x0A");
|
|
close(file);
|
|
exit(1);
|
|
}
|
|
|
|
// Display the sound level
|
|
printf("%d\n", data);
|
|
|
|
// Close the I2C connection
|
|
close(file);
|
|
return 0;
|
|
}
|