The recipes provide examples of how you can use Blue Dot. Don't be restricted by these ideas and be sure to have a look at the :doc:`dotapi` as there is more to be discovered.
The simplest way to use the Blue Dot is as a wireless button.
.. currentmodule:: bluedot
Let's say "Hello World" by creating the :class:`BlueDot` object then waiting for the Blue Dot app to connect and be pressed:
.. literalinclude:: examples/hello_world.py
Alternatively you can also use :attr:`~BlueDot.when_pressed` to call a function:
.. literalinclude:: examples/hello_event.py
.. literalinclude:: examples/goodbye_world.py
.. literalinclude:: examples/shout_hello.py
Flash an LED
Using Blue Dot in combination with :mod:`gpiozero` you can interact with electronic components, such as LEDs, connected to your Raspberry Pi.
When the Blue Dot is pressed, the LED connected to GPIO 27 will turn on; when released it will turn off:
.. literalinclude:: examples/led1.py
.. literalinclude:: examples/led2.py
.. literalinclude:: examples/led3.py
.. literalinclude:: examples/camera.py
The Blue Dot can also be used as a joystick when the middle, top, bottom, left or right areas of the dot are touched.
Using the position the Blue Dot was pressed you can work out whether it was pressed to go up, down, left, right like the D-pad on a joystick:
.. literalinclude:: examples/dpad.py
At the moment the D-pad only registers when it is pressed. To get it work
when the position is moved you should add the following line above
bd.when_moved = dpad
These recipes assume your robot is constructed with a pair of H-bridges. The forward and backward pins for the H-bridge of the left wheel are 17 and 18 respectively, and the forward and backward pins for H-bridge of the right wheel are 22 and 23 respectively.
.. literalinclude:: examples/robot1.py
Variable Speed Robot
You can change the robot to use variable speeds, so the further towards the edge you press the Blue Dot, the faster the robot will go.
The :attr:`~BlueDotPosition.distance` attribute returns how far from the centre the Blue Dot was pressed, which can be passed to the robot's functions to change its speed:
.. literalinclude:: examples/robot2.py
.. literalinclude:: examples/robot3.py
By holding down the Blue Dot and moving the position you can use it as an analogue slider.
Using the :attr:`BlueDotPosition.distance` property which is returned when the position is moved you can create a slider which goes from the centre out in any direction:
.. literalinclude:: examples/slider_centre.py
Left to Right
The :attr:`BlueDotPosition.x` property returns a value from -1 (far left) to 1 (far right). Using this value you can create a slider which goes horizontally through the middle:
.. literalinclude:: examples/slider_left_right.py
To make a vertical slider you could change the code above to use :attr:`BlueDotPosition.y` instead.
.. literalinclude:: examples/slider_dimmer.py
You can interact with the Blue Dot by swiping across it, like you would to move between pages in a mobile app.
Detecting a single swipe is easy using :attr:`~BlueDot.wait_for_swipe`:
.. literalinclude:: examples/swipe1.py
Alternatively you can also use :attr:`~BlueDot.when_swiped` to call a function:
.. literalinclude:: examples/swipe2.py
.. literalinclude:: examples/swipe_direction.py
Speed, Angle, and Distance
:class:`BlueDotSwipe` returns more than just the direction. It also includes the speed of the swipe (in Blue Dot radius per second), the angle, and the distance between the start and end positions of the swipe:
.. literalinclude:: examples/swipe_speed_angle.py
You can use Blue Dot like a rotary encoder or "iPod classic click wheel" - rotating around the outer edge of the Blue Dot will cause it to "tick". The Blue Dot is split into a number of virtual segments (the default is 8), when the position moves from one segment to another, it ticks.
Using the :attr:`~BlueDot.when_rotated` callback you can create a counter which increments / decrements when the Blue Dot is rotated either clockwise or anti-clockwise. A :class:`BlueDotRotation` object is passed to the callback. Its :attr:`~BlueDotRotation.value` property will be -1 if rotated anti-clockwise and 1 if rotated clockwise:
.. literalinclude:: examples/rotation.py
The rotation speed can be modified using the :attr:`BlueDot.rotation_segments` property which changes the number of segments the Blue Dot is split into:
bd.rotation_segments = 16
You can interact with the Bluetooth adapter using :class:`BlueDot`.
You can put your Raspberry Pi into pairing mode which will allow pairing from other devices for 60 seconds:
.. literalinclude:: examples/bt_pairing.py
Or connect up a physical button up to start the pairing (the button is assumed to be wired to GPIO 27):
.. literalinclude:: examples/bt_pair_button.py
You can iterate over the devices that your Raspberry Pi is paired too:
.. literalinclude:: examples/bt_enumerate.py
Blue Dot includes a :class:`MockBlueDot` class to allow you to test and debug your program without having to use Bluetooth or a Blue Dot client.
:class:`MockBlueDot` inherits from :class:`BlueDot` and is used in the same way, but you have the option of launching a mock app which you can click with a mouse or writing scripts to simulate the Blue Dot being used.
Launch the mock Blue Dot app to test by clicking the on-screen dot with the mouse:
.. literalinclude:: examples/mock_app.py
Tests can also be scripted using :class:`MockBlueDot`:
.. literalinclude:: examples/mock_script.py