Test program for magnetometer
Dependencies: mbed AES BLE_API nRF51822 smallAES
ButtonAService.h@2:4871b5ad7938, 2019-09-30 (annotated)
- Committer:
- f3d
- Date:
- Mon Sep 30 10:38:33 2019 +0000
- Revision:
- 2:4871b5ad7938
Code for the LSM303 version of the BBC Microbit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 2:4871b5ad7938 | 1 | #ifndef __BUTTONA_SERVICE_H__ |
f3d | 2:4871b5ad7938 | 2 | #define __BUTTONA_SERVICE_H__ |
f3d | 2:4871b5ad7938 | 3 | // Button A is on P0 bit 17 |
f3d | 2:4871b5ad7938 | 4 | volatile uint32_t * P0OUT = (uint32_t *)0x50000504; |
f3d | 2:4871b5ad7938 | 5 | volatile uint32_t * P0DIR = (uint32_t *)0x50000514; |
f3d | 2:4871b5ad7938 | 6 | volatile uint32_t * P0IN = (uint32_t *)0x50000510; |
f3d | 2:4871b5ad7938 | 7 | volatile uint32_t * P0CONF = (uint32_t *)(0x50000700); |
f3d | 2:4871b5ad7938 | 8 | int8_t initialValue=0; |
f3d | 2:4871b5ad7938 | 9 | class ButtonAService { |
f3d | 2:4871b5ad7938 | 10 | public: |
f3d | 2:4871b5ad7938 | 11 | const static uint16_t BUTTONA_SERVICE_UUID = 0x1eee; |
f3d | 2:4871b5ad7938 | 12 | const static uint16_t BUTTONA_STATE_CHARACTERISTIC_UUID = 0x2019; |
f3d | 2:4871b5ad7938 | 13 | |
f3d | 2:4871b5ad7938 | 14 | ButtonAService(BLEDevice &_ble) : |
f3d | 2:4871b5ad7938 | 15 | ble(_ble), ButtonState(BUTTONA_STATE_CHARACTERISTIC_UUID,&initialValue,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
f3d | 2:4871b5ad7938 | 16 | { |
f3d | 2:4871b5ad7938 | 17 | GattCharacteristic *charTable[] = {&ButtonState}; |
f3d | 2:4871b5ad7938 | 18 | GattService btnService(BUTTONA_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
f3d | 2:4871b5ad7938 | 19 | ble.addService(btnService); |
f3d | 2:4871b5ad7938 | 20 | P0CONF[17] = 0; // On power up, input buffer is not connected so must do this |
f3d | 2:4871b5ad7938 | 21 | PreviousButtonState = -1; |
f3d | 2:4871b5ad7938 | 22 | } |
f3d | 2:4871b5ad7938 | 23 | |
f3d | 2:4871b5ad7938 | 24 | GattAttribute::Handle_t getValueHandle() const { |
f3d | 2:4871b5ad7938 | 25 | return ButtonState.getValueHandle(); |
f3d | 2:4871b5ad7938 | 26 | } |
f3d | 2:4871b5ad7938 | 27 | void poll() { |
f3d | 2:4871b5ad7938 | 28 | uint8_t newValue = GetButtonAState(); |
f3d | 2:4871b5ad7938 | 29 | if (newValue != PreviousButtonState) |
f3d | 2:4871b5ad7938 | 30 | { |
f3d | 2:4871b5ad7938 | 31 | // only send an update if the button state has changed (reduces traffic) |
f3d | 2:4871b5ad7938 | 32 | ble.gattServer().write(this->getValueHandle(), (uint8_t *)&newValue, sizeof(uint8_t)); |
f3d | 2:4871b5ad7938 | 33 | PreviousButtonState = newValue; |
f3d | 2:4871b5ad7938 | 34 | } |
f3d | 2:4871b5ad7938 | 35 | } |
f3d | 2:4871b5ad7938 | 36 | uint8_t GetButtonAState() |
f3d | 2:4871b5ad7938 | 37 | { |
f3d | 2:4871b5ad7938 | 38 | if ((*P0IN & (1 << 17))==0) |
f3d | 2:4871b5ad7938 | 39 | return 1; |
f3d | 2:4871b5ad7938 | 40 | else |
f3d | 2:4871b5ad7938 | 41 | return 0; |
f3d | 2:4871b5ad7938 | 42 | } |
f3d | 2:4871b5ad7938 | 43 | |
f3d | 2:4871b5ad7938 | 44 | private: |
f3d | 2:4871b5ad7938 | 45 | BLEDevice &ble; |
f3d | 2:4871b5ad7938 | 46 | ReadOnlyGattCharacteristic<int8_t> ButtonState; |
f3d | 2:4871b5ad7938 | 47 | uint8_t PreviousButtonState; |
f3d | 2:4871b5ad7938 | 48 | |
f3d | 2:4871b5ad7938 | 49 | }; |
f3d | 2:4871b5ad7938 | 50 | |
f3d | 2:4871b5ad7938 | 51 | #endif |