Guo Wenxue
2022-09-22 732d1963ed83abf4e5f1b06ec451a6943421d716
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
/*********************************************************************************
 *      Copyright:  (C) 2019 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  hal.c
 *    Description:  This file is HAL(Hardware Abstract Layer) initial functions
 *                 
 *        Version:  1.0.0(2019年06月24日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2019年06月24日 23时46分47秒"
 *                 
 ********************************************************************************/
 
#include "hal.h"
 
                              /* LED_R  LED_G   LED_B */
static int led_pin[LED_MAX] = { 0,0,0 };
static int relay_pin = 0;
static int beep_pin = 0;
static int infrared_pin = 0;
 
void init_relay(int pin);
void init_led(int redpin, int greenpin, int bluepin);
void init_beep(int pin);
 
int hal_init(hwconf_t *hwconf)
{
    if( !hwconf->enable )
    {
        log_err("All hardware modules are disabled, skip initialise HAL.\n");
        return 0;
    }
 
    wiringPiSetup();
 
    if( hwconf->leds )
        init_led(hwconf->red_pin, hwconf->green_pin, hwconf->blue_pin); 
 
    if( hwconf->relay_pin )
        init_relay(hwconf->relay_pin);
 
    if( hwconf->beep_pin )
        init_beep(hwconf->beep_pin);
 
    if( hwconf->infrared_pin )
        init_infrared(hwconf->infrared_pin);
 
    if( hwconf->sht2x && sht2x_init() < 0 )
    {
        log_err("Initialise SHT20 T&H sensor failure\n");
        return -1;
    } 
    
    if( hwconf->lux && tsl2561_init() < 0 )
    {
        log_err("Initialise TSL2561 Lux sensor failure\n");
        return -1;
    } 
 
    return 0;
}
 
void init_led(int redpin, int greenpin, int bluepin)
{
    int         i; 
 
    led_pin[LED_R]=redpin; 
    led_pin[LED_G]=greenpin; 
    led_pin[LED_B]=bluepin;
    
    for(i=0; i<LED_MAX; i++)
    {
        if(led_pin[i])
            pinMode(led_pin[i], OUTPUT );
    }
}
 
int turn_led(int which, int cmd)
{
    if( which<0 || which>=LED_MAX )
        return -1;
 
    if( OFF == cmd )
    {
        if(led_pin[which])
            digitalWrite (led_pin[which], LOW); 
    }
    else 
    {
        if(led_pin[which])
            digitalWrite (led_pin[which], HIGH);
    }
 
    return 0;
}
 
 
void init_relay(int pin)
{
    relay_pin = pin;
    pinMode(relay_pin, OUTPUT);
}
 
void init_beep(int pin)
{
    beep_pin = pin;
 
    /* TBD */
    //pinMode(relay_pin, OUTPUT);
}
 
void turn_beep(int times)
{
    /* TBD */
}
 
/* wiringPi IRQ handler use a thread and can not pass argument, so we don't use interrupt */
void init_infrared(int pin)
{
    infrared_pin = pin;
    pinMode(pin, INPUT);
}
 
/*  jitter_time: cancel jitter interval time 
 * Return value: 1(HIGH): Sombody detected  0(LOW): Nobody detected */
int infrared_detect(int jitter_time)
{
    if( digitalRead(infrared_pin) )
    {
        msleep(jitter_time);
    }
    return digitalRead(infrared_pin);
}
 
void turn_relay(int cmd)
{
    if( OFF == cmd )
    {
        digitalWrite ( relay_pin, LOW );
    }
    else
    {
        digitalWrite ( relay_pin, HIGH );
    }
}