|
| 1 | +{% |
| 2 | +assign class_name = currentClass.friendlyName | camel_case | capitalize %}{% |
| 3 | +comment %}{% |
| 4 | +if currentClass.systemDeviceNameConvention %}{% |
| 5 | + assign device_name_convention = currentClass.systemDeviceNameConvention | replace: '\{\d\}', '*' %}{% |
| 6 | +else %}{% |
| 7 | + assign device_name_convention = '*' %}{% |
| 8 | +endif %}{% |
| 9 | +if currentClass.inheritance %}{% |
| 10 | + assign base_class = currentClass.inheritance | camel_case | capitalize %}{% |
| 11 | +else %}{% |
| 12 | + assign base_class = 'Device' %}{% |
| 13 | +endif%}{% |
| 14 | +assign driver_name = "" %}{% |
| 15 | +if currentClass.driverName %}{% |
| 16 | + for name in currentClass.driverName %}{% |
| 17 | + capture driver_name %}{{ driver_name }}, '{{name}}'{% endcapture %}{% |
| 18 | + endfor %}{% |
| 19 | + capture driver_name %} driver_name=[{{ driver_name | remove_first:', ' }}],{% endcapture %}{% |
| 20 | +endif %}{% endcomment %} |
| 21 | +import fcntl |
| 22 | +import array |
| 23 | + |
| 24 | +class {{ class_name }}({{ base_class }}): |
| 25 | + |
| 26 | + """{% for line in currentClass.description %} |
| 27 | + {{ line }}{% endfor %} |
| 28 | + """ |
| 29 | + |
| 30 | + KEY_MAX = 0x2FF |
| 31 | + KEY_BUF_LEN = int((KEY_MAX + 7) / 8) |
| 32 | + EVIOCGKEY = (2 << (14 + 8 + 8) | KEY_BUF_LEN << (8 + 8) | ord('E') << 8 | 0x18) |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + self._buf = array.array( 'B', [0] * self.KEY_BUF_LEN ) |
| 36 | + self.filehandle_cache = {} |
| 37 | + |
| 38 | + def _button_file(self, name): |
| 39 | + if name not in self.filehandle_cache: |
| 40 | + f = open( name, 'r' ) |
| 41 | + self.filehandle_cache[name] = f |
| 42 | + else: |
| 43 | + f = self.filehandle_cache[name] |
| 44 | + return f |
| 45 | + |
| 46 | + def read_button(self, name, button): |
| 47 | + ret = fcntl.ioctl(self._button_file('/dev/input/by-path/platform-gpio-keys.0-event'), self.EVIOCGKEY, self._buf) |
| 48 | + if (ret < 0): |
| 49 | + return None |
| 50 | + else: |
| 51 | + return not bool(self._buf[int(button / 8)] & 1 << button % 8) |
| 52 | + |
| 53 | + @property |
| 54 | + def up(self): |
| 55 | + return self.read_button( 'platform-gpio-keys.0-event', 103 ) |
| 56 | + |
| 57 | + @property |
| 58 | + def down(self): |
| 59 | + return self.read_button( 'platform-gpio-keys.0-event', 108 ) |
| 60 | + |
| 61 | + @property |
| 62 | + def left(self): |
| 63 | + return self.read_button( 'platform-gpio-keys.0-event', 105 ) |
| 64 | + |
| 65 | + @property |
| 66 | + def right(self): |
| 67 | + return self.read_button( 'platform-gpio-keys.0-event', 106 ) |
| 68 | + |
| 69 | + @property |
| 70 | + def enter(self): |
| 71 | + return self.read_button( 'platform-gpio-keys.0-event', 28 ) |
| 72 | + |
| 73 | + @property |
| 74 | + def backspace(self): |
| 75 | + return self.read_button( 'platform-gpio-keys.0-event', 14 ) |
| 76 | + |
| 77 | +{% comment %} |
| 78 | +{% if currentClass.inheritance %} |
| 79 | + SYSTEM_CLASS_NAME = {{ base_class }}.SYSTEM_CLASS_NAME |
| 80 | + SYSTEM_DEVICE_NAME_CONVENTION = {{ base_class }}.SYSTEM_DEVICE_NAME_CONVENTION |
| 81 | +{% else %} |
| 82 | + SYSTEM_CLASS_NAME = '{{ currentClass.systemClassName }}' |
| 83 | + SYSTEM_DEVICE_NAME_CONVENTION = '{{ device_name_convention }}' |
| 84 | +{% endif %} |
| 85 | + def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs): |
| 86 | + if port is not None: |
| 87 | + kwargs['port_name'] = port |
| 88 | + Device.__init__(self, self.SYSTEM_CLASS_NAME, name,{{ driver_name }} **kwargs) |
| 89 | +{% endcomment %} |
0 commit comments