Skip to content

Commit

Permalink
Send big numbers
Browse files Browse the repository at this point in the history
This is working but putting checking in to see if anyone can spot what
I’m doing wrong.
  • Loading branch information
ukBaz committed May 31, 2016
1 parent 98e6b21 commit 7a242d9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
9 changes: 5 additions & 4 deletions bluezero/peripheral.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def GetAll(self, interface):

@dbus.service.method(constants.GATT_CHRC_IFACE,
in_signature='a{sv}',
out_signature='ay')
out_signature='aya{sy}')
def ReadValue(self, options):
"""Return the characteristic value.
Expand All @@ -531,9 +531,10 @@ def ReadValue(self, options):
# print('Reading Characteristic', self.value)
if self.value is None:
self.value = 0
return [dbus.Byte(self.value)]
return dbus.ByteArray([(self.value).to_bytes((self.value.bit_length() // 8) + 1, byteorder='little')])
# return self.value

@dbus.service.method(constants.GATT_CHRC_IFACE, in_signature='aya{sv}')
@dbus.service.method(constants.GATT_CHRC_IFACE, in_signature='aya{sy}')
def WriteValue(self, value, options):
"""Set the characteristic value.
Expand Down Expand Up @@ -630,7 +631,7 @@ def send_notify_event(self, value):
# print('Update prop')
self.PropertiesChanged(
constants.GATT_CHRC_IFACE,
{'Value': [dbus.Byte(self.value)]}, [])
{'Value': [dbus.Int16(self.value)]}, [])

####################
# Descriptor Classes
Expand Down
70 changes: 70 additions & 0 deletions examples/sensehat_pressure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import sys
import os

from sense_hat import SenseHat

sys.path.insert(0,
os.path.split(os.path.dirname(os.path.realpath(__file__)))[0])
from bluezero import peripheral


sense = SenseHat()


def get_sensehat_pressure():
if pressure_characteristic.notifying:
print(int(sense.pressure))
pressure_characteristic.send_notify_event(int(sense.pressure))
return True
else:
return False

def ble_change_update_rate():
peripheral.GObject.timeout_add(1000, get_sensehat_pressure)



pressure_service = peripheral.Service(
'11118000-2222-3333-4444-555566667777',
True)


# Pressure
pressure_characteristic = peripheral.Characteristic(
'11118010-2222-3333-4444-555566667777',
['read', 'notify'],
pressure_service,
value=0)

pressure_characteristic.add_notify_event(ble_change_update_rate)
# Descriptor
pressure_descriptor = peripheral.UserDescriptor('Pressure', pressure_characteristic)
pressure_characteristic.add_descriptor(pressure_descriptor)

# Update Frequency
update_characteristic = peripheral.Characteristic(
'11118011-2222-3333-4444-555566667777',
['read', 'write'],
pressure_service,
value=1020)
update_characteristic.add_write_event(ble_change_update_rate)
# Descriptor
update_descriptor = peripheral.UserDescriptor('update rate', update_characteristic)
update_characteristic.add_descriptor(update_descriptor)

pressure_service.add_characteristic(pressure_characteristic)
pressure_service.add_characteristic(update_characteristic)

app = peripheral.Application()
app.add_service(pressure_service)

app.add_device_name('SensePressure')

# Start service and advertise
try:
app.start()
except KeyboardInterrupt:
print('KeyboardInterrupt')
finally:
app.stop()
print('finally')

0 comments on commit 7a242d9

Please sign in to comment.