From 0b01ca69e073c3e140f6e819be62665ad6d513ae Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Wed, 19 Oct 2016 18:19:57 +0200 Subject: nwztools/plattools: add adc test Change-Id: Ic3ef964e8b5cc7b8ca3f02f141e9e4436a4d41db --- utils/nwztools/plattools/nwz_adc.h | 42 ++++++++++++++++++++++++ utils/nwztools/plattools/nwz_fb.h | 56 ++++++++++++++++++++++++++++++++ utils/nwztools/plattools/nwz_keys.h | 2 ++ utils/nwztools/plattools/nwz_lib.c | 40 +++++++++++++++++++++-- utils/nwztools/plattools/nwz_lib.h | 11 +++++++ utils/nwztools/plattools/test_adc.c | 64 +++++++++++++++++++++++++++++++++++++ 6 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 utils/nwztools/plattools/nwz_adc.h create mode 100644 utils/nwztools/plattools/nwz_fb.h create mode 100644 utils/nwztools/plattools/test_adc.c (limited to 'utils/nwztools') diff --git a/utils/nwztools/plattools/nwz_adc.h b/utils/nwztools/plattools/nwz_adc.h new file mode 100644 index 0000000000..86b2dc7595 --- /dev/null +++ b/utils/nwztools/plattools/nwz_adc.h @@ -0,0 +1,42 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2016 Amaury Pouly + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef __NWZ_ADC_H__ +#define __NWZ_ADC_H__ + +#define NWZ_ADC_DEV "/dev/icx_adc" + +#define NWZ_ADC_TYPE 'm' + +#define NWZ_ADC_MIN_CHAN 0 +#define NWZ_ADC_MAX_CHAN 7 + +#define NWZ_ADC_VCCBAT 0 +#define NWZ_ADC_VCCVBUS 1 +#define NWZ_ADC_ADIN3 2 +#define NWZ_ADC_ADIN4 3 +#define NWZ_ADC_ADIN5 4 +#define NWZ_ADC_ADIN6 5 +#define NWZ_ADC_ADIN7 6 +#define NWZ_ADC_ADIN8 7 + +#define NWZ_ADC_GET_VAL(chan) _IOR(NWZ_ADC_TYPE, chan, unsigned char) + +#endif /* __NWZ_ADC_H__ */ diff --git a/utils/nwztools/plattools/nwz_fb.h b/utils/nwztools/plattools/nwz_fb.h new file mode 100644 index 0000000000..c857c5eb8a --- /dev/null +++ b/utils/nwztools/plattools/nwz_fb.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2016 Amaury Pouly + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef __NWZ_FB_H__ +#define __NWZ_FB_H__ + +#define NWZ_FB_LCD_DEV "/dev/fb/0" +#define NWZ_FB_TV_DEV "/dev/fb/1" + +#define NWZ_FB_TYPE 'N' + +/* How backlight works: + * + * The brightness interface is a bit strange. There 6 levels: 0 throught 5. + * Level 0 means backlight off. When changing brightness, one sets level to the + * target brightness. The driver is gradually change the brightness to reach the + * target level. The step parameters control how many hardware steps will be done. + * For example, setting step to 1 will brutally change the level in one step. + * Setting step to 2 will change brightness in two steps: one intermediate and + * finally the target one. The more steps, the more gradual the transition. The + * period parameters controls the speed to changes between steps. Using this + * interface, one can achieve fade in/out at various speeds. */ +#define NWZ_FB_BL_MIN_LEVEL 0 +#define NWZ_FB_BL_MAX_LEVEL 5 +#define NWZ_FB_BL_MIN_STEP 1 +#define NWZ_FB_BL_MAX_STEP 100 +#define NWZ_FB_BL_MIN_PERIOD 10 + +struct nwz_fb_brightness +{ + int level; /* brightness level: 0-5 */ + int step; /* number of hardware steps to do when changing: 1-100 */ + int period; /* period in ms between steps when changing: >=10 */ +}; + +#define NWZ_FB_SET_BRIGHTNESS _IOW(NWZ_FB_TYPE, 0x07, struct nwz_fb_brightness) +#define NWZ_FB_GET_BRIGHTNESS _IOR(NWZ_FB_TYPE, 0x08, struct nwz_fb_brightness) + +#endif /* __NWZ_FB_H__ */ diff --git a/utils/nwztools/plattools/nwz_keys.h b/utils/nwztools/plattools/nwz_keys.h index 8228e5b620..4a8c28737f 100644 --- a/utils/nwztools/plattools/nwz_keys.h +++ b/utils/nwztools/plattools/nwz_keys.h @@ -21,6 +21,8 @@ #ifndef __NWZ_KEYS_H__ #define __NWZ_KEYS_H__ +#define NWZ_KEY_DEV "/dev/input/event0" + /* The Sony icx_key driver reports keys via the /dev/input/event0 device and * abuses the standard struct input_event. The input_event.code is split into * two parts: diff --git a/utils/nwztools/plattools/nwz_lib.c b/utils/nwztools/plattools/nwz_lib.c index 6316217902..b654855bb8 100644 --- a/utils/nwztools/plattools/nwz_lib.c +++ b/utils/nwztools/plattools/nwz_lib.c @@ -71,7 +71,7 @@ void nwz_lcdmsgf(bool clear, int x, int y, const char *format, ...) int nwz_key_open(void) { - return open("/dev/input/event0", O_RDONLY); + return open(NWZ_KEY_DEV, O_RDONLY); } void nwz_key_close(int fd) @@ -160,7 +160,7 @@ const char *nwz_key_get_name(int keycode) int nwz_fb_open(bool lcd) { - return open(lcd ? "/dev/fb/0" : "/dev/fb/1", O_RDWR); + return open(lcd ? NWZ_FB_LCD_DEV : NWZ_FB_TV_DEV, O_RDWR); } void nwz_fb_close(int fd) @@ -183,3 +183,39 @@ int nwz_fb_set_brightness(int fd, struct nwz_fb_brightness *bl) else return 1; } + +int nwz_adc_open(void) +{ + return open(NWZ_ADC_DEV, O_RDONLY); +} + +void nwz_adc_close(int fd) +{ + close(fd); +} + +static const char *nwz_adc_name[] = +{ + [NWZ_ADC_VCCBAT] = "VCCBAT", + [NWZ_ADC_VCCVBUS] = "VCCVBUS", + [NWZ_ADC_ADIN3] = "ADIN3", + [NWZ_ADC_ADIN4] = "ADIN4", + [NWZ_ADC_ADIN5] = "ADIN5", + [NWZ_ADC_ADIN6] = "ADIN6", + [NWZ_ADC_ADIN7] = "ADIN7", + [NWZ_ADC_ADIN8] = "ADIN8", +}; + +const char *nwz_adc_get_name(int ch) +{ + return nwz_adc_name[ch]; +} + +int nwz_adc_get_val(int fd, int ch) +{ + unsigned char val; + if(ioctl(fd, NWZ_ADC_GET_VAL(ch), &val) < 0) + return -1; + else + return val; +} diff --git a/utils/nwztools/plattools/nwz_lib.h b/utils/nwztools/plattools/nwz_lib.h index 86f11ac346..5c8ad3e50c 100644 --- a/utils/nwztools/plattools/nwz_lib.h +++ b/utils/nwztools/plattools/nwz_lib.h @@ -32,6 +32,7 @@ #include "nwz_keys.h" #include "nwz_fb.h" +#include "nwz_adc.h" /* run a program and exit with nonzero status in case of error * argument list must be NULL terminated */ @@ -61,6 +62,7 @@ bool nwz_key_event_is_press(struct input_event *evt); bool nwz_key_event_get_hold_status(struct input_event *evt); /* get keycode name */ const char *nwz_key_get_name(int keycode); + /* open framebuffer device */ int nwz_fb_open(bool lcd); /* close framebuffer device */ @@ -70,4 +72,13 @@ int nwz_fb_get_brightness(int fd, struct nwz_fb_brightness *bl); /* set backlight brightness (return -1 on error, 1 on success) */ int nwz_fb_set_brightness(int fd, struct nwz_fb_brightness *bl); +/* open adc device */ +int nwz_adc_open(void); +/* close adc device */ +void nwz_adc_close(int fd); +/* get channel name */ +const char *nwz_adc_get_name(int ch); +/* read channel value, return -1 on error */ +int nwz_adc_get_val(int fd, int ch); + #endif /* _NWZLIB_H_ */ diff --git a/utils/nwztools/plattools/test_adc.c b/utils/nwztools/plattools/test_adc.c new file mode 100644 index 0000000000..52eafd042d --- /dev/null +++ b/utils/nwztools/plattools/test_adc.c @@ -0,0 +1,64 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2016 Amaury Pouly + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include "nwz_lib.h" + +int main(int argc, char **argv) +{ + /* clear screen and display welcome message */ + nwz_lcdmsg(true, 0, 0, "test_adc"); + nwz_lcdmsg(false, 0, 2, "PWR OFF: quit"); + /* open input device */ + int input_fd = nwz_key_open(); + if(input_fd < 0) + { + nwz_lcdmsg(false, 3, 7, "Cannot open input device"); + sleep(2); + return 1; + } + /* open adc device */ + int adc_fd = nwz_adc_open(); + if(adc_fd < 0) + { + nwz_lcdmsg(false, 3, 4, "Cannot open adc device"); + sleep(2); + return 1; + } + /* display input state in a loop */ + while(1) + { + /* print channels */ + for(int i = NWZ_ADC_MIN_CHAN; i <= NWZ_ADC_MAX_CHAN; i++) + nwz_lcdmsgf(false, 1, 4 + i, "%s: %d ", nwz_adc_get_name(i), + nwz_adc_get_val(adc_fd, i)); + /* wait for event (10ms) */ + int ret = nwz_key_wait_event(input_fd, 10000); + if(ret != 1) + continue; + struct input_event evt; + if(nwz_key_read_event(input_fd, &evt) != 1) + continue; + if(nwz_key_event_get_keycode(&evt) == NWZ_KEY_OPTION && !nwz_key_event_is_press(&evt)) + break; + } + /* finish nicely */ + return 0; +} + -- cgit