summaryrefslogtreecommitdiff
path: root/peripheral/libmraa/examples/firmata_curie_imu.c
blob: 1564d110b43666fccb4925298751b11239f21ffc (plain)
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
/*
 * Author: Brendan Le Foll
 * Copyright (c) 2016 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <unistd.h>

#include "mraa.h"
#include "mraa/firmata.h"

#define FIRMATA_START_SYSEX 0xF0
#define FIRMATA_END_SYSEX 0xF7
#define FIRMATA_CURIE_IMU 0x11
#define FIRMATA_CURIE_IMU_READ_ACCEL 0x00

void
interrupt(uint8_t* buf, int length)
{
    printf("reg read returned: %d, with buffer size %d\n", ((buf[6] & 0x7f) | ((buf[7] & 0x7f) << 7)), length);
}

int
main()
{
    mraa_init();
    //! [Interesting]

   /**
    * This example reads from the FirmataCurieIMU plugin
    */

    mraa_add_subplatform(MRAA_GENERIC_FIRMATA, "/dev/ttyACM0");
    mraa_firmata_context firm = mraa_firmata_init(FIRMATA_CURIE_IMU);
    if (firm == NULL) {
        return EXIT_FAILURE;
    }

    mraa_firmata_response(firm, interrupt);

    uint8_t* buffer = calloc(4, 0);
    if (buffer == NULL) {
        free(firm);
        return EXIT_FAILURE;
    }
    buffer[0] = FIRMATA_START_SYSEX;
    buffer[1] = FIRMATA_CURIE_IMU;
    buffer[2] = FIRMATA_CURIE_IMU_READ_ACCEL;
    buffer[3] = FIRMATA_END_SYSEX;

    mraa_firmata_write_sysex(firm, buffer, 4);

    sleep(1);

    // stop the isr and set it again
    mraa_firmata_response_stop(firm);
    mraa_firmata_response(firm, interrupt);
    mraa_firmata_write_sysex(firm, buffer, 4);

    sleep(1);

    // close everything and try again
    mraa_firmata_close(firm);
    firm = mraa_firmata_init(FIRMATA_CURIE_IMU);
    mraa_firmata_response(firm, interrupt);
    mraa_firmata_write_sysex(firm, buffer, 4);

    sleep(10);

    mraa_firmata_close(firm);
    //! [Interesting]

    return EXIT_SUCCESS;
}