qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 01/14] GPIO rotary encoder implementation
@ 2009-07-15 14:52 Filip Navara
  2009-07-15 15:33 ` Blue Swirl
  0 siblings, 1 reply; 6+ messages in thread
From: Filip Navara @ 2009-07-15 14:52 UTC (permalink / raw)
  To: qemu-devel

This patch introduces an emulation of the rotary encoder that could be connected
to GPIO controller.

An internal state is maintained with the values of the output pins. When a key is
pressed the state is modified accordingly and the information is signaled on the
output pins.

Key mappings are specified by the "key-left" and "key-left-alt" properties for a
counter-clockwise direction and "key-right" and "key-right-alt" for a clockwise
direction.

Signed-off-by: Filip Navara <filip.navara@gmail.com>
---
 Makefile.target  |    1 +
 hw/gpio_rotary.c |  136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 0 deletions(-)
 create mode 100644 hw/gpio_rotary.c

diff --git a/Makefile.target b/Makefile.target
index 1a71f3a..0e66139 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -666,6 +666,7 @@ obj-y += framebuffer.o
 obj-y += syborg.o syborg_fb.o syborg_interrupt.o syborg_keyboard.o
 obj-y += syborg_serial.o syborg_timer.o syborg_pointer.o syborg_rtc.o
 obj-y += syborg_virtio.o
+obj-y += gpio_rotary.o
 CPPFLAGS += -DHAS_AUDIO
 endif
 ifeq ($(TARGET_BASE_ARCH), sh4)
diff --git a/hw/gpio_rotary.c b/hw/gpio_rotary.c
new file mode 100644
index 0000000..c35810c
--- /dev/null
+++ b/hw/gpio_rotary.c
@@ -0,0 +1,136 @@
+/*
+ * GPIO Rotary Coder
+ *
+ * Copyright (c) 2009 Filip Navara
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "sysbus.h"
+#include "console.h"
+
+typedef struct RotaryCoderState {
+    SysBusDevice busdev;
+    qemu_irq out[2];
+    uint8_t state;
+    uint8_t key_left;
+    uint8_t key_right;
+    uint8_t key_left_alt;
+    uint8_t key_right_alt;
+    uint8_t extension;
+} RotaryCoderState;
+
+static void rotary_update(RotaryCoderState *s, int direction)
+{
+    s->state += direction;
+    s->state %= 4;    
+
+    qemu_set_irq(s->out[0], s->state == 1 || s->state == 2);
+    qemu_set_irq(s->out[1], s->state == 2 || s->state == 3);
+}
+
+static void rotary_keyboard_event(void *opaque, int keycode)
+{
+    RotaryCoderState *s = opaque;
+
+    if (keycode == 0xe0 && !s->extension) {
+        s->extension = 0x80;
+        return;
+    }
+
+    if (!(keycode & 0x80)) {   
+        keycode &= 0x7f;
+        keycode |= s->extension;
+
+        if (keycode == s->key_left || keycode == s->key_left_alt) {
+            rotary_update(s, 3);
+        } else if (keycode == s->key_right || keycode == s->key_right_alt) {
+            rotary_update(s, 1);
+        }
+    }
+
+    s->extension = 0;
+}
+
+static void rotary_save(QEMUFile *f, void *opaque)
+{
+    RotaryCoderState *s = opaque;
+
+    qemu_put_byte(f, s->state);
+    qemu_put_byte(f, s->extension);
+}
+
+static int rotary_load(QEMUFile *f, void *opaque, int version_id)
+{
+    RotaryCoderState *s = opaque;
+
+    if (version_id != 1)
+        return -EINVAL;
+
+    s->state = qemu_get_byte(f);
+    s->extension = qemu_get_byte(f);
+
+    return 0;
+}
+
+/*
+static void rotary_late_init(DeviceState *dev)
+{
+    RotaryCoderState *s = FROM_SYSBUS(RotaryCoderState, sysbus_from_qdev(dev));
+
+    rotary_update(s);
+}
+*/
+
+static void rotary_init(SysBusDevice *dev)
+{
+    RotaryCoderState *s = FROM_SYSBUS(RotaryCoderState, dev);
+
+    s->key_left = qdev_get_prop_int(&dev->qdev, "key-left", 0xcb);
+    s->key_right = qdev_get_prop_int(&dev->qdev, "key-right", 0xcd);
+    s->key_left_alt = qdev_get_prop_int(&dev->qdev, "key-left-alt", 0x4b);
+    s->key_right_alt = qdev_get_prop_int(&dev->qdev, "key-right-alt", 0x4d);
+
+    qdev_init_gpio_out(&dev->qdev, s->out, 2);
+
+    qemu_add_kbd_event_handler(rotary_keyboard_event, s);
+
+    register_savevm("gpio_rotary", -1, 1, rotary_save, rotary_load, s);
+}
+
+static SysBusDeviceInfo rotary_info = {
+    .init = rotary_init,
+    /* .qdev.late_init = rotary_late_init, */
+    .qdev.name  = "gpio,rotary",
+    .qdev.size  = sizeof(RotaryCoderState),
+    .qdev.props = (DevicePropList[]) {
+        {.name = "key-left", .type = PROP_TYPE_INT},
+        {.name = "key-right", .type = PROP_TYPE_INT},
+        {.name = "key-left-alt", .type = PROP_TYPE_INT},
+        {.name = "key-right-alt", .type = PROP_TYPE_INT},
+        {.name = NULL}
+    }
+};
+
+static void rotary_register(void)
+{
+    sysbus_register_withprop(&rotary_info);
+}
+
+device_init(rotary_register)
-- 
1.6.3.msysgit.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-07-18 15:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-15 14:52 [Qemu-devel] [PATCH 01/14] GPIO rotary encoder implementation Filip Navara
2009-07-15 15:33 ` Blue Swirl
2009-07-15 15:38   ` Filip Navara
2009-07-18  8:52     ` Filip Navara
2009-07-18  9:00       ` Blue Swirl
2009-07-18 15:26         ` Filip Navara

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).