增加蜂鸣器和环境光的中间件
authorzf zf <2919300294@qq.com>
Thu, 1 Jun 2023 09:29:26 +0000 (17:29 +0800)
committerzf zf <2919300294@qq.com>
Thu, 1 Jun 2023 09:29:26 +0000 (17:29 +0800)
CMakeLists.txt
als/CMakeLists.txt [new file with mode: 0644]
als/inc/als.h [new file with mode: 0644]
als/src/als.c [new file with mode: 0644]
app/CMakeLists.txt
buzzer/CMakeLists.txt [new file with mode: 0644]
buzzer/inc/buzzer.h [new file with mode: 0644]
buzzer/src/buzzer.c [new file with mode: 0644]

index 876914a51644ca7adc0100a8fe6d05a036477c4d..49b7463d70f22a7ae10ae204d5778f6a55c78f61 100644 (file)
@@ -8,5 +8,7 @@ add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/button )
 add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/led )
 add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/log )
 add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/tof )
+add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/als )
+add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/buzzer )
 #add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/485 )
 add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/tuya-iot-core-sdk )
diff --git a/als/CMakeLists.txt b/als/CMakeLists.txt
new file mode 100644 (file)
index 0000000..6a76d9b
--- /dev/null
@@ -0,0 +1,24 @@
+cmake_minimum_required(VERSION 3.8)
+
+project(mg_als)
+
+SET(CMAKE_BUILD_TYPE "Debug")
+SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
+SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
+ 
+include_directories(
+       ${PROJECT_SOURCE_DIR}/./
+       ${PROJECT_SOURCE_DIR}/../log/inc/
+       ${PROJECT_SOURCE_DIR}/src/
+       ${PROJECT_SOURCE_DIR}/inc/
+       ${CMAKE_SYSROOT}/usr/include/
+       )
+
+aux_source_directory(./src/ SRCS)
+
+add_library( ${PROJECT_NAME} STATIC  ${SRCS} )
+#add_library( ${PROJECT_NAME}  ${SRCS} )
+#install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION /app/lib )
+
diff --git a/als/inc/als.h b/als/inc/als.h
new file mode 100644 (file)
index 0000000..580fe30
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef __ALS__H__
+#define __ALS__H__
+
+#include <unistd.h>
+#include <stdint.h>
+typedef enum {
+    BRIGHT = 1,  //亮
+    DARK = 0,    //暗
+}als_st_t;
+
+
+struct als_info{
+    als_st_t status;
+    uint32_t ad_value; //AD采样值
+    uint32_t threshold_low;  //阈值1
+    uint32_t threshold_high; //阈值2
+};
+typedef struct als_info als_info_t;
+
+struct mg_als{
+    char* dev_path;
+    int fd;
+    als_info_t data;
+};
+typedef struct mg_als mg_als_t;
+
+
+mg_als_t * mg_als_open( void );
+void mg_als_close( mg_als_t * als_ctrl );
+void mg_als_read( mg_als_t * als_ctrl, als_info_t *info );
+void mg_als_cfg( mg_als_t * als_ctrl, uint32_t threshold_high, uint32_t threshold_low );
+
+#endif //_ALS__H__
diff --git a/als/src/als.c b/als/src/als.c
new file mode 100644 (file)
index 0000000..f4f4188
--- /dev/null
@@ -0,0 +1,122 @@
+#include <stdio.h> 
+#include <signal.h> 
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include "mg_log.h"
+#include "als.h"
+
+#define BUFFER_SIZE 32
+
+static int32_t stop;
+static int last_staus = -1;
+static int init = 0;
+
+static mg_als_t g_als = {
+    .dev_path = "/sys/bus/iio/devices/iio:device0/in_voltage4_raw",
+};
+
+
+void* als_task( void* param )
+{
+    ssize_t bytesRead;
+    char buffer[BUFFER_SIZE];
+
+    mg_als_t *p_als = ( mg_als_t*)param;
+
+    while(!stop)
+    {
+        if(stop)
+            break;
+
+        lseek(p_als->fd,0,SEEK_SET); 
+               bytesRead = read(p_als->fd,buffer,BUFFER_SIZE -1);
+        if(bytesRead < 0){
+            MG_LOGE("read %s failed\n",p_als->fd);
+            goto error;
+        }
+        buffer[bytesRead] = '\0';
+        p_als->data.ad_value = atoi(buffer);
+        if(init)
+        {
+            if(p_als->data.ad_value > ((p_als->data.threshold_high >> 1) + (p_als->data.threshold_low >> 1))){
+                p_als->data.status = BRIGHT;
+            }else{
+                p_als->data.status = DARK;
+            }
+            init = 0;
+        }else if(p_als->data.ad_value > (p_als->data.threshold_high)){
+            p_als->data.status = BRIGHT;
+            
+        }else if(p_als->data.ad_value < (p_als->data.threshold_low)){
+            p_als->data.status = DARK;
+        }
+        usleep(10000);
+    }
+error:
+    return NULL;
+}
+
+
+mg_als_t * mg_als_open( void )
+{   
+    pthread_t als_thread;
+
+    stop = 0;
+    if ((g_als.fd = open(g_als.dev_path,O_RDONLY)) < 0){
+               MG_LOGE("als open in_voltage4_raw");
+               goto error;
+       }
+
+    if( pthread_create( &als_thread, NULL, als_task, (void *)&g_als) != 0 ){
+               MG_LOGE("create als task!");
+               goto error;
+       }
+
+    return &g_als;
+error:
+    return NULL;
+}
+
+
+void mg_als_close( mg_als_t * als_ctrl )
+{
+    stop = 1;
+    usleep(20);
+    if(als_ctrl)
+    {
+        close(als_ctrl->fd);
+        MG_LOGD( "tw8013 als close!\n" );
+    }else{
+        MG_LOGD( "close Parameters are empty !!!\n" );
+    }
+}
+
+
+void mg_als_read( mg_als_t * als_ctrl, als_info_t *info )
+{
+ 
+    usleep(5000);
+    if(!stop){
+         info->ad_value = als_ctrl->data.ad_value;
+    }else{
+        info->ad_value = 44444;  //Invalid value
+    }
+    info->threshold_high = als_ctrl->data.threshold_high;
+    info->threshold_low  = als_ctrl->data.threshold_low;
+    info->status         = als_ctrl->data.status;
+}
+
+
+void mg_als_cfg( mg_als_t * als_ctrl, uint32_t threshold_high, uint32_t threshold_low )
+{
+    if(!stop){
+        als_ctrl->data.threshold_high = threshold_high;
+        als_ctrl->data.threshold_low  = threshold_low;
+        init = 1;
+    }else{
+        MG_LOGD("als stop cannot be set");
+    }
+}
\ No newline at end of file
index a91f08b8d4bd80474ef7e83c3872a448c6a47f6b..673a8d9b06dccfa6fe385f8a1a7c666b1f904c1f 100644 (file)
@@ -17,6 +17,8 @@ include_directories(
        ${PROJECT_SOURCE_DIR}/../button/inc/
        ${PROJECT_SOURCE_DIR}/../led/inc/
        ${PROJECT_SOURCE_DIR}/../tof/inc/
+       ${PROJECT_SOURCE_DIR}/../als/inc/
+       ${PROJECT_SOURCE_DIR}/../buzzer/inc/
        ${PROJECT_SOURCE_DIR}/../log/inc/
        ${CMAKE_SYSROOT}/usr/include/
        )
@@ -39,6 +41,8 @@ target_link_libraries(
        mg_button
        mg_tof
        mg_led
+       mg_als
+       mg_buzzer
        mg_log
        log4c
        rt
diff --git a/buzzer/CMakeLists.txt b/buzzer/CMakeLists.txt
new file mode 100644 (file)
index 0000000..85d1d8c
--- /dev/null
@@ -0,0 +1,23 @@
+cmake_minimum_required(VERSION 3.8)
+
+project(mg_buzzer)
+
+SET(CMAKE_BUILD_TYPE "Debug")
+SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
+SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
+ 
+include_directories(
+       ${PROJECT_SOURCE_DIR}/./
+       ${PROJECT_SOURCE_DIR}/../log/inc/
+       ${PROJECT_SOURCE_DIR}/src/
+       ${PROJECT_SOURCE_DIR}/inc/
+       ${CMAKE_SYSROOT}/usr/include/
+       )
+
+aux_source_directory(./src/ SRCS)
+
+add_library( ${PROJECT_NAME} STATIC  ${SRCS} )
+#add_library( ${PROJECT_NAME}  ${SRCS} )
+#install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION /app/lib )
diff --git a/buzzer/inc/buzzer.h b/buzzer/inc/buzzer.h
new file mode 100644 (file)
index 0000000..d0d412b
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef __BUZZER__H__
+#define __BUZZER__H__
+
+#include <stdio.h>
+#include <stdint.h>
+#include <time.h> 
+
+
+struct buzzer_status{
+       float freq_hz;       
+       int32_t duty_cycle;
+};
+
+typedef struct buzzer_status buzzer_status_t;
+
+struct mg_buzzer{
+       char*  dev_path[5];
+       uint32_t enable;
+       uint32_t time_ms;
+       buzzer_status_t buzzer_x_t[2];
+};
+
+typedef struct mg_buzzer mg_buzzer_t;
+
+mg_buzzer_t * mg_buzzer_open(void);
+void mg_buzzer_close(mg_buzzer_t * buzzer_ctrl);
+void mg_buzzer_start( mg_buzzer_t * buzzer_ctrl, float freq_hz, uint32_t duty_cycle, uint32_t time_ms );
+void mg_buzzer_get( mg_buzzer_t * buzzer_ctrl, float *freq_hz, uint32_t *duty_cycle, uint32_t *time_ms, uint32_t* enable);
+
+#endif //__BUZZER__H__
+
diff --git a/buzzer/src/buzzer.c b/buzzer/src/buzzer.c
new file mode 100644 (file)
index 0000000..4b4101a
--- /dev/null
@@ -0,0 +1,214 @@
+#include <stdio.h> 
+#include <signal.h> 
+#include <string.h> 
+#include <stdlib.h> 
+#include <unistd.h> 
+#include <math.h>
+#include "mg_log.h"
+#include "buzzer.h"
+
+
+
+static uint32_t init = 0;
+static timer_t timerid;
+
+
+mg_buzzer_t buzzer_init ={
+       .enable   = 0,
+       .dev_path[0] = "export",
+       .dev_path[1] = "unexport",
+       .dev_path[2] = "pwm0/period",
+       .dev_path[3] = "pwm0/duty_cycle",
+       .dev_path[4] = "pwm0/enable",
+       
+
+};
+
+static int32_t mg_buzzer_start_t(uint32_t parameter ,char* path)
+{
+       char system_cmd[100];
+
+       sprintf(system_cmd, "echo %d > /sys/class/pwm/pwmchip6/%s ", parameter, path);
+       if(system(system_cmd)){
+               MG_LOGE( "system set [%s] failed\r\n",path);
+               return -1;
+       }
+       return 0;
+}
+
+
+
+static void mg_buzzer_enable( mg_buzzer_t *buzzer_x )
+{
+       mg_buzzer_start_t(1 , buzzer_x->dev_path[4]);
+       buzzer_x->enable = 1;
+}
+
+static int32_t mg_buzzer_disabled(mg_buzzer_t *buzzer_x )
+{
+       mg_buzzer_start_t(0 , buzzer_x->dev_path[4]);
+       buzzer_x->enable = 0;
+       return 0;
+}
+
+static void buzzer_timer_thread(union sigval v) 
+{
+    MG_LOGD("timer_thread function! %d\n", v.sival_int); 
+       mg_buzzer_disabled(&buzzer_init);
+} 
+ 
+
+static void mg_buzzer_timer_init()
+{
+       struct sigevent evp; 
+    memset(&evp, 0, sizeof(struct sigevent));           
+
+    evp.sigev_value.sival_int = 1;                        
+    evp.sigev_notify = SIGEV_THREAD;                      
+    evp.sigev_notify_function = buzzer_timer_thread;        
+ 
+    if (timer_create(CLOCK_REALTIME, &evp, &timerid) == -1) 
+    {   
+        MG_LOGE("buzzer fail to timer_create! \r\n"); 
+        exit(-1); 
+    }  
+       else{
+               MG_LOGD( " buzzer create timer OK !\r\n");      
+       }
+       return; 
+}
+
+
+void mg_buzzer_get( mg_buzzer_t * buzzer_ctrl, float *freq_hz, uint32_t *duty_cycle, uint32_t *time_ms, uint32_t* enable)
+{
+       MG_LOGD( "tw8013 buzzer get!\n" );
+       
+       if(buzzer_ctrl->enable)
+       {
+               *freq_hz    = buzzer_ctrl->buzzer_x_t[1].freq_hz;
+               *duty_cycle = buzzer_ctrl->buzzer_x_t[1].duty_cycle ;
+               *time_ms    = buzzer_ctrl->time_ms;
+               *enable     = buzzer_ctrl->enable;
+       }else{
+               *freq_hz    = 0;
+               *duty_cycle = 0;
+               *time_ms    = 0;
+               *enable     = 0;
+       }
+}
+
+
+static int32_t mg_buzzer_set( mg_buzzer_t * buzzer_ctrl, buzzer_status_t* buzzer_t)
+{
+       buzzer_status_t buzzer_x;
+
+       buzzer_x.freq_hz    = (1 / buzzer_t->freq_hz) * pow(10,9);
+       buzzer_x.duty_cycle = buzzer_t->duty_cycle * (buzzer_x.freq_hz / 100);
+       
+       if(buzzer_ctrl->enable){
+               mg_buzzer_disabled(buzzer_ctrl);
+       }
+
+       if(init){
+               //clean duty_cycle 
+               if(mg_buzzer_start_t(0,buzzer_ctrl->dev_path[3]))
+                       goto err;
+       }
+       init = 1;
+
+       if(mg_buzzer_start_t((int)buzzer_x.freq_hz,buzzer_ctrl->dev_path[2]))
+               goto err;
+       
+       if(mg_buzzer_start_t(buzzer_x.duty_cycle,buzzer_ctrl->dev_path[3])){
+               goto err;
+       }else{
+               mg_buzzer_enable(buzzer_ctrl);
+               buzzer_ctrl->buzzer_x_t[1].freq_hz    = buzzer_ctrl->buzzer_x_t[0].freq_hz;
+               buzzer_ctrl->buzzer_x_t[1].duty_cycle = buzzer_ctrl->buzzer_x_t[0].duty_cycle;
+       }
+       return 0;
+err:
+       MG_LOGE( "buzzer set failed \r\n");     
+       return -1;
+}
+
+
+void mg_buzzer_start( mg_buzzer_t * buzzer_ctrl, float freq_hz, uint32_t duty_cycle, uint32_t time_ms )
+{
+       buzzer_status_t tmp;
+
+       if(freq_hz == 0){
+               if(buzzer_ctrl->enable){
+                       mg_buzzer_disabled(buzzer_ctrl);
+               }
+               buzzer_ctrl->buzzer_x_t[1].freq_hz    = freq_hz;
+               buzzer_ctrl->buzzer_x_t[1].duty_cycle = duty_cycle;
+               buzzer_ctrl->time_ms                  = time_ms;
+       }else if((freq_hz >= 1) && (freq_hz <= 10000)){
+               //Duty cycle limitation
+               if(duty_cycle > 100)
+                       duty_cycle = 100;
+               else if(duty_cycle < 0)
+                       duty_cycle = 0;
+
+               tmp.freq_hz    = freq_hz;
+               tmp.duty_cycle = duty_cycle;
+
+               buzzer_ctrl->time_ms = time_ms;
+               buzzer_ctrl->buzzer_x_t[0].freq_hz      = freq_hz;
+               buzzer_ctrl->buzzer_x_t[0].duty_cycle = duty_cycle;
+
+               if( time_ms ){
+                       //start timer...
+                       struct itimerspec it;  
+
+                       it.it_interval.tv_sec = 0;
+                       it.it_interval.tv_nsec = 0;
+                       it.it_value.tv_sec = time_ms/1000; 
+                       it.it_value.tv_nsec =  1000*1000 *(time_ms%1000);
+
+                       if (timer_settime(timerid, 0, &it, NULL) == -1){
+                               MG_LOGE( "buzzer start timer fail!\r\n");
+                       }else{
+                               MG_LOGD( "buzzer timer start\r\n"); 
+                       }
+               }
+               mg_buzzer_set(buzzer_ctrl,&tmp);
+       }else{
+               MG_LOGE( "period Out of range Please enter within 1 to 10000\r\n"); 
+       }
+}
+
+
+mg_buzzer_t * mg_buzzer_open(void)
+{
+
+       MG_LOGD( "tw8013 buzzer open...\n");
+       
+       if(access("/sys/class/pwm/pwmchip6/pwm0", 0))
+       {
+               if(mg_buzzer_start_t(0,buzzer_init.dev_path[0]))
+               {
+                       MG_LOGE( "tw8013 buzzer open failed ...\n");
+                       return NULL;
+               }
+       }       
+       mg_buzzer_timer_init();
+       return &buzzer_init;
+}
+
+
+void mg_buzzer_close(mg_buzzer_t * buzzer_ctrl)
+{
+       MG_LOGD( "tw8013 buzzer close...\n");
+       
+       init = 0;
+       if(buzzer_ctrl){
+               if(!access("/sys/class/pwm/pwmchip6/pwm0", 0))
+               {
+                       mg_buzzer_disabled(buzzer_ctrl);
+                       mg_buzzer_start_t(0,buzzer_ctrl->dev_path[3]);
+                       mg_buzzer_start_t(0,buzzer_ctrl->dev_path[1]);
+               }       
+       }
+}
\ No newline at end of file