pyb.Switch.rst 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. .. currentmodule:: pyb
  2. .. _pyb.Switch:
  3. class Switch -- switch object
  4. =============================
  5. A Switch object is used to control a push-button switch.
  6. Usage::
  7. sw = pyb.Switch() # create a switch object
  8. sw.value() # get state (True if pressed, False otherwise)
  9. sw() # shorthand notation to get the switch state
  10. sw.callback(f) # register a callback to be called when the
  11. # switch is pressed down
  12. sw.callback(None) # remove the callback
  13. Example::
  14. pyb.Switch().callback(lambda: pyb.LED(1).toggle())
  15. Constructors
  16. ------------
  17. .. class:: pyb.Switch()
  18. Create and return a switch object.
  19. Methods
  20. -------
  21. .. method:: Switch.__call__()
  22. Call switch object directly to get its state: ``True`` if pressed down,
  23. ``False`` otherwise.
  24. .. method:: Switch.value()
  25. Get the switch state. Returns ``True`` if pressed down, otherwise ``False``.
  26. .. method:: Switch.callback(fun)
  27. Register the given function to be called when the switch is pressed down.
  28. If ``fun`` is ``None``, then it disables the callback.