Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mbed sync #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
yotta_targets
yotta_modules
.hg
7 changes: 7 additions & 0 deletions source/examples/proximity-heart/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"microbit-dal":{
"bluetooth":{
"enabled": 0
}
}
}
160 changes: 160 additions & 0 deletions source/examples/proximity-heart/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
The MIT License (MIT)

Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/


/*
* This is a simple example that uses the micro:bit radio to show if another
* micro:bit running the same code is nearby.
*
* Each micro:bit is periodically broadcasting a group name, and listening
* to see if another micro:bit in that group is also in-range.
*
* When another micro:bit in the same group is detected, the display of the
* detecting micro:bit will show a filled-in heart shape
*
* Otherwise, the display will show an empty heart shape
*
*/

#include "MicroBit.h"

#if MICROBIT_BLE_ENABLED
#ifdef YOTTA_CFG
#error "This example needs BLE to be disabled. Use the yotta config.json in the proximit-heart directory to do this"
#else
#error "This example needs BLE to be disabled in the microbit config file in the microbit-dal: MicroBitConfig.h"
#endif
#endif

MicroBit uBit;

// Have we seen a friend recently?
uint8_t friend_seen = 0;

// Are we currently sending out messages?
uint8_t broadcast = 1;

/* We have a group name, and if any micro:bit is in range and in the group
* then the others will see it.
*/
const char group_name[] = "tiger";

const uint8_t empty_heart_arr[] {
0, 1, 0, 1, 0,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
0, 1, 0, 1, 0,
0, 0, 1, 0, 0, };

const uint8_t full_heart_arr[] {
0, 1, 0, 1, 0,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
0, 1, 1, 1, 0,
0, 0, 1, 0, 0, };

const uint8_t small_heart_arr[] {
0, 0, 0, 0, 0,
0, 1, 0, 1, 0,
0, 1, 1, 1, 0,
0, 1, 1, 0, 0,
0, 0, 0, 0, 0, };

MicroBitImage empty_heart(5,5,empty_heart_arr);
MicroBitImage full_heart(5,5,full_heart_arr);
MicroBitImage small_heart(5,5,small_heart_arr);

/* We send messages when people press buttons 'A' and 'B'.
* At the moment, all micro:bits listening for messages
* will see these and can respond to them
* Challenge: make only certain micro:bits respond to these
*/
void onButtonA(MicroBitEvent)
{
uBit.radio.datagram.send("1");
}

void onButtonB(MicroBitEvent)
{
uBit.radio.datagram.send("2");
}

/* We toggle broadcasting if both buttons are pressed together */
void onButtonAB(MicroBitEvent)
{
broadcast = !broadcast;
uBit.display.print("!");
}


void onData(MicroBitEvent)
{
ManagedString s = uBit.radio.datagram.recv();
int rssi = uBit.radio.getRSSI();

if (s == "1")
uBit.display.print(full_heart);

if (s == "2")
uBit.display.print(small_heart);

/* For detecting the presence of our friend, we require them to be sending
* the same group name as we are in
*/
if (s == group_name && rssi < 70) {
// We can make this larger to allow more missed packets
friend_seen = 3;
}
}

int main()
{
// Initialise the micro:bit runtime.
uBit.init();

// Setup some button handlers to allow direct heartbeat control with buttons
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButtonA);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButtonB);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_EVT_ANY, onButtonAB);

//Setup a hander to run when data is received
uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData);

uBit.radio.enable();

while(1) {
if (friend_seen) {
uBit.display.print(full_heart);
friend_seen -= 1;
} else {
uBit.display.print(empty_heart);
}

if (broadcast)
uBit.radio.datagram.send(group_name);

uBit.sleep(1000);
}
}
1 change: 1 addition & 0 deletions source/microbit.lib
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://developer.mbed.org/teams/Lancaster-University/code/microbit/#4b89e7e3494f
49 changes: 49 additions & 0 deletions sync-mbed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
# This script is for syncing the microbit-samples repository on GitHub to
# developer.mbed.org
# usage:
# cd microbit-samples
# (optional) git pull etc
# (optional) - update source/microbit.lib to point to the latest microbit.lib on mbed
# ./sync-mbed.sh

set -u
set -x

MBED_USERNAME="LancasterUniversity"
MBED_URL="developer.mbed.org"
MICROBIT_TEAM="teams/microbit"
EXAMPLES_PATH="source/examples"
EXAMPLE_PREPEND="microbit"

GIT_REVISION=$(git describe --tags)
EXAMPLES=$(ls "$EXAMPLES_PATH")
#Or just do a subset:
#EXAMPLES="hello-world invaders logic-gates proximity-heart simple-animation simple-radio-rx simple-radio-tx snake"

#Feeling uncertain? Uncomment this and you get to 'approve' each line
#trap read debug

for example in $EXAMPLES; do
ENAME="$EXAMPLE_PREPEND-$example"
cd "$EXAMPLES_PATH/$example" || exit
CODE="https://[email protected]/$MICROBIT_TEAM/code/$ENAME"
if [ ! -d .hg ]; then
hg init
fi
FILES="$(git ls-files)";
hg pull $CODE
hg update
#clobber everything with the versions in git
for FILE in $FILES; do
git checkout "$FILE"
done
cp ../../*.lib ./
hg add *
hg commit -u $MBED_USERNAME -m "Update to git: $GIT_REVISION"
PUSH="https://developer.mbed.org/$MICROBIT_TEAM/code/$ENAME"
hg push "$PUSH"
rm *.lib
cd ../../../
done