linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Young <sean@mess.org>
To: linux-media@vger.kernel.org
Cc: "Antti Seppälä" <a.seppala@gmail.com>,
	"James Hogan" <james@albanarts.com>,
	"David Härdeman" <david@hardeman.nu>
Subject: [PATCH v5 07/18] [media] rc: rc-ir-raw: Add Manchester encoder (phase encoder) helper
Date: Mon, 12 Dec 2016 21:13:43 +0000	[thread overview]
Message-ID: <b3f8ca8fe3c86982e2613a1494e1de9f4cdee02f.1481575826.git.sean@mess.org> (raw)
In-Reply-To: <1669f6c54c34e5a78ce114c633c98b331e58e8c7.1481575826.git.sean@mess.org>
In-Reply-To: <cover.1481575826.git.sean@mess.org>

From: Antti Seppälä <a.seppala@gmail.com>

Adding a simple Manchester encoder to rc-core.
Manchester coding is used by at least RC-5 and RC-6 protocols and their
variants.

Signed-off-by: Antti Seppälä <a.seppala@gmail.com>
Signed-off-by: James Hogan <james@albanarts.com>
Signed-off-by: Sean Young <sean@mess.org>
Cc: David Härdeman <david@hardeman.nu>
---
 drivers/media/rc/rc-core-priv.h | 33 ++++++++++++++++
 drivers/media/rc/rc-ir-raw.c    | 85 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+)

diff --git a/drivers/media/rc/rc-core-priv.h b/drivers/media/rc/rc-core-priv.h
index d8be011..74513c6 100644
--- a/drivers/media/rc/rc-core-priv.h
+++ b/drivers/media/rc/rc-core-priv.h
@@ -156,6 +156,39 @@ static inline bool is_timing_event(struct ir_raw_event ev)
 #define TO_US(duration)			DIV_ROUND_CLOSEST((duration), 1000)
 #define TO_STR(is_pulse)		((is_pulse) ? "pulse" : "space")
 
+/* functions for IR encoders */
+
+static inline void init_ir_raw_event_duration(struct ir_raw_event *ev,
+					      unsigned int pulse,
+					      u32 duration)
+{
+	init_ir_raw_event(ev);
+	ev->duration = duration;
+	ev->pulse = pulse;
+}
+
+/**
+ * struct ir_raw_timings_manchester - Manchester coding timings
+ * @leader:		duration of leader pulse (if any) 0 if continuing
+ *			existing signal (see @pulse_space_start)
+ * @pulse_space_start:	1 for starting with pulse (0 for starting with space)
+ * @clock:		duration of each pulse/space in ns
+ * @invert:		if set clock logic is inverted
+ *			(0 = space + pulse, 1 = pulse + space)
+ * @trailer_space:	duration of trailer space in ns
+ */
+struct ir_raw_timings_manchester {
+	unsigned int leader;
+	unsigned int pulse_space_start:1;
+	unsigned int clock;
+	unsigned int invert:1;
+	unsigned int trailer_space;
+};
+
+int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
+			  const struct ir_raw_timings_manchester *timings,
+			  unsigned int n, unsigned int data);
+
 /*
  * Routines from rc-raw.c to be used internally and by decoders
  */
diff --git a/drivers/media/rc/rc-ir-raw.c b/drivers/media/rc/rc-ir-raw.c
index c23c877..4b2d82a 100644
--- a/drivers/media/rc/rc-ir-raw.c
+++ b/drivers/media/rc/rc-ir-raw.c
@@ -250,6 +250,91 @@ static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols)
 }
 
 /**
+ * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
+ * @ev:		Pointer to pointer to next free event. *@ev is incremented for
+ *		each raw event filled.
+ * @max:	Maximum number of raw events to fill.
+ * @timings:	Manchester modulation timings.
+ * @n:		Number of bits of data.
+ * @data:	Data bits to encode.
+ *
+ * Encodes the @n least significant bits of @data using Manchester (bi-phase)
+ * modulation with the timing characteristics described by @timings, writing up
+ * to @max raw IR events using the *@ev pointer.
+ *
+ * Returns:	0 on success.
+ *		-ENOBUFS if there isn't enough space in the array to fit the
+ *		full encoded data. In this case all @max events will have been
+ *		written.
+ */
+int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
+			  const struct ir_raw_timings_manchester *timings,
+			  unsigned int n, unsigned int data)
+{
+	bool need_pulse;
+	unsigned int i;
+	int ret = -ENOBUFS;
+
+	i = 1 << (n - 1);
+
+	if (timings->leader) {
+		if (!max--)
+			return ret;
+		if (timings->pulse_space_start) {
+			init_ir_raw_event_duration((*ev)++, 1, timings->leader);
+
+			if (!max--)
+				return ret;
+			init_ir_raw_event_duration((*ev), 0, timings->leader);
+		} else {
+			init_ir_raw_event_duration((*ev), 1, timings->leader);
+		}
+		i >>= 1;
+	} else {
+		/* continue existing signal */
+		--(*ev);
+	}
+	/* from here on *ev will point to the last event rather than the next */
+
+	while (n && i > 0) {
+		need_pulse = !(data & i);
+		if (timings->invert)
+			need_pulse = !need_pulse;
+		if (need_pulse == !!(*ev)->pulse) {
+			(*ev)->duration += timings->clock;
+		} else {
+			if (!max--)
+				goto nobufs;
+			init_ir_raw_event_duration(++(*ev), need_pulse,
+						   timings->clock);
+		}
+
+		if (!max--)
+			goto nobufs;
+		init_ir_raw_event_duration(++(*ev), !need_pulse,
+					   timings->clock);
+		i >>= 1;
+	}
+
+	if (timings->trailer_space) {
+		if (!(*ev)->pulse)
+			(*ev)->duration += timings->trailer_space;
+		else if (!max--)
+			goto nobufs;
+		else
+			init_ir_raw_event_duration(++(*ev), 0,
+						   timings->trailer_space);
+	}
+
+	ret = 0;
+nobufs:
+	/* point to the next event rather than last event before returning */
+	++(*ev);
+	return ret;
+}
+EXPORT_SYMBOL(ir_raw_gen_manchester);
+
+/**
  * ir_raw_encode_scancode() - Encode a scancode as raw events
  *
  * @protocol:		protocol
-- 
2.9.3


  parent reply	other threads:[~2016-12-12 21:13 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-12 21:13 [PATCH v5 00/18] Use sysfs filter for winbond & nuvoton wakeup Sean Young
2016-12-12 21:13 ` [PATCH v5 06/18] [media] rc: rc-ir-raw: Add scancode encoder callback Sean Young
2016-12-12 21:13 ` [PATCH v5 01/18] [media] rc: change wakeup_protocols to list all protocol variants Sean Young
2016-12-12 21:13 ` [PATCH v5 02/18] [media] img-ir: use new wakeup_protocols sysfs mechanism Sean Young
2016-12-12 22:31   ` James Hogan
2016-12-13  7:54     ` Sean Young
2016-12-13 11:31       ` James Hogan
2016-12-12 21:13 ` Sean Young [this message]
2016-12-12 21:13 ` [PATCH v5 03/18] [media] rc: Add scancode validation Sean Young
2016-12-12 21:13 ` [PATCH v5 08/18] [media] rc: rc-ir-raw: Add pulse-distance modulation helper Sean Young
2016-12-12 21:13 ` [PATCH v5 04/18] [media] winbond-cir: use sysfs wakeup filter Sean Young
2016-12-12 21:13 ` [PATCH v5 09/18] [media] rc: ir-rc5-decoder: Add encode capability Sean Young
2016-12-12 21:13 ` [PATCH v5 10/18] [media] rc: ir-rc6-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 11/18] [media] rc: ir-nec-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 12/18] [media] rc: ir-jvc-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 13/18] [media] rc: ir-sanyo-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 14/18] [media] rc: ir-sharp-decoder: " Sean Young
2016-12-12 21:13 ` [PATCH v5 05/18] [media] rc: raw IR drivers cannot handle cec, unknown or other Sean Young
2016-12-12 21:13 ` [PATCH v5 15/18] [media] rc: ir-sony-decoder: Add encode capability Sean Young
2016-12-12 21:13 ` [PATCH v5 16/18] [media] rc: rc-core: Add support for encode_wakeup drivers Sean Young
2016-12-12 21:13 ` [PATCH v5 17/18] [media] rc: rc-loopback: Add loopback of filter scancodes Sean Young
2016-12-12 21:13 ` [PATCH v5 18/18] [media] rc: nuvoton-cir: Add support wakeup via sysfs filter callback Sean Young

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=b3f8ca8fe3c86982e2613a1494e1de9f4cdee02f.1481575826.git.sean@mess.org \
    --to=sean@mess.org \
    --cc=a.seppala@gmail.com \
    --cc=david@hardeman.nu \
    --cc=james@albanarts.com \
    --cc=linux-media@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).