Files
hisi_SS524V100_SDK/mpp/sample/pwm/light_pwm_lvds.c
T
xu 8dd17a5b9a 修改:信号包独立发包,周期100ms;
布局切换按键,增加开机读取电平状态
2026-09-16 17:00:58 +08:00

268 lines
6.4 KiB
C

#include <linux/input.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include "keyboard.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include "confile.h"
#include "Util.h"
#define KEY_EVENT_DEV1_NAME "/dev/input/event1"
#define KEY_EVENT_DEV0_NAME "/dev/input/event0"
#define KEY_CODE_LAYOUT 0x68 /* 布局开关按键 */
#define SHM_LAYOUT_NAME "/video_switch" /* [1] = 布局开关状态,ctrl_pnl 上报面板 byte27 */
#define DEFAULT_MD_LEN 4096
int switch_video;
unsigned char key_value;
static pthread_t pkts_thread;
int count=0;
int osd_on=0;
int socket_descriptor;
struct sockaddr_in address;
void * memmap(unsigned int phy_addr, unsigned int size);
#if 0
void* pkts_worker(void *arg)
{
printf("pkts_worker...\n");
int anjian_switch=0;
uint32_t ulOld;
int read_key;
void* pMem = NULL;
while(1)
{
// pMem = memmap(0x12150000, DEFAULT_MD_LEN); //gpio0
/// ulOld = *(uint32_t *)((char *)pMem + 0x3FC); //data
// read_key = (ulOld &0x04)>>2; //get gpio0_2 value 0:FA 1:FE
// printf("read_key: %d\n\r",read_key);
// if(read_key==0x0)
// {
// mode=0;
//count=1;
//printf("key %d\n",key);
// }else
// mode= -1;
// printf("mode %d\n",mode);
anjian_switch =count;
if(anjian_switch ==(switch_video+1)){
anjian_switch =1;
}
printf("-------------%x\n",anjian_switch);
sendto(socket_descriptor,&anjian_switch,4,0,(struct sockaddr *)&address,sizeof(address));
sleep(2);
}
}
#endif
// int *switch_memory_open(void) {
// char buf[128];
// int fd;
// sprintf(buf, "/switch_mode");
// fd = shm_open(buf, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
// if(fd < 0) { perror("shm_open"); exit(1); }
// if (ftruncate(fd, 4096) == -1) { perror("ftruncate"); exit(1); }
// int *retval = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
// return retval;
// }
int *switch_memory_open(const char *name) {
int fd = shm_open(name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if(fd < 0) { perror("shm_open"); exit(1); }
if (ftruncate(fd, 4096) == -1) { perror("ftruncate"); exit(1); }
int *retval = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
return retval;
}
int get_parameter(struct decoce_param *pparam)
{
if(confile_init()) {
printf("config file parse filed!\n");
return -1;
}
pparam->light = get_conf_int("light", 0);
//pparam->osd_on = get_conf_int("osd_on", 0);
printf("light %d \n",pparam->light);
return 0;
}
/* 开机时按键可能已经按住,输入层不会上报"电平没变"的事件,只能主动查一次 */
static int key_is_down(int fd, unsigned int code)
{
unsigned long keybits[(KEY_MAX / 8 / sizeof(unsigned long)) + 1];
memset(keybits, 0, sizeof(keybits));
if(ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits) < 0)
{
printf("---EVIOCGKEY error!---\n");
return 0;
}
return (keybits[code / (8 * sizeof(unsigned long))] >>
(code % (8 * sizeof(unsigned long)))) & 1UL;
}
int main(int argc, char **argv)
{
unsigned char buf[1024];
int key_fd = 0;
struct input_event key_event = {0};
int ret;
int port;
int *video_mode;
int len;
pthread_attr_t attr;
struct decoce_param param;
int count_down=0;
int flag_down=0;
key_fd = open(KEY_EVENT_DEV1_NAME, O_RDONLY);
if(key_fd <= 0)
{
printf("---open %s device error!---\n",KEY_EVENT_DEV1_NAME);
return ret;
}
//light_mode = switch_memory_open();
video_mode = switch_memory_open(SHM_LAYOUT_NAME);
video_mode[1] = key_is_down(key_fd, KEY_CODE_LAYOUT);
get_parameter(&param);
//printf("---\n");
//switch_mode[0] = 0x01;
//light_mode[3] = param.light;
count = param.light;
//switch_mode[1] = param.osd_on;
//osd_on = param.osd_on;
sprintf(buf,"./pwm.sh %d",count);
system(buf);
printf("----%s \n",buf);
while(1)
{
ret = read(key_fd, &key_event, sizeof(key_event));
//printf("%d\n\r",ret);
if(ret)
{
//printf("key_event.type %d EV_KEY %d \n",key_event.type,EV_KEY);
//printf("key_event.value %d key_event.code %d count_down %d\n",key_event.value, key_event.code,count_down);
/* 布局开关:直接跟按键电平,按下=1 松开=0,不做翻转、不留状态 */
if(key_event.type == EV_KEY && key_event.code == KEY_CODE_LAYOUT){
video_mode[1] = key_event.value ? 1 : 0;
}
if(key_event.type == EV_KEY && key_event.value == 1 )
{
flag_down=1;
count_down=1;
}else if(key_event.type == EV_KEY && key_event.value == 2){
count_down++;
}else if(key_event.type == EV_KEY && key_event.value == 0){
if(flag_down == 1){ //短按
if((key_event.code == 0x66) )
{
//key_value = 0x01; //light down OLD_MIN 200 new_max 400 old_max 1200 new_max 1800
count=count+200; //old 200 new 300
if(count >= 1200){
count=1200;
}
set_conf_int("light",count);
save_confile("/root/light_bg.cfg");
sprintf(buf,"./pwm.sh %d",count);
system(buf);
printf("----%s \n",buf);
}else if((key_event.code == 0x67) ){
count=count-200; //old 200 new 300
if(count <= 200){
count=200; //old 200 nes 400
}
set_conf_int("light",count);
save_confile("/root/light_bg.cfg");
//light_mode[3] = count;
sprintf(buf,"./pwm.sh %d",count);
system(buf);
printf("----%s \n",buf);
}
#if 0
if((count_down > 50) && (key_event.code == 0x67)){
printf("long\n");
osd_on ^= 0x01;
//switch_mode[1] = osd_on;
// count_down=0;
//set_conf_int("osd_on",osd_on);
//save_confile("/root/decode.cfg");
}else if((count_down > 50) && (key_event.code == 0x66)){
osd_on ^= 0x01;
}
#endif
// count_down=0;
}
flag_down =0;
count_down =0;
}
}
}
}