linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Szymon Janc <szymon.janc@gmail.com>
To: Antonio Ospite <ao2@ao2.it>
Cc: linux-bluetooth@vger.kernel.org,
	Bastien Nocera <hadess@hadess.net>,
	Frank Praznik <frank.praznik@oh.rr.com>
Subject: Re: [PATCH BlueZ 3/5] plugins/sixaxis: factor out a calc_leds_bitmap() function
Date: Wed, 07 May 2014 21:59:02 +0200	[thread overview]
Message-ID: <2381743.AtH49zkCbR@athlon> (raw)
In-Reply-To: <1399370776-5027-4-git-send-email-ao2@ao2.it>

Hi Antonio,

On Tuesday 06 May 2014 12:06:14 Antonio Ospite wrote:
> This is also in preparation of set_leds_sysfs().
> ---
>  plugins/sixaxis.c | 36 ++++++++++++++++++++++++------------
>  1 file changed, 24 insertions(+), 12 deletions(-)
> 
> diff --git a/plugins/sixaxis.c b/plugins/sixaxis.c
> index 61fd0b5..b629c06 100644
> --- a/plugins/sixaxis.c
> +++ b/plugins/sixaxis.c
> @@ -125,7 +125,25 @@ static int set_master_bdaddr(int fd, const bdaddr_t
> *bdaddr) return ret;
>  }
> 
> -static gboolean set_leds_hidraw(int fd, int number)
> +static gboolean calc_leds_bitmap(int number, uint8_t *bitmap)
> +{

Make this return bool. Don't use glib types if this is not required due to use 
of glib API.

> +	*bitmap = 0;

Move this after number check. It is usually good to make function leave no 
side effects (if possible) if it failed.

You could also make this helper simply return bitmap value and then treat 0 as 
error.

> +
> +	/* TODO we could support up to 10 (1 + 2 + 3 + 4) */
> +	if (number > 7)
> +		return FALSE;
> +
> +	if (number > 4) {
> +		*bitmap |= 0x10;
> +		number -= 4;
> +	}
> +
> +	*bitmap |= 0x01 << number;
> +
> +	return TRUE;
> +}
> +
> +static gboolean set_leds_hidraw(int fd, uint8_t leds_bitmap)
>  {
>  	/*
>  	 * the total time the led is active (0xff means forever)
> @@ -148,16 +166,7 @@ static gboolean set_leds_hidraw(int fd, int number)
>  	};
>  	int ret;
> 
> -	/* TODO we could support up to 10 (1 + 2 + 3 + 4) */
> -	if (number > 7)
> -		return FALSE;
> -
> -	if (number > 4) {
> -		leds_report[10] |= 0x10;
> -		number -= 4;
> -	}
> -
> -	leds_report[10] |= 0x01 << number;
> +	leds_report[10] = leds_bitmap;
> 
>  	ret = write(fd, leds_report, sizeof(leds_report));
>  	if (ret == sizeof(leds_report))
> @@ -175,6 +184,7 @@ static gboolean setup_leds(GIOChannel *channel,
> GIOCondition cond, gpointer user_data)
>  {
>  	int number = GPOINTER_TO_INT(user_data);
> +	uint8_t bitmap = 0;
>  	int fd;
> 
>  	if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
> @@ -183,7 +193,9 @@ static gboolean setup_leds(GIOChannel *channel,
> GIOCondition cond, DBG("number %d", number);
> 
>  	fd = g_io_channel_unix_get_fd(channel);
> -	set_leds_hidraw(fd, number);
> +
> +	if (calc_leds_bitmap(number, &bitmap))
> +		set_leds_hidraw(fd, bitmap);
> 
>  	return FALSE;
>  }

-- 
Szymon K. Janc
szymon.janc@gmail.com

  reply	other threads:[~2014-05-07 19:59 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-06 10:06 [PATCH BlueZ 0/5] plugin/sixaxis: use the sysfs leds class Antonio Ospite
2014-05-06 10:06 ` [PATCH BlueZ 1/5] plugins/sixaxis: simplify get_js_numbers() Antonio Ospite
2014-05-07 19:57   ` Szymon Janc
2014-05-06 10:06 ` [PATCH BlueZ 2/5] plugins/sixaxis: factor out a set_leds_hidraw() function Antonio Ospite
2014-05-07 19:58   ` Szymon Janc
2014-05-06 10:06 ` [PATCH BlueZ 3/5] plugins/sixaxis: factor out a calc_leds_bitmap() function Antonio Ospite
2014-05-07 19:59   ` Szymon Janc [this message]
2014-05-06 10:06 ` [PATCH BlueZ 4/5] plugins/sixaxis: add a get_leds_data() function Antonio Ospite
2014-05-07 20:04   ` Szymon Janc
2014-05-09 12:22   ` Johan Hedberg
2014-05-09 13:31     ` Antonio Ospite
2014-05-06 10:06 ` [PATCH BlueZ 5/5] plugins/sixaxis: add a set_leds_sysfs() function Antonio Ospite
2014-05-07 20:24   ` Szymon Janc
2014-05-14 21:40 ` [PATCHv2 BlueZ 0/5] plugin/sixaxis: Set leds using the sysfs leds class Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 2/5] plugins/sixaxis: Factor out a set_leds_hidraw() function Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 3/5] plugins/sixaxis: Factor out a calc_leds_bitmap() function Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 4/5] plugins/sixaxis: Add a get_leds_data() function Antonio Ospite
2014-05-15 20:25     ` Szymon Janc
2014-05-16  8:13       ` Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 5/5] plugins/sixaxis: Add a set_leds_sysfs() function Antonio Ospite
2014-05-15 20:25     ` Szymon Janc
2014-05-15 15:28   ` [PATCHv2 BlueZ 0/5] plugin/sixaxis: Set leds using the sysfs leds class Frank Praznik
2014-05-15 16:33     ` Antonio Ospite
2014-05-16 12:37   ` Szymon Janc
2014-05-27 11:25   ` [PATCHv3 " Antonio Ospite
2014-05-27 11:25     ` [PATCHv3 BlueZ 4/5] plugins/sixaxis: Add a get_leds_data() function Antonio Ospite
2014-05-27 11:25     ` [PATCHv3 BlueZ 5/5] plugins/sixaxis: Add a set_leds_sysfs() function Antonio Ospite
2014-06-08 12:58     ` [PATCHv3 BlueZ 0/5] plugin/sixaxis: Set leds using the sysfs leds class Szymon Janc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2381743.AtH49zkCbR@athlon \
    --to=szymon.janc@gmail.com \
    --cc=ao2@ao2.it \
    --cc=frank.praznik@oh.rr.com \
    --cc=hadess@hadess.net \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).