clicks.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2024 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from .. import PebbleCommander, exceptions, parsers
  15. @PebbleCommander.command()
  16. def click_short(cmdr, button):
  17. """ Click a button.
  18. """
  19. button = int(str(button), 0)
  20. if not 0 <= button <= 3:
  21. raise exceptions.ParameterError('button out of range: %d' % button)
  22. ret = cmdr.send_prompt_command("click short %d" % button)
  23. if not ret[0].startswith("OK"):
  24. raise exceptions.PromptResponseError(ret)
  25. @PebbleCommander.command()
  26. def click_long(cmdr, button, hold_ms=20):
  27. """ Hold a button.
  28. `hold_ms` is how many ms to hold the button down before releasing.
  29. """
  30. return cmdr.click_multiple(button, hold_ms=hold_ms)
  31. @PebbleCommander.command()
  32. def click_multiple(cmdr, button, count=1, hold_ms=20, delay_ms=0):
  33. """ Rhythmically click a button.
  34. """
  35. button = int(str(button), 0)
  36. count = int(str(count), 0)
  37. hold_ms = int(str(hold_ms), 0)
  38. delay_ms = int(str(delay_ms), 0)
  39. if not 0 <= button <= 3:
  40. raise exceptions.ParameterError('button out of range: %d' % button)
  41. if not count > 0:
  42. raise exceptions.ParameterError('count out of range: %d' % count)
  43. if hold_ms < 0:
  44. raise exceptions.ParameterError('hold_ms out of range: %d' % hold_ms)
  45. if delay_ms < 0:
  46. raise exceptions.ParameterError('delay_ms out of range: %d' % delay_ms)
  47. ret = cmdr.send_prompt_command(
  48. "click multiple {button:d} {count:d} {hold_ms:d} {delay_ms:d}".format(**locals()))
  49. if not ret[0].startswith("OK"):
  50. raise exceptions.PromptResponseError(ret)