Forum: Projekte & Code CSC Beispiel mit Bluetoe


von Torsten R. (Firma: Torrox.de) (torstenrobitzki)


Lesenswert?

Hallo,
nach dem Bluetoe (Beitrag "Open Source Bluetooth LE GATT Server") jetzt so 
weit ist, dass man damit funktionierende Bluetooth LE peripherals auf 
nrf51422s implementieren kann, habe ich mal das Cycling Speed and 
Cadence Profil als Library Funktionalität umgesetzt und damit ein 
kleines Beispiel gebastelt:
1
#include <bluetoe/bindings/nrf51.hpp>
2
#include <bluetoe/services/csc.hpp>
3
#include <bluetoe/server.hpp>
4
#include <bluetoe/sensor_location.hpp>
5
#include <nrf.h>
6
7
struct handler {
8
9
    /*
10
     * Functions required by the bluetoe CSC implementation to support wheel and crank revolutions
11
     */
12
    std::pair< std::uint32_t, std::uint16_t > cumulative_wheel_revolutions_and_time()
13
    {
14
        return std::make_pair( wheel_revolutions_, last_wheel_event_time_ );
15
    }
16
17
    std::pair< std::uint16_t, std::uint16_t > cumulative_crank_revolutions_and_time()
18
    {
19
        return std::make_pair( crank_revolutions_, last_crank_event_time_ );
20
    }
21
22
    void set_cumulative_wheel_revolutions( std::uint32_t new_value );
23
24
    volatile std::uint32_t wheel_revolutions_;
25
    volatile std::uint16_t last_wheel_event_time_;
26
27
    volatile std::uint32_t crank_revolutions_;
28
    volatile std::uint16_t last_crank_event_time_;
29
};
30
31
static constexpr char server_name[] = "Gruener Blitz";
32
33
using bicycle = bluetoe::server<
34
    bluetoe::server_name< server_name >,
35
    bluetoe::appearance::cycling_speed_and_cadence_sensor,
36
    bluetoe::cycling_speed_and_cadence<
37
        bluetoe::sensor_location::hip,
38
        bluetoe::csc::wheel_revolution_data_supported,
39
        bluetoe::csc::crank_revolution_data_supported,
40
        bluetoe::csc::handler< handler >
41
    >
42
>;
43
44
bicycle gatt;
45
46
void handler::set_cumulative_wheel_revolutions( std::uint32_t new_value )
47
{
48
    wheel_revolutions_ = new_value;
49
    gatt.confirm_cumulative_wheel_revolutions( gatt );
50
}
51
52
bluetoe::nrf51< bicycle > server;
53
54
void init_bike_hardware();
55
56
int main()
57
{
58
    init_bike_hardware();
59
60
    for ( ;; )
61
        server.run( gatt );
62
}
63
// ab hier kommt der nrf51 hardware-Teil: https://github.com/TorstenRobitzki/bluetoe/blob/master/examples/nrf51/cycling_speed_and_cadence.cpp

Die Definition des Servers steckt in dem Typen bicycle. Meinungen?

mfg Torsten

von Torsten R. (Firma: Torrox.de) (torstenrobitzki)


Lesenswert?

Und hier ein noch kleineres Beispiel, mit dem man einfach eine LED via 
Bluetooth an und aus schalten kann:
1
#include <bluetoe/server.hpp>
2
#include <bluetoe/bindings/nrf51.hpp>
3
#include <nrf.h>
4
5
using namespace bluetoe;
6
7
static constexpr int io_pin = 19;
8
9
static std::uint8_t io_pin_write_handler( bool state )
10
{
11
    NRF_GPIO->OUT = state
12
        ? NRF_GPIO->OUT | ( 1 << io_pin )
13
        : NRF_GPIO->OUT & ~( 1 << io_pin );
14
15
    return error_codes::success;
16
}
17
18
typedef server<
19
    service<
20
        service_uuid< 0xC11169E1, 0x6252, 0x4450, 0x931C, 0x1B43A318783B >,
21
        characteristic<
22
            free_write_handler< bool, io_pin_write_handler >
23
        >
24
    >
25
> blinky_server;
26
27
blinky_server gatt;
28
29
nrf51< blinky_server > gatt_srv;
30
31
int main()
32
{
33
    // Init GPIO pin
34
    NRF_GPIO->PIN_CNF[ io_pin ] =
35
        ( GPIO_PIN_CNF_DRIVE_S0H1 << GPIO_PIN_CNF_DRIVE_Pos ) |
36
        ( GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos );
37
38
    for ( ;; )
39
        gatt_srv.run( gatt );
40
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.