/*
 *
 * N55A甲烷气体传感器
 *
 */

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>

void setup(int pin) {
    wiringPiSetup();
    pinMode(pin, INPUT);
}

int readMethaneLevel(int pin) {
    int methaneDetected = digitalRead(pin);
    return methaneDetected;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Error: Invalid number of arguments.\n");
        printf("Usage: %s PIN\n", argv[0]);
        exit(1);
    }

    int pin = atoi(argv[1]);
    if (pin < 0) {
        printf("Error: Invalid PIN value.\n");
        exit(1);
    }

    setup(pin);

    while(1) {
        int result = readMethaneLevel(pin);
        if (result == HIGH) {
            printf("CH4\n");
        }
        
        delay(1000); // 每隔1秒读取一次传感器
    }

    return 0;
}

