垃圾桶项目代码

这个项目目前来说已经基本完成,这里主要是分享一下代码,后续可能会根据垃圾桶硬件模块的增加更新一些新的代码。

首先是garbage.h的代码

#ifndef __GARBAGE__H
#define __GARBAGE__H
void garbage_init(void);
void garbage_final(void);
char *garbage_category(char *category);
#define WGET_CMD "wget http://127.0.0.1:8080/?action=snapshot -O/tmp/garbage.jpg"
#define GARBAGE_FILE "/tmp/garbage.jpg"
#endif

然后是pwm.h的代码

#ifndef __PWM__H
#define __PWM__H
#define PWM_GARBAGE 7
#define PWM_RECOVERABLE_GARBAGE 5
void pwm_write(int pwm_pin);
void pwm_stop(int pwm_pin);
#endif

接着是uartTool.c的代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "wiringSerial.h"
int myserialOpen (const char *device, const int baud)
{
   struct termios options ;
   speed_t myBaud ;
   int status, fd ;
   switch (baud){
   case  9600:  myBaud = B9600 ; break ;
   case 115200:  myBaud = B115200 ; break ;
   }
   if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) ==
-1)
   return -1 ;
   fcntl (fd, F_SETFL, O_RDWR) ;

   tcgetattr (fd, &options) ;
   cfmakeraw  (&options) ;
   cfsetispeed (&options, myBaud) ;
   cfsetospeed (&options, myBaud) ;
   options.c_cflag |= (CLOCAL | CREAD) ;
   options.c_cflag &= ~PARENB ;
   options.c_cflag &= ~CSTOPB ;
   options.c_cflag &= ~CSIZE ;
   options.c_cflag |= CS8 ;
   options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
   options.c_oflag &= ~OPOST ;
   options.c_cc [VMIN] =  0 ;
   options.c_cc [VTIME] = 100 ;
   tcsetattr (fd, TCSANOW, &options) ;


   ioctl (fd, TIOCMGET, &status);
   status |= TIOCM_DTR ;
   status |= TIOCM_RTS ;
   ioctl (fd, TIOCMSET, &status);
   usleep (10000) ;
   return fd ;
}
void serialSendstring (const int fd, const unsigned char *s, int len)
{
   int ret;
   ret = write (fd, s, len);
   if (ret < 0)
      printf("Serial Puts Error\n");
}
int serialGetstring (const int fd, unsigned char *buffer)
{
   int n_read;
   n_read = read(fd, buffer,32);
   return n_read;
}

以及uratTool.h的代码

#ifndef __UARTTOOL_H
#define __UARTTOOL_H
int myserialOpen (const char *device, const int baud);
void serialSendstring (const int fd, const unsigned char *s, int len);
int serialGetstring (const int fd, unsigned char *buffer);
#define SERIAL_DEV "/dev/ttyS5"
#define BAUD 115200
#endif

最后是main.c代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <wiringPi.h>
#include <pthread.h>
#include "uartTool.h"
#include "garbage.h"
#include "pwm.h"
int serial_fd = -1;
pthread_cond_t cond;
pthread_mutex_t mutex;
static int detect_process(const char *process_name)
{ 
    int n = -1;
    FILE *strm; 
    char buf[128]={0}; 
    sprintf(buf,"ps -ax | grep %s|grep -v grep", process_name); 
    if((strm = popen(buf, "r")) != NULL) { 
        if(fgets(buf, sizeof(buf), strm) != NULL){
            printf("buf=%s\n",buf);
            n = atoi(buf);
            printf("n=%d\n",n);
        }
    }else{
        return -1;
    }
    pclose(strm); 
    return n; 
}
void *pget_voice(void *arg)
{
    unsigned char buffer[6] = {0xAA, 0x55, 0x00, 0x00, 0X55, 0xAA};
    int len = 0;
    printf("%s|%s|%d\n", __FILE__, __func__, __LINE__);
    if (-1 == serial_fd){
        printf("%s|%s|%d: open serial failed\n", __FILE__, __func__, __LINE__);
        pthread_exit(0);
    }
    printf("%s|%s|%d\n", __FILE__, __func__, __LINE__);
    while(1){
        len = serialGetstring(serial_fd, buffer);
        printf("%s|%s|%d, len=%d\n", __FILE__, __func__, __LINE__,len);
        if (len > 0 && buffer[2] == 0x46){
            printf("%s|%s|%d\n", __FILE__, __func__, __LINE__);
            pthread_mutex_lock(&mutex);
            buffer[2] = 0x00;
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&mutex);
        }
    }
    pthread_exit(0);
}
void *psend_voice(void *arg)
{
    pthread_detach(pthread_self());
    unsigned char *buffer = (unsigned char *)arg;
    if (-1 == serial_fd){
        printf("%s|%s|%d: open serial failed\n", __FILE__, __func__, __LINE__);
        pthread_exit(0);
    }
    if (NULL != buffer){
        serialSendstring(serial_fd, buffer, 6);
    }
    pthread_exit(0);
}
void *popen_trash_can(void *arg)
{
    pthread_detach(pthread_self());
    unsigned char *buffer = (unsigned char *)arg;
    if (buffer[2] == 0x43){
        printf("%s|%s|%d: buffer[2]=0x%x\n", __FILE__, __func__, __LINE__,buffer[2]);
        pwm_write(PWM_RECOVERABLE_GARBAGE);
        delay(2000);
        pwm_stop(PWM_RECOVERABLE_GARBAGE);
    }else if (buffer[2] != 0x45){
        printf("%s|%s|%d: buffer[2]=0x%x\n", __FILE__, __func__, __LINE__,buffer[2]);
        pwm_write(PWM_GARBAGE);
        delay(2000);
        pwm_stop(PWM_GARBAGE);
    }
    pthread_exit(0);
}
void *pcategory(void *arg)
{
    unsigned char buffer[6] = {0xAA, 0x55, 0x00, 0x00, 0X55, 0xAA}; 
    char *category = NULL;
    pthread_t send_voice_tid, trash_tid;
    while (1){
        printf("%s|%s|%d: \n", __FILE__, __func__, __LINE__);
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        pthread_mutex_unlock(&mutex);
        printf("%s|%s|%d: \n", __FILE__, __func__, __LINE__);
        buffer[2] = 0x00;
        system(WGET_CMD);
        if (0 == access(GARBAGE_FILE, F_OK)){
            category = garbage_category(category);
            if (strstr(category, "干垃圾")){
                buffer[2] = 0x41;
            }else if (strstr(category, "湿垃圾")){
                buffer[2] = 0x42;
            }else if (strstr(category, "可回收垃圾")){
                buffer[2] = 0x43;
            }else if (strstr(category, "有害垃圾")){
                buffer[2] = 0x44;
            }else{
                buffer[2] = 0x45;
            }
        }else{
            buffer[2] = 0x45;
        }
        //开垃圾桶开关
    pthread_create(&trash_tid, NULL, psend_voice, (void *)buffer);
        //开语音播报线程
    pthread_create(&send_voice_tid, NULL, popen_trash_can, (void *)buffer);
        //buffer[2] = 0x00;
    remove(GARBAGE_FILE);
    }
    pthread_exit(0);
}
int main(int argc, char *argv[])
{
    int len = 0;
    int ret = -1;
    char *category = NULL;
    pthread_t get_voice_tid, category_tid;
    wiringPiSetup();
    garbage_init();
    ret = detect_process("mjpg_streamer");
    if ( -1 == ret){
        printf("detect process failed\n");
        goto END;
    }
    serial_fd = myserialOpen(SERIAL_DEV, BAUD);
    if (-1 == serial_fd){
        printf("open serial failed\n");
        goto END;
    }
    //开语音线程
    printf("%s|%s|%d\n", __FILE__, __func__, __LINE__);
    pthread_create(&get_voice_tid, NULL, pget_voice, NULL);
    //开阿里云交互线程
    printf("%s|%s|%d\n", __FILE__, __func__, __LINE__);
    pthread_create(&category_tid, NULL, pcategory, NULL);
    pthread_join(get_voice_tid, NULL);
    pthread_join(category_tid, NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    close(serial_fd);
END:
    garbage_final();
    return 0;
}

最后执行下面的命令就可以运行了

gcc -o test *.c *.h -I /usr/include/python3.10/ -lpython3.10 -lwiringPi
sudo -E ./test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值