쿠루쿠루님의 블로그를 통해서 micro:bit에서 Arduino IDE를 이용해서 개발이 가능한것을 알게 되었습니다. 보드추가 방법은 쿠루쿠루님의 블로그가 잘 나와있으니 참고 하시고, Arduino IDE를 이용해서 LED를 끄고 켜는 예제를 만들어 보았습니다.
보드 추가
메뉴 [파일] - [환경설정]을 선택하시면 아래와 같이 [환경설정]창을 나옵니다. 이 [환경설정]창에서 [추가적인 보드 메니저 URLs]에 https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json 값을 설정합니다.
int led = 16; // P0_16
void setup() {
pinMode(led, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
#include "MicroBit.h"
#include "MicroBitUARTService.h"
MicroBit uBit;
MicroBitUARTService *uart;
// we use events abd the 'connected' variable to keep track of the status of the Bluetooth connection
void onConnected(MicroBitEvent)
{
uBit.display.print("C");
// 종료 문자열
ManagedString eom(":");
while(1) {
//ManagedString msg = uart->read(1);
ManagedString msg = uart->readUntil(eom);
uBit.display.scroll(msg);
}
}
void onDisconnected(MicroBitEvent)
{
uBit.display.print("D");
}
int main()
{
// Initialise the micro:bit runtime.
uBit.init();
// Services/Pairing Config/Power Level
uBit.display.scroll("BLE ABDILMT/P/0");
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected);
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected);
//new MicroBitAccelerometerService(*uBit.ble, uBit.accelerometer);
new MicroBitButtonService(*uBit.ble);
//new MicroBitIOPinService(*uBit.ble, uBit.io);
//new MicroBitLEDService(*uBit.ble, uBit.display);
//new MicroBitMagnetometerService(*uBit.ble, uBit.compass);
//new MicroBitTemperatureService(*uBit.ble, uBit.thermometer);
// Note GATT table size increased from default in MicroBitConfig.h
// #define MICROBIT_SD_GATT_TABLE_SIZE 0x500
uart = new MicroBitUARTService(*uBit.ble, 32, 32);
uBit.display.scroll("UART ready");
// If main exits, there may still be other fibers running or registered event handlers etc.
// Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
// sit in the idle task forever, in a power efficient sleep.
release_fiber();
}
Delphi 화면 디자인
Delphi 소스 - Main.pas
unit Main;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Objects, FMX.Edit, FMX.Controls.Presentation, System.Bluetooth,
System.Bluetooth.Components, FMX.Layouts, FMX.ListBox;
const
UUID_BUTTON_SERVICE : TBluetoothUUID = '{E95D9882-251D-470A-A062-FA1922DFA9A8}';
UUID_BUTTON1_CHARACTERRISTIC : TBluetoothUUID = '{E95DDA90-251D-470A-A062-FA1922DFA9A8}';
UUID_BUTTON2_CHARACTERRISTIC : TBluetoothUUID = '{E95DDA91-251D-470A-A062-FA1922DFA9A8}';
UUID_UART_SERVICE : TBluetoothUUID = '{6E400001-B5A3-F393-E0A9-E50E24DCCA9E}';
UUID_UART_TX_CHARACTERRISTIC : TBluetoothUUID = '{6E400003-B5A3-F393-E0A9-E50E24DCCA9E}';
type
TForm42 = class(TForm)
Connect: TButton;
Edit1: TEdit;
Send: TButton;
ButtonA: TCircle;
Label1: TLabel;
BTLableA: TLabel;
ButtonB: TCircle;
BtLabelB: TLabel;
BluetoothLE1: TBluetoothLE;
ListBox1: TListBox;
StyleBook1: TStyleBook;
procedure BluetoothLE1CharacteristicRead(const Sender: TObject;
const ACharacteristic: TBluetoothGattCharacteristic;
AGattStatus: TBluetoothGattStatus);
procedure BluetoothLE1EndDiscoverDevices(const Sender: TObject;
const ADeviceList: TBluetoothLEDeviceList);
procedure BluetoothLE1EndDiscoverServices(const Sender: TObject;
const AServiceList: TBluetoothGattServiceList);
procedure ConnectClick(Sender: TObject);
procedure SendClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1ItemClick(const Sender: TCustomListBox;
const Item: TListBoxItem);
private
{ private 먬뙻 }
public
{ public 먬뙻 }
end;
var
Form42: TForm42;
GBleDevice : TBluetoothLEDevice;
BackColor : TColor;
implementation
{$R *.fmx}
{$R *.iPhone55in.fmx IOS}
procedure TForm42.BluetoothLE1CharacteristicRead(const Sender: TObject;
const ACharacteristic: TBluetoothGattCharacteristic;
AGattStatus: TBluetoothGattStatus);
begin
if(ACharacteristic.UUID = UUID_BUTTON1_CHARACTERRISTIC) then
begin
if ACharacteristic.GetValueAsInt8(0) <> 0 then
ButtonA.Fill.Color := TAlphaColorRec.Red
else
ButtonA.Fill.Color := BackColor;
end
else
if(ACharacteristic.UUID = UUID_BUTTON2_CHARACTERRISTIC) then
begin
if ACharacteristic.GetValueAsInt8(0) <> 0 then
ButtonB.Fill.Color := TAlphaColorRec.Red
else
ButtonB.Fill.Color := BackColor;
end;
end;
procedure TForm42.BluetoothLE1EndDiscoverDevices(const Sender: TObject;
const ADeviceList: TBluetoothLEDeviceList);
var
i : uint32; // ARM 궶궻궳띍밙돸
device : TBluetoothLEDevice;
begin
ListBox1.Clear;
for i := 0 to ADeviceList.Count-1 do begin
device := ADeviceList.Items[i];
ListBox1.Items.AddObject(device.DeviceName +'/' + device.Address, device);
end;
{
if ADeviceList.Count > 0 then begin
for i := 0 to Pred(ADeviceList.Count) do begin
if Pos('BBC micro:bit', ADeviceList[i].DeviceName) > 0 then begin
Label1.Text := ADeviceList[i].DeviceName ;// ' (' + ADeviceList[i].Address + ')';
BluetoothLE1.CancelDiscovery;
GBleDevice := ADeviceList[i];
GBleDevice.DiscoverServices;
exit;
end;
end;
end;
}
end;
procedure TForm42.BluetoothLE1EndDiscoverServices(const Sender: TObject;
const AServiceList: TBluetoothGattServiceList);
var
i : uint32; // ARM 궶궻궳띍밙돸
LGattService : TBluetoothGattService;
begin
LGattService := BluetoothLE1.GetService(GBleDevice, UUID_BUTTON_SERVICE);
if LGattService <> nil then
begin
for i := 0 to Pred(LGattService.Characteristics.Count) do
begin
if LGattService.Characteristics[i].UUID = UUID_BUTTON1_CHARACTERRISTIC then
begin
GBleDevice.SetCharacteristicNotification(LGattService.Characteristics[i], True);
end
else
if LGattService.Characteristics[i].UUID = UUID_BUTTON2_CHARACTERRISTIC then
begin
GBleDevice.SetCharacteristicNotification(LGattService.Characteristics[i], True);
end;
end;
end;
end;
procedure TForm42.ConnectClick(Sender: TObject);
begin
if( not BluetoothLE1.Enabled ) then
begin
BluetoothLE1.Enabled := True;
BluetoothLE1.DiscoverDevices(1000); // 10뷳
Connect.Text := 'Disconnect';
Label1.Text := '';
end
else
begin
ListBox1.Clear;
BluetoothLE1.CancelDiscovery;
BluetoothLE1.Enabled := False;
GBleDevice := NIL;
Connect.Text := 'Connect';
end;
end;
procedure TForm42.FormCreate(Sender: TObject);
begin
BackColor := ButtonA.Fill.Color;
end;
procedure TForm42.ListBox1ItemClick(const Sender: TCustomListBox; const Item: TListBoxItem);
var
BLEDevice : TBluetoothLEDevice;
begin
BLEDevice := Item.Data as TBluetoothLEDevice;
Label1.Text := BLEDevice.DeviceName ;// ' (' + ADeviceList[i].Address + ')';
BluetoothLE1.CancelDiscovery;
GBleDevice := BLEDevice;
GBleDevice.DiscoverServices;
end;
procedure TForm42.SendClick(Sender: TObject);
var
i : uint32; // ARM 궶궻궳띍밙돸
LGattService : TBluetoothGattService;
LCharact : TBluetoothGattCharacteristic;
begin
if GBleDevice = NIL then exit;
if GBleDevice.IsConnected = false then exit;
LGattService := BluetoothLE1.GetService(GBleDevice, UUID_UART_SERVICE);
if LGattService = NIL then exit;
// LCharact := LGattService.GetCharacteristic(UUID_UART_TX_CHARACTERRISTIC);
for i := 0 to Pred(LGattService.Characteristics.Count) do
begin
if LGattService.Characteristics[i].UUID = UUID_UART_TX_CHARACTERRISTIC then
begin
LGattService.Characteristics[i].SetValueAsString(Edit1.Text+':', True);
GBleDevice.WriteCharacteristic(LGattService.Characteristics[i]);
exit;
end;
end;
end;
end.
micro:bit는 블루투스 4.0인 BLE를 지원하지만, 앱인벤터는 BT통신(블루투스 2.0)만 지원하고 있어서, micro:bit에 HC-06을 사용해서 앱인벤터와 BT 통신하는 예제를 구현해보았습니다. 예제는 앱인벤터에서 r 문자를 보내면 micro:bit에서 r를 LED에 출력하고 Hello World를 전달하면 다시 앱인벤터에서는 전달받은 문자열을 TextView에 보여주고, 다시 q 를 micro:bit에 전달하면 micro:bit는 while을 빠져 나와서 bye를 LED에 출력하고 프로그램을 종료합니다.
micro:bit는 기본적으로 microsoft block, microsot touch, javascript, python을 지원지만, c++도 지원하는데, 온라인 상태의 편집기가 아닌 오프라인에서 컴파일해서 hex 파일을 만들 수 있습니다. 온라인 상태에서 가능한 mbed은 다음에 알아보도록 하고 오프라인에서 hex 파일을 생성하는 방법을 알아보겠습니다.
yotta 설치
파이썬 OpenSSL 패키지 설치
pip install pyopenssl
srecord 설치
srecord 다운로드(https://sourceforge.net/projects/srecord/files/latest/download?source=files) 받아서 C:\yotta 폴더에 압축풀기
전면의 LED를 입력모드로 변경하면, 전면 LED가 광 센서로 작동하여 주변 광을 감지 할 수 있음
온도센서 - Temperature Sensor
이 센서를 통해 micro:bit는 주변의 온도를 감지
가속도 센서 - Accelerometer
가속도 센서는 흔들림, 기울기 및 자유 낙하 등이 있음(향후 드론도 만들 수 있을 것으로 예상)
나침반 - Compass
나침반 기능이 있어서 동서남북 측정
Radio
WIFI나 블루투스보다 송수신 거리가 길어서 드론에서 많이 사용하는 통신 수단으로 라디오 송수신 기능 제공
Bluetooth
BLE (Bluetooth Low Energy) 통신 신호를 송수신하거나 프로그램 업로드 가능
USB Interface
PC와 연결해서 전원 공급과 동시에 프로그램 업로드가능한 마이크로 5핀 커넥터 제공
핀배열
Hardware pin-name
Software pin-name
Capability
P0
P0_3
Analog, digital and touch
P1
P0_2
Analog, digital and touch
P2
P0_1
Analog, digital and touch
P3
P0_4, COL1
Analog and digital
P4
P0_5, COL2
Analog and digital
P5
P0_17, buttonA
Digital only
P6
P0_12, COL9
Digital only
P7
P0_11, COL8
Digital only
P8
P0_18
Digital only
P9
P0_10, COL7
Digital only
P10
P0_6, COL3
Analog and digital
P11
P0_26, buttonB
Digital only
P12
P0_20
Digital only
P13
P0_23
Digital, SPI
P14
P0_22
Digital, SPI
P15
P0_21
Digital, SPI
P16
P0_16
Digital only
P19
P0_0
Digital, I2C
P20
P0_30
Digital, I2C
개인적인 평가
micro:bit는 아두이노와 다르게 기본적으로 제공되는 센서가 많아서 micro:bit 하나만으로도 충분히 많은 아이디어를 구현 할 수 있는 기기로 프로그램 개발 언어도 초보자가 개발가능한 Microsoft Block이나 Microsoft Touch를 지원하고 있고, 조금 더 전문적인 언어로 JavaScript, python도 지원합니다. 아직까지 지원 모듈이 없지만 드론제작울 염두 해두고 만든것 처럼 가속도 센서나, 드론에서 많이 사용하는 배터리도 호환되고, 통신방식으로 라이오 송수신까지 지원합니다.
프로그램 업로드는 USB이용해서 PC에서 가능하고, 블루투스를 이용해서 모바일 폰에서도 업로드가 가능합니다.
$ cat /etc/armbian-release
## PLEASE DO NOT EDIT THIS FILE
BOARD=orangepizeroplus2-h5
BOARD_NAME="Orange Pi Zero Plus 2"
VERSION=5.34.171121
LINUXFAMILY=sunxi64
BRANCH=next
ARCH=arm64
IMAGE_TYPE=nightly
BOARD_TYPE=conf
INITRD_ARCH=arm64
KERNEL_IMAGE_TYPE=Image
Ubuntu 16.04.3 LTS orangepizeroplus2 ttyS0
orangepizeroplus2 login: root
Password: [1234]
You are required to change your password immediately (root enforced)
Changing password for root.
(current) UNIX password: [1234]
Enter new UNIX password: [orangepi]
Retype new UNIX password: [orangepi]
___ ____ _ _____ ____ _ ____
/ _ \| _ \(_) |__ /___ _ __ ___ | _ \| |_ _ ___ |___ \
| | | | |_) | | / // _ \ '__/ _ \ | |_) | | | | / __| __) |
| |_| | __/| | / /| __/ | | (_) | | __/| | |_| \__ \ / __/
\___/|_| |_| /____\___|_| \___/ |_| |_|\__,_|___/ |_____|
Welcome to ARMBIAN 5.34.171121 nightly Ubuntu 16.04.3 LTS 4.13.14-sunxi64
System load: 0.42 0.12 0.04 Up time: 0 min
Memory usage: 8 % of 482MB IP:
CPU temp: 27°C
Usage of /: 4% of 29G
[ General system configuration (beta): armbian-config ]
New to Armbian? Check the documentation first: https://docs.armbian.com
You are using an Armbian nightly build meant only for developers to provide
constructive feedback to improve build system, OS settings or user experience.
If this does not apply to you, STOP NOW!. Especially don't use this image for
daily work since things might not work as expected or at all and may break
anytime with next update. YOU HAVE BEEN WARNED!
This image is provided AS IS with NO WARRANTY and NO END USER SUPPORT!.
Creating a new user account. Press <Ctrl-C> to abort
Please provide a username (eg. your forename): pi
Trying to add user pi
Adding user `pi' ...
Adding new group `pi' (1000) ...
Adding new user `pi' (1000) with group `pi' ...
Creating home directory `/home/pi' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: [orangepi]
Retype new UNIX password: [orangepi]
passwd: password updated successfully
Changing the user information for pi
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
Dear pi, your account pi has been created and is sudo enabled.
Please use this account for your daily work from now on.
root@orangepizeroplus2:~#
pi@orangepizeroplus2:~$ sudo fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1,2, default 2): [Enter]
Partition 2 has been deleted.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): [Enter]
Using default response p.
Partition number (1-4, default 1): [Enter]
First sector (2048-62333951, default 2048): [Enter]
Last sector, +sectors or +size{K,M,G,T,P} (2048-62333951, default 62333951): [Enter]
Created a new partition 1 of type 'Linux' and of size 29.7 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
pi@orangepizeroplus2:~$