Griffin Powermate audio controller for mpd (Music Player Daemon)
Clone or download
Pull request Compare This branch is 5 commits ahead of bethebunny:master.
senyoltw mpdへの接続を毎回実行することに
繋ぎっぱなしだとエラーが多発したため
Latest commit 9e7d5cc Dec 12, 2018

README.md

Griffin Powermate audio controller for mpd (Music Player Daemon)

powermate

A small python framework for scripting interactions with the Griffin Powermate.

  • Python 2 and 3
  • Any number of powermates
  • Can have more than one script interact with the same powermate

setup

In order to read and write to the Powermate event files on linux, you will need to do the following (ymmv, but this should work on most modern distros).

$ sudo groupadd input
$ sudo usermod -a -G input "$USER"
$ echo 'KERNEL=="event*", NAME="input/%k", MODE="660", GROUP="input"' | sudo tee -a /etc/udev/rules.d/99-input.rules

After a reboot your scripts should be able to read/write to the device.

使い方 How to USE

pip install python-mpd2
python mpd_powermate.py

サービス化 daemonize

cd powermate
sudo cp powermate.service /etc/systemd/system/powermate.service
sudo systemctl daemon-reload
sudo systemctl start powermate.service

writing your own

from powermate import PowerMateBase, LedEvent, MAX_BRIGHTNESS

class ExamplePowerMate(PowerMateBase):
  def __init__(self, path):
    super(ExamplePowerMate, self).__init__(path)
    self._pulsing = False
    self._brightness = MAX_BRIGHTNESS

  def short_press(self):
    print('Short press!')
    self._pulsing = not self._pulsing
    print(self._pulsing)
    if self._pulsing:
      return LedEvent.pulse()
    else:
      return LedEvent(brightness=self._brightness)

  def long_press(self):
    print('Long press!')

  def rotate(self, rotation):
    print('Rotate {}!'.format(rotation))
    self._brightness = max(0, min(MAX_BRIGHTNESS, self._brightness + rotation))
    self._pulsing = False
    return LedEvent(brightness=self._brightness)

  def push_rotate(self, rotation):
    print('Push rotate {}!'.format(rotation))

if __name__ == '__main__':
  pm = ExamplePowerMate(glob.glob('/dev/input/by-id/*PowerMate*')[0])
  pm.run()

You can implement any subset of these commands. By default a press will take 1 second to be considered a long_press, and a long_press will default to doing a short_press unless overridden. The threshold for a long press may be changed by passing the keyword argument long_threshold to the super constructor.

If you have several powermates that you want to control you will need to modify the globbing expression in the main block appropriately.

If you want to have more than one script or rules sets that interact with a single powermate, they will all need to be run through one central PowerMateBase instance; otherwise events read by one script won't be read by the other. This is convenient to do through the add_listener method. See the example at the bottom of powermate.py for how to do this. This should be robust to badly behaving code, so eg. if one of your handlers is hanging it won't stop the other handlers from working properly.