首页 > 其他分享 >PRO-1:停车系统框架完善

PRO-1:停车系统框架完善

时间:2022-11-29 18:35:26浏览次数:32  
标签:pwm led 框架 temp PRO 停车 val include void


---main.c

---include

                 :key.h

:led.h

:my.h

:pwm.h

---lib

              :fun.c

:key.c

:led.c

pwm.c



--------------------------------main.c------------------------------

#include <my.h>
#include"key.h"
#include"led.h"
#include"pwm.h"


int main(void)
{
USR *user = NULL;


user = user_init();
led_init();
key_init();
pwm_open();
pwm_outset();
led_outset();


for(;;)
{
input_userinfo(user);
start_time(user);
printf("PRESS ENTER KEY CONTINUE...\n");
getchar();
end_time(user);
print_userinfo(user);
}
user_exit(user);
led_exit();
key_exit();
pwm_close();
return 0;
}

----------------------------------fun.c----------------------------------------

#include <my.h>
#include"key.h"


USR *user_init(void)
{
USR *temp = NULL;


if((temp = (USR *)malloc(sizeof(USR))) == NULL)
{
perror("MALLOC ERROR");
exit(EXIT_FAILURE);
}
return temp;
}




void user_exit(USR *temp)
{
free(temp);
return;
}


void input_userinfo(USR *temp)
{
int num;
printf("===WELCOME===\n");
printf("Please input name:>");
scanf("%s", temp->name);
printf("Please input card:>");
scanf("%s", temp->card);


FLAG:
printf("TYPE LIST:\n");
printf("1----20/h\n");
printf("2----15/h\n");
printf("3----10/h\n");
printf("4----5/h\n");
printf("please choose:>");
num=key_read();
pwm_outset();
switch(num)
{
case 1: temp->type=1;
temp->money=20.00f;
break;
case 2: temp->type=2;
temp->money=15.00f;
break;
case 3: temp->type=3;
temp->money=10.00f;
break;
case 4: temp->type=4;
temp->money=5.00f;
break;
default:
printf("ERROR, INPUT AGAIN!");
goto FLAG;
}
/* scanf("%d", &temp->type);


switch(temp->type)
{
case 1: temp->money = 20.00f; break;
case 2: temp->money = 15.00f; break;
case 3: temp->money = 10.00f; break;
case 4: temp->money = 5.00f; break;
default:
printf("ERROR, INPUT AGAIN!");
goto FLAG;
}
*/
getchar();
return;
}


void start_time(USR *temp)
{
time_t mytm;
struct tm *val = NULL;


mytm = time(NULL);
val = localtime(&mytm);


memcpy(&temp->start_time, val, sizeof(struct tm));
printf("start time: %d-%d-%d %d:%d:%d\n", val->tm_year+1900, val->tm_mon+1\
,val->tm_mday, val->tm_hour, val->tm_min, val->tm_sec);
return;
}




void end_time(USR *temp)
{
time_t mytm;
struct tm *val = NULL;


mytm = time(NULL);
val = localtime(&mytm);


memcpy(&temp->end_time, val, sizeof(struct tm));
printf("end time: %d-%d-%d %d:%d:%d\n", val->tm_year+1900, val->tm_mon+1\
,val->tm_mday, val->tm_hour, val->tm_min, val->tm_sec);
return;
}


void print_userinfo(USR *temp)
{
int val;


val = temp->end_time.tm_sec - temp->start_time.tm_sec;
if(val < 0)
val += 60;


printf("NAME:%s\n", temp->name);
printf("CARD:%s\n", temp->card);
printf("TYPE:%d\n", temp->type);
printf("MONEY:%.2f\n", temp->money * val);
printf("==BYEBYE===\n");
return;
}


------------------------------------------key.c----------------------------------------------

#include"key.h"




void key_init(void)
{
if((key_fd=open(KEY_NAME,O_RDWR))<0)
{
perror("OPEN FAIL");
exit(-1);
}
}


void key_exit(void)
{
if(close(key_fd)<0)
{
perror("EXIT FAIL");
exit(-1);
}
}
int key_read(void)
{
int num;


while(read(key_fd,&num,sizeof(int))<0);
return num;
}

--------------------------------------------led.c----------------------------------------------

#include"led.h"




void led_init()
{
if((led_fd=open(LED_NAME,O_RDWR))<0)
{
perror("OPEN FAIL");
exit(-1);
}


}
void led_exit(void)
{
if(close(led_fd)<0)
{
perror("EXIT FAIL");
exit(-1);
}
}
void led_on(int num)
{
if(ioctl(led_fd,CMD_LED_ON,num)<0)
{
perror("ON FAIL");
exit(-1);
}
}
void led_off(int num)
{
if(ioctl(led_fd,CMD_LED_OFF,num)<0)
{
perror("OFF FAIL");
exit(-1);
}
}
void led_outset(void)
{
led_on(1);
led_on(2);
led_on(3);
usleep(50000);
led_off(1);
led_off(2);
led_off(3);


led_on(1);
usleep(50000);
led_off(1);
led_on(2);
usleep(50000);
led_off(2);
led_on(3);
usleep(50000);
led_off(3);


}

----------------------------------------pwm.c---------------------------------------------------

#include"pwm.h"




void pwm_open(void)
{
if((pwm_fd=open("/dev/pwm",O_RDWR))<0)
{
perror("OPEN FILE FAIL!!!");
exit(EXIT_FAILURE);
}
}
void pwm_close(void)
{
close(pwm_fd);
}
void pwm_fre(int num)
{
ioctl(pwm_fd,SET_FRE,num);
}
void pwm_beep_on(void)
{
ioctl(pwm_fd,BEEP_ON);
}
void pwm_beep_off(void)
{
ioctl(pwm_fd,BEEP_OFF);
}
void pwm_outset(void)
{
int i,wide=100;
for (i=0; i<3; i++)
{
wide+=1500;
pwm_fre(wide);
pwm_beep_on();
usleep(50000);
pwm_beep_off();
}
}

-----------------------------------------------key.h--------------------------------------------------

#ifndef _KEY_H_
#define _KEY_H_


#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>


#define KEY_NAME "/dev/key"


int key_fd;
void key_init(void);
void key_exit(void);
int key_read(void);
#endif

---------------------------------------led.h--------------------------------------------------------------

#ifndef _LED_H_
#define _LED_H_  
 
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
 
#define LED_NAME "/dev/led"
#define CMD_LED_ON _IO('k',0)
#define CMD_LED_OFF _IO('k',1)


int led_fd;


void led_init(void);
void led_exit(void);
void led_on(int);
void led_off(int);


#endif

----------------------------------------------my.h-------------------------------------------------------

#ifndef _MY_H_
#define _MY_H_


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


#define LEN 20


typedef struct  
{
char name[20];
char card[20];
int type;
float money;
struct tm start_time;
struct tm end_time;
}USR;


USR *user_init(void);
void user_exit(USR *);
void input_userinfo(USR *);
void start_time(USR *);
void end_time(USR *);
void print_userinfo(USR *);


#endif

-----------------------------------------------pwm.h--------------------------------------------------

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define magic_number 'k'
#define BEEP_ON _IO(magic_number,0)
#define BEEP_OFF _IO(magic_number,1)
#define SET_FRE _IO(magic_number,2)


int pwm_fd;
void pwm_open(void);
void pwm_close(void);
void pwm_fre(int);
void pwm_beep_on(void);
void pwm_beep_off(void);

标签:pwm,led,框架,temp,PRO,停车,val,include,void
From: https://blog.51cto.com/u_12853553/5896516

相关文章

  • 移动跨平台开发框架的演变、对比与选择
    随着移动互联网技术的飞速发展,智能终端迅速普及推广,而原有的NativeApp有一个明显痛点——就是相同的功能需要在不同的平台上都实现一遍,显然,这种开发模式已经无法满足企......
  • ReadProcessMemory函数的分析
    ReadProcessMemory函数用于读取其他进程的数据。我们知道自远古时代结束后,user模式下的进程都有自己的地址空间,进程与进程间互不干扰,这叫私有财产神圣不可侵犯。但windows里......
  • ArcObjects SDK开发 007 自定义App-Command-Tool框架
    1、为什么再设计一套App-Command-Tool框架为什么我们要自己再设计一套App-Command框架,而不直接使用AOAPI中的AxControl-ICommand这套已经非常好的框架呢?1、宿主不同。我......
  • WPF_MVVM框架(5)
    1.NuGet引用MVVM框架包引入该框架包之后,默认会在目录下创建ViewModel层的示例代码2.第二步,通过在MainViewModel中创建一些业务代码,将其与MainWindow.xaml通过......
  • Kafka 生产者Producer
    Kafka生产者Producer原理producer和consumer过去直接与Zookeeper连接,以获得这些信息。现在Kafka已经脱离了这种耦合,从0.8版和0.9版开始,客户端直接从Kafkabrokers那里......
  • android自定义view实现progressbar的效果
    一键清理是很多Launcher都会带有的功能,其效果也比较美观。实现方式也许有很多中,其中常见的是使用图片drawable来完成的,具体可以参考这篇文章:​​模仿......
  • android studio添加project libs库步骤
    在Eclipse中选择要导出的项目,然后依次选择菜单file->export->Android->GenerateGradlebuild files.之后依次点击next到finish即可回到Androidstudio打开settin......
  • Backtrader - multiprocessing.pool.MaybeEncodingError: Error sending result
    1.ErrorMessagemultiprocessing.pool.MaybeEncodingError:Errorsendingresult:'[<backtrader.cerebro.OptReturnobjectat0x000002DF30503CF8>]'.Reason:'Pickl......
  • JS中的 Object.defineProperty
    Object.defineProperty​​Object.defineProperty()​​方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性,并返回此对象。语法:Object.defineProperty(obj,p......
  • sqlplus导入sql命令报错ORA-01756: quoted string not properly terminated
    ORA-01756:quotedstringnotproperlyterminatedsqlplus中使用@sql文件执行sql表结构脚本后,sql脚本中存在中文注释时会报错,如下图所示查看数据库字符集#检查数据库......