/*
 * @Author: dugulingping
 * @Date: 2022-04-17 16:00:40
 * @LastEditTime: 2022-05-23 16:57:11
 * @LastEditors: dugulingping
 * @Description: 
 * @FilePath: \test\DHTdisplay\DHTdisplay.ino
 */
#include "DHT.h"
// DHT11
#define DHTPIN 4
#define DHTTYPE DHT22   // DHT 11
//LED
#define SCLK 14
#define RCLK 13
#define DIO 15

DHT dht(DHTPIN, DHTTYPE);
//定义字库
unsigned char LED_0F[] = {
    // 0	 1	  2	   3	4	 5	  6	   7	8	 9	  A	   b	C    d	  E    F    -
    0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0xFF,0xbf
};
//定义位
unsigned char LED_BIT[] = {
    //  1   2   3   4   1+2   3+4   1~4
    0x01,0x02,0x04,0x08,0x03,0x0C,0x0F
};

//定义dht数据
int dhtdata[3] = {0};

void setup() {
    Serial.begin(115200);
    dht.begin();
    //让三个脚都是输出状态
    pinMode(SCLK,OUTPUT);
    pinMode(RCLK,OUTPUT);
    pinMode(DIO,OUTPUT); 

  
}
//定时
int delay_date = 0;
void loop() {
    if(delay_date < 2000){
        delay_date+=4;
    }else{
        delay_date = 0;
        DHT_Data();
    }
    if(!dhtdata[1]||!dhtdata[2]){
        LED_display(LED_BIT[6], 0xBF);
        return;
    }
    int num1 = dhtdata[0]%10;
    int num2 = dhtdata[0]/10;
    int num3 = dhtdata[2]%10;
    int num4 = dhtdata[2]/10;

    LED_display(LED_BIT[2], LED_0F[num1]-128);
    LED_display(LED_BIT[3], LED_0F[num2]);
    LED_display(LED_BIT[0], LED_0F[num3]-128);
    LED_display(LED_BIT[1], LED_0F[num4]);

    //LED_display(LED_BIT[2], LED_0F[data_split(dhtdata[0])[0]]-128);
    //LED_display(LED_BIT[1], LED_0F[data_split(dhtdata[2])[1]]);
    //LED_display(LED_BIT[0], LED_0F[data_split(dhtdata[2])[0]]);
    
}

void LED_display(unsigned char bit, unsigned char digit ){
    LED_OUT(digit);
    LED_OUT(bit);
    digitalWrite(RCLK,LOW);
    digitalWrite(RCLK,HIGH);
    delay(1);
}
void LED_OUT(unsigned char X){
    unsigned char i;
    for(i=8;i>=1;i--)
    {
        if(X&0x80)
            digitalWrite(DIO,HIGH);
        else
            digitalWrite(DIO,LOW);
        X<<=1;
        digitalWrite(SCLK,LOW);
        digitalWrite(SCLK,HIGH);
    }
}

int * data_split(int num){
    int res[2] = {0};
    for(int i = 0; num > 0; i++){
        res[i] = num%10;
    }
    return res;
}
void DHT_Data(){
    float h = dht.readHumidity();
    float t = dht.readTemperature(false);
    float hic = dht.computeHeatIndex(t, h, false);

    if (isnan(h) || isnan(t)){
        Serial.println(F("Failed to read from DHT sensor!"));
        dhtdata[0] = 0;
        dhtdata[1] = 0;
        dhtdata[2] = 0;
    return;
    }
    dhtdata[0] = h;
    dhtdata[1] = t;
    dhtdata[2] = hic;
    Serial.println(h);
}