-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrf_ble_write_to_led.py
133 lines (109 loc) · 4.06 KB
/
nrf_ble_write_to_led.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
##### ######
# Import basic libtaties #
##### ######
import sys
import board
import analogio
from time import sleep
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull
# Library for BLE Radio Advertising
#######################################
import _bleio
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
# Libray For grenerating a new Service
########################################
from adafruit_ble.services import Service
from adafruit_ble.uuid import StandardUUID
from adafruit_ble.characteristics import Characteristic
from adafruit_ble.characteristics.int import Uint8Characteristic
from adafruit_ble.characteristics.int import Uint16Characteristic
from adafruit_ble.characteristics.int import Uint32Characteristic
button_state = 0
class myDataService(Service):
uuid = StandardUUID(0xA000) # (0xA000 - my/Custom Service ID)
myOutData = Uint8Characteristic(
max_value=255,
properties=Characteristic.READ | Characteristic.NOTIFY ,
uuid=StandardUUID(0xA001), #0ls -alcat xA002 -my/ID Write-to-Leds ID
# value=bytes([button_state])
)
myInData = Uint8Characteristic(
max_value=255,
properties=Characteristic.WRITE ,
uuid=StandardUUID(0xA002), #0ls -alcat xA002 -my/ID Write-to-Leds ID
)
#CircuitPython Bug (removes old 0xA000 - service)
_bleio.adapter.enabled=0
_bleio.adapter.enabled=1
# oprett BLE - Bateri service med batteli level
dataService=myDataService()
#Start BLE-Radio
ble = BLERadio()
ble.name = "RunesBLE"
#Fortell verden hvem du er og hva du kan gjøre
advertisement = ProvideServicesAdvertisement(myDataService)
led1 = DigitalInOut(board.LED1) # led Hvit
led1.direction = Direction.OUTPUT
led1.value=True
led2_r = DigitalInOut(board.LED2_R) # led Rød
led2_r.direction = Direction.OUTPUT
led2_r.value=True
led2_g = DigitalInOut(board.LED2_G) # led Grønn
led2_g.direction = Direction.OUTPUT
led2_g.value=True
led2_b = DigitalInOut(board.LED2_B) # led Blå
led2_b.direction = Direction.OUTPUT
led2_b.value=True
switch = DigitalInOut(board.SW1)
switch.direction.INPUT
while True:
print("Venter på tilkobling/bonding ")
ble.start_advertising(advertisement)
while not ble.connected:
pass
print("Bluetooth enhet er tilkoblet/bondet ")
#Ikke vis verden hvem du er :)
ble.stop_advertising()
batNivaa=80
oldLedVal=0
buttonVal=False
while ble.connected:
if(buttonVal != switch.value):
buttonVal = switch.value
dataService.myOutData=buttonVal
newLedValue=dataService.myInData # Leser verdien som kommer inn fra bruker
if(oldLedVal != newLedValue) :
if(newLedValue == 1): #Slår på LED 1
print("New Led data " + str(newLedValue))
led1.value=False
oldLedVal=newLedValue
elif(newLedValue == 2):#Slår på LED 2, med farge rød
print("New Led data " + str(newLedValue))
led1.value=True
led2_r.value=False
led2_g.value=True
led2_b.value=True
oldLedVal=newLedValue
elif(newLedValue == 3): #Slår på LED 2, med farge grønn
print("New Led data " + str(newLedValue))
led1.value=True
led2_r.value=True
led2_g.value=False
led2_b.value=True
oldLedVal=newLedValue
elif(newLedValue == 4): #Slår på LED 2, med farge Blå
print("New Led data " + str(newLedValue))
led1.value=True
led2_r.value=True
led2_g.value=True
led2_b.value=False
oldLedVal=newLedValue
elif(newLedValue == 0): #Slår av alle LED's
led1.value=True
led2_r.value=True
led2_b.value=True
led2_g.value=True
sleep(.001)
print("Bluetooth enhet er frakoblet")