๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[๋ฆฌ๋ˆ…์Šค ์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ ์ œ์–ด] SenseHAT ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Raspberry Pi

[๋ฆฌ๋ˆ…์Šค ์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์˜ ์ œ์–ด] SenseHAT

์ง•์ง•์•ŒํŒŒ์นด 2024. 1. 8. 19:49
728x90
๋ฐ˜์‘ํ˜•

<์„œ์˜์ง„ ๋‹˜์˜ ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ฆฌ๋ˆ…์Šค ํ”„๋กœ๊ทธ๋ž˜๋ฐ with ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด ์„œ์ ์„ ์ฐธ๊ณ ํ•ด์„œ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

 

โญ WiringPi 

๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด์— GPIO ํ•€ ์ œ์–ด๋ฅผ ์œ„ํ•œ C์–ธ์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ WiringPi ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜

// ๊ธฐ์กด์— ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๊ฐ€ ์ฐธ์กฐ ํ–ˆ๋‹ค๊ณ  ์—๋Ÿฌ ๋‚จ. ๊ทธ๋ž˜์„œ ์‚ญ์ œํ•จ
sudo apt purge wiringpi
hash -r
git clone https://github.com/WiringPi/WiringPi.git
cd WiringPi
git pull origin
./build
gpio -v

 

โญ ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์™€ SenseHAT ์ œ์–ดํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜

๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์™€ SenseHAT ์„ ์ œ์–ดํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜
SenseHAT ์€ ๊ธฐ์ƒ ๊ด€์ธก ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธฐ์•• ์„ผ์„œ, ์˜จ์Šต๋„ ์„ผ์„œ ํƒ‘์žฌํ•จ
๊ธฐ์•• ์„ผ์„œ(LPS25H), ์˜จ์Šต๋„ ์„ผ์„œ(HTS221)

 

2๊ฐœ์˜ ์„ผ์„œ์— ๋Œ€ํ•œ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ ๊ฐ๊ฐ ์ƒ์„ฑ + ์ดˆ๊ธฐํ™”
p ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ LPS25H ์„ผ์„œ์˜ ๊ธฐ์••๊ณผ ์˜จ๋„ ์ถœ๋ ฅ
t ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ HTS221 ์„ผ์„œ์˜ ์˜จ๋„์™€ ์Šต๋„ ์ถœ๋ ฅ
kbhit() ํ•จ์ˆ˜ ์ด์šฉํ•ด์„œ ํ˜„์žฌ ํ‚ค๋ณด๋“œ์—์„œ ๋ˆ„๋ฅธ ๋ฌธ์ž์—ด ๊ฐ€์ ธ์˜ค๊ณ , ๊ฐ ์„ผ์„œ๋ฅผ ์ œ์–ดํ•จ
ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์„œ ๋‹ค๋ฅธ ๋™์ž‘ ์ˆ˜ํ–‰ X

// ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์™€ SenseHAT ์„ ์ œ์–ดํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜
// SenseHAT ์€ ๊ธฐ์ƒ ๊ด€์ธก ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธฐ์•• ์„ผ์„œ, ์˜จ์Šต๋„ ์„ผ์„œ ํƒ‘์žฌํ•จ
// ๊ธฐ์•• ์„ผ์„œ(LPS25H), ์˜จ์Šต๋„ ์„ผ์„œ(HTS221)
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <sys/ioctl.h>

// I2C๋ฅผ ์œ„ํ•œ ์žฅ์น˜ ํŒŒ์ผ
static const char* I2C_DEV = "/dev/i2c-1";
// ioctl() ํ•จ์ˆ˜์—์„œ I2C_SLAVE ์„ค์ •์„ ์œ„ํ•œ ๊ฐ’
static const int I2C_SLAVE = 0x0703;
// SenseHAT ์˜ i2c-1 ๊ฐ’
static const int LPS25H_ID = 0x5C;
static const int HTS221_ID = 0x5F;
// STMicroelectronics ์˜ ์ŠคํŽ™ ๋ฌธ์„œ ๊ฐ’
static const int CTRL_REG1 = 0x20;
static const int CTRL_REG2 = 0x21;
// STMicroelectronics ์˜ LPS25H ์ŠคํŽ™ ๋ฌธ์„œ ๊ฐ’
static const int PRESS_OUT_XL = 0x28;
static const int PRESS_OUT_L = 0x29;
static const int PRESS_OUT_H = 0x2A;
static const int PTEMP_OUT_L = 0x2B;
static const int PTEMP_OUT_H = 0x2C;
// STMicroelectronics ์˜ HTS221 ์ŠคํŽ™ ๋ฌธ์„œ ๊ฐ’
static const int H0_T0_OUT_L = 0x36;
static const int H0_T0_OUT_H = 0x37;
static const int H1_T0_OUT_L = 0x3A;
static const int H1_T0_OUT_H = 0x3B;
static const int H0_rH_x2 = 0x30;
static const int H1_rH_x2 = 0x31;

static const int H_T_OUT_L = 0x28;
static const int H_T_OUT_H = 0x29;

static const int T0_OUT_L = 0x3C;
static const int T0_OUT_H = 0x3D;
static const int T1_OUT_L = 0x3E;
static const int T1_OUT_H = 0x3F;
static const int T0_degC_x8 = 0x32;
static const int T1_degC_x8 = 0x33;
static const int T1_T0_MSB = 0x35;

static const int TEMP_OUT_L = 0x2A;
static const int TEMP_OUT_H = 0x2B;

int kbhit(void);
// ๊ธฐ์••๊ณผ ์˜จ๋„๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜
void getPressure(int fd, double *temperature, double *pressure);
// ์˜จ๋„์™€ ์Šต๋„๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜
void getTemperature(int fd, double *temperature, double *humidity);

int main(int argc, char **argv) {
    int i = 0;
    int pressure_fd, temperature_fd;
    // ์˜จ๋„์™€ ์••๋ ฅ, ์Šต๋„๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
    double t_c = 0.0;
    double temperature, humidity;

    // => 2๊ฐœ์˜ ์„ผ์„œ์— ๋Œ€ํ•œ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ ๊ฐ๊ฐ ์ƒ์„ฑ + ์ดˆ๊ธฐํ™”
    // => p ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ LPS25H ์„ผ์„œ์˜ ๊ธฐ์••๊ณผ ์˜จ๋„ ์ถœ๋ ฅ
    // => t ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ HTS221 ์„ผ์„œ์˜ ์˜จ๋„์™€ ์Šต๋„ ์ถœ๋ ฅ
    // => kbhit() ํ•จ์ˆ˜ ์ด์šฉํ•ด์„œ ํ˜„์žฌ ํ‚ค๋ณด๋“œ์—์„œ ๋ˆ„๋ฅธ ๋ฌธ์ž์—ด ๊ฐ€์ ธ์˜ค๊ณ , ๊ฐ ์„ผ์„œ๋ฅผ ์ œ์–ดํ•จ
    // => ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์„œ ๋‹ค๋ฅธ ๋™์ž‘ ์ˆ˜ํ–‰ X

    // I2C ์žฅ์น˜ ํŒŒ์ผ ์˜คํ”ˆ
    if ((pressure_fd = open(I2C_DEV, O_RDWR)) < 0) {
        perror("Unable to open i2c device");
        return 1;
    }

    // I2C ์žฅ์น˜๋ฅผ ์Šฌ๋ ˆ์ดํ”„ ๋ชจ๋“œ๋กœ LPS25H ์„ค์ •
    if (ioctl(pressure_fd, I2C_SLAVE, LPS25H_ID) < 0) {
        perror("Unable to configure i2c slave device");
        close(pressure_fd);
        return 1;
    }

    // I2C ์žฅ์น˜ ํŒŒ์ผ ์˜คํ”ˆ
    if ((temperature_fd = open(I2C_DEV, O_RDWR)) < 0) {
        perror("Unable to open i2c device");
        return 1;
    }

    // I2C ์žฅ์น˜๋ฅผ ์Šฌ๋ ˆ์ด๋ธŒ ๋ชจ๋“œ๋กœ HTS221 ์„ค์ •
    if (ioctl(temperature_fd, I2C_SLAVE, HTS221_ID) < 0) {
        perror("Unable to configure i2c slave device");
        return 1;
    }

    printf("p : Pressure, t : Temperature, q : Quit\n");

    for (i = 0; ; i++) {
        // ํ‚ค๋ณด๋“œ๊ฐ€ ๋ˆŒ๋ ธ๋Š”์ง€ ํ™•์ธ
        if (kbhit()) {
            // ๋ฌธ์ž ์ฝ๋Š”๋‹ค
            switch(getchar()) {
                case "p":
                    // LPS25H ์žฅ์น˜ ์ดˆ๊ธฐํ™”
                    wiringPiI2CWriteReg8(pressure_fd, CTRL_REG1, 0x00);
                    wiringPiI2CWriteReg8(pressure_fd, CTRL_REG1, 0x84);
                    wiringPiI2CWriteReg8(pressure_fd, CTRL_REG2, 0x01);

                    // ๊ธฐ์••๊ณผ ์˜จ๋„ ๊ฐ’ ์–ป๊ธฐ
                    getPressure(pressure_fd, &t_c, &pressure);
                    // ๊ณ„์‚ฐ๋œ ๊ฐ’ ์ถœ๋ ฅ
                    printf("Temperature(from LPS25H) = %.2f C\n", t_c);
                    printf("Pressure = %.0f hpa\n", pressure);
                    break;
                case "t":
                    // HTS221 ์žฅ์น˜ ์ดˆ๊ธฐํ™”
                    wiringPiI2CWriteReg8(temperature_fd, CTRL_REG1, 0x00);
                    wiringPiI2CWriteReg8(temperature_fd, CTRL_REG1, 0x84);
                    wiringPiI2CWriteReg8(temperature_fd, CTRL_REG2, 0x01);

                    // ์˜จ๋„์™€ ์Šต๋„ ๊ฐ’ ์–ป๊ธฐ
                    getPressure(temperature_fd, &temperature, &humidity);
                    // ๊ณ„์‚ฐ๋œ ๊ฐ’ ์ถœ๋ ฅ
                    printf("Temperature(from LPS25H) = %.2f C\n", temperature);
                    printf("Humidity = %.0f%% rH\n", humidity);
                    break;
                case "q":
                    goto END;
                    break;
            };
        }
        printf("%20d\t\t\r", i);
        delay(100);
    }
END:
    printf("Good Bye!\n");

    // ์‚ฌ์šฉ ๋๋‚œ ์žฅ์น˜ ์ •๋ฆฌ
    wiringPiI2CWriteReg8(pressure_fd, CTRL_REG1, 0x00);
    close(pressure_fd);

    wiringPiI2CWriteReg8(temperature_fd, CTRL_REG1, 0x00);
    close(temperature_fd);

    return 0;
}

// ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
int kbhit(void) {
    // ํ„ฐ๋ฏธ๋„์— ๋Œ€ํ•œ ๊ตฌ์กฐ์ฒด
    struct termios oldt, newt;
    int ch, oldf;
    
    // ํ˜„์žฌ ํ„ฐ๋ฏธ๋„์— ์„ค์ •๋œ ์ •๋ณด ๊ฐ€์ ธ์˜ด
    tcgetattr(0, &oldt);
    newt = oldt;
    // ์ •๊ทœ ๋ชจ๋“œ ์ž…๋ ฅ๊ณผ ์—์ฝ”๋ฅผ ํ•ด์ œ (ICANON ์ •๊ทœ๋ชจ๋“œ๋กœ ์ž…๋ ฅ์ด ์ด๋ค„์ง€๊ฒŒ ํ•จ, ECHO ๋Š” ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ ๋ชจ๋‹ˆํ„ฐ๋กœ ์ถœ๋ ฅํ•˜๋„๋ก ํ•จ)
    newt.c_lflag &= ~(ICANON | ECHO);
    // ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ํ„ฐ๋ฏธ๋„ ์„ค์ • (TCSANOW ๋ฐ”๋กœ ํ„ฐ๋ฏธ๋„์— ๋ณ€๊ฒฝ๋œ ์†์„ฑ์ด ์ ์šฉ๋˜๋„๋ก ํ•จ)
    tcsetattr(0, TCSANOW, &newt);
    oldf = fcntl(0, F_GETFL, 0);
    // ์ž…๋ ฅ์„ ๋…ผ๋ธ”๋กœํ‚น ๋ชจ๋“œ๋กœ ์„ค์ •
    fcntl(0, F_SETFL, oldf | O_NONBLOCK);
    // getchar() : ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ๋œ ๋ฌธ์ž๊ฐ€ ํ™”๋ฉด์— ์ž๋™์œผ๋กœ ์ถœ๋ ฅ๋จ
    ch = getchar();

    if (ch != EOF) {
        // ์•ž์—์„œ ์ฝ์œผ๋ฉฐ ๊บผ๋‚ธ ๋ฌธ์ž๋ฅผ ๋‹ค์‹œ ๋„ฃ๋Š”๋‹ค
        ungetc(ch, stdin);
    }
    return 0;
}

// ๊ธฐ์••๊ณผ ์˜จ๋„ ๊ณ„์‚ฐ ํ•จ์ˆ˜
void getPressure(int fd, double *temperature, double *pressure)
{
    int result;
    unsigned char temp_out_l = 0, temp_out_h = 0; 	/* ์˜จ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ */
    unsigned char press_out_xl = 0; 	/* ๊ธฐ์••์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ */
    unsigned char press_out_l = 0;
    unsigned char press_out_h = 0;
    short temp_out = 0; 		/* ์˜จ๋„์™€ ์••๋ ฅ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ */
    int press_out = 0;

    /* ์ธก์ •์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ */
    do {
        delay(25); 			/* 25๋ฐ€๋ฆฌ์ดˆ ๋Œ€๊ธฐ */
        result = wiringPiI2CReadReg8(fd, CTRL_REG2);
    } while(result != 0);

    /* ์ธก์ •๋œ ์˜จ๋„ ๊ฐ’ ์ฝ๊ธฐ(2๋ฐ”์ดํŠธ ์ฝ๊ธฐ) */
    temp_out_l = wiringPiI2CReadReg8(fd, PTEMP_OUT_L);
    temp_out_h = wiringPiI2CReadReg8(fd, PTEMP_OUT_H);

    /* ์ธก์ •๋œ ๊ธฐ์•• ๊ฐ’ ์ฝ๊ธฐ(3๋ฐ”์ดํŠธ ์ฝ๊ธฐ) */
    press_out_xl = wiringPiI2CReadReg8(fd, PRESS_OUT_XL);
    press_out_l = wiringPiI2CReadReg8(fd, PRESS_OUT_L);
    press_out_h = wiringPiI2CReadReg8(fd, PRESS_OUT_H);

    /* ๊ฐ๊ฐ ์ธก์ •ํ•œ ๊ฐ’๋“ค์„ ํ•ฉ์„ฑํ•ด์„œ ์˜จ๋„(16๋น„ํŠธ)์™€ ๊ธฐ์••(24๋น„ํŠธ) ๊ฐ’ ์ƒ์„ฑ(๋น„ํŠธ/์‹œํ”„ํŠธ ์ด์šฉ) */
    temp_out = temp_out_h << 8 | temp_out_l;
    press_out = press_out_h << 16 | press_out_l << 8 | press_out_xl;

    /* ์ถœ๋ ฅ๊ฐ’ ๊ณ„์‚ฐ */
    *temperature = 42.5 + (temp_out / 480.0);
    *pressure = press_out / 4096.0;
}

// ์˜จ๋„์™€ ์Šต๋„๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
void getTemperature(int fd, double *temperature, double *humidity)
{
    int result;

    /* ์ธก์ •์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ */
    do {
        delay(25); 			/* 25๋ฐ€๋ฆฌ์ดˆ ๋Œ€๊ธฐ */
        result = wiringPiI2CReadReg8(fd, CTRL_REG2);
    } while(result != 0);

    /* ์˜จ๋„(LSB(ADC))๋ฅผ ์œ„ํ•œ ๋ณด์ •๊ฐ’(x-๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ 2์ง€์ ) ์ฝ๊ธฐ */
    unsigned char t0_out_l = wiringPiI2CReadReg8(fd, T0_OUT_L);
    unsigned char t0_out_h = wiringPiI2CReadReg8(fd, T0_OUT_H);
    unsigned char t1_out_l = wiringPiI2CReadReg8(fd, T1_OUT_L);
    unsigned char t1_out_h = wiringPiI2CReadReg8(fd, T1_OUT_H);

    /* ์˜จ๋„(°C)๋ฅผ ์œ„ํ•œ ๋ณด์ •๊ฐ’(y-๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ 2์ง€์ ) ์ฝ๊ธฐ */
    unsigned char t0_degC_x8 = wiringPiI2CReadReg8(fd, T0_degC_x8);
    unsigned char t1_degC_x8 = wiringPiI2CReadReg8(fd, T1_degC_x8);
    unsigned char t1_t0_msb = wiringPiI2CReadReg8(fd, T1_T0_MSB);

    /* ์Šต๋„(LSB(ADC))๋ฅผ ์œ„ํ•œ ๋ณด์ •๊ฐ’(x-๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ 2์ง€์ ) ์ฝ๊ธฐ */
    unsigned char h0_out_l = wiringPiI2CReadReg8(fd, H0_T0_OUT_L);
    unsigned char h0_out_h = wiringPiI2CReadReg8(fd, H0_T0_OUT_H);
    unsigned char h1_out_l = wiringPiI2CReadReg8(fd, H1_T0_OUT_L);
    unsigned char h1_out_h = wiringPiI2CReadReg8(fd, H1_T0_OUT_H);

    /*์Šต๋„(% rH)๋ฅผ ์œ„ํ•œ ๋ณด์ •๊ฐ’(y-๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ 2์ง€์ ) ์ฝ๊ธฐ */
    unsigned char h0_rh_x2 = wiringPiI2CReadReg8(fd, H0_rH_x2);
    unsigned char h1_rh_x2 = wiringPiI2CReadReg8(fd, H1_rH_x2);

    /* ๊ฐ๊ฐ ์ธก์ •ํ•œ ๊ฐ’๋“ค์„ ํ•ฉ์„ฑํ•ด์„œ ์˜จ๋„(x-๊ฐ’) ๊ฐ’ ์ƒ์„ฑ(๋น„ํŠธ/์‹œํ”„ํŠธ ์ด์šฉ) */
    short s_t0_out = t0_out_h << 8 | t0_out_l;
    short s_t1_out = t1_out_h << 8 | t1_out_l;

    /* ๊ฐ๊ฐ ์ธก์ •ํ•œ ๊ฐ’๋“ค์„ ํ•ฉ์„ฑํ•ด์„œ ์Šต๋„(x-๊ฐ’) ๊ฐ’ ์ƒ์„ฑ(๋น„ํŠธ/์‹œํ”„ํŠธ ์ด์šฉ) */
    short s_h0_t0_out = h0_out_h << 8 | h0_out_l;
    short s_h1_t0_out = h1_out_h << 8 | h1_out_l;

    /* 16๋น„ํŠธ์™€ 10๋น„ํŠธ์˜ ๊ฐ’ ์ƒ์„ฑ(๋น„ํŠธ ๋งˆ์Šคํฌ/์‹œํ”„ํŠธ ์ด์šฉ) */
    unsigned short s_t0_degC_x8 = (t1_t0_msb & 3) << 8 | t0_degC_x8;
    unsigned short s_t1_degC_x8 = ((t1_t0_msb & 12) >> 2) << 8 | t1_degC_x8;

    /* ์˜จ๋„ ๋ณด์ •๊ฐ’(y-๊ฐ’) ๊ณ„์‚ฐ */
    double d_t0_degC = s_t0_degC_x8 / 8.0;
    double d_t1_degC = s_t1_degC_x8 / 8.0;

    /* ์Šต๋„ ๋ณด์ •๊ฐ’(y-๊ฐ’) ๊ณ„์‚ฐ */
    double h0_rH = h0_rh_x2 / 2.0;
    double h1_rH = h1_rh_x2 / 2.0;

    /* ์˜จ๋„์™€ ์Šต๋„์˜ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๋ณด์ • ์„ ํ˜• ์ง์„  ๊ทธ๋ž˜ํ”„ 'y = mx + c' ๊ณต์‹์„ ๊ณ„์‚ฐ */
    double t_gradient_m = (d_t1_degC - d_t0_degC) / (s_t1_out - s_t0_out);
    double t_intercept_c = d_t1_degC - (t_gradient_m * s_t1_out);
    double h_gradient_m = (h1_rH - h0_rH) / (s_h1_t0_out - s_h0_t0_out);
    double h_intercept_c = h1_rH - (h_gradient_m * s_h1_t0_out);

    /* ์ฃผ๋ณ€์˜ ์˜จ๋„ ์ฝ๊ธฐ(2๋ฐ”์ดํŠธ ์ฝ๊ธฐ) */
    unsigned char t_out_l = wiringPiI2CReadReg8(fd, TEMP_OUT_L);
    unsigned char t_out_h = wiringPiI2CReadReg8(fd, TEMP_OUT_H);

    /* 16๋น„ํŠธ ๊ฐ’ ์ƒ์„ฑ */
    short s_t_out = t_out_h << 8 | t_out_l;

    /* ์ฃผ๋ณ€์˜ ์Šต๋„ ์ฝ๊ธฐ(2๋ฐ”์ดํŠธ ์ฝ๊ธฐ) */
    unsigned char h_t_out_l = wiringPiI2CReadReg8(fd, H_T_OUT_L);
    unsigned char h_t_out_h = wiringPiI2CReadReg8(fd, H_T_OUT_H);

    /* 16๋น„ํŠธ ๊ฐ’ ์ƒ์„ฑ */
    short s_h_t_out = h_t_out_h << 8 | h_t_out_l;

    /* ์ฃผ๋ณ€์˜ ์˜จ๋„ ๊ณ„์‚ฐ */
    *temperature = (t_gradient_m * s_t_out) + t_intercept_c;

    /* ์ฃผ๋ณ€์˜ ์Šต๋„ ๊ณ„์‚ฐ */
    *humidity = (h_gradient_m * s_h_t_out) + h_intercept_c;
}

BUT.. ์“ฐ๋‹ˆ๋Š” SenseHAT ์ด ์—†๋‹ค!

728x90
๋ฐ˜์‘ํ˜•
Comments