Root/fw/sample.c

Source at commit 8eca4c99c74a195272740ee944affd783734fff5 created 8 years 3 months ago.
By Werner Almesberger, fw/sample.c: fix logic in handler() and filter incomplete samples
1/*
2 * fw/sample.c - Acceleration sensor sample protocol
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stddef.h>
15#include <stdbool.h>
16#include <stdint.h>
17
18#include <avr/interrupt.h>
19
20#include "rf.h"
21#include "sweep.h"
22#include "accel.h"
23#include "proto.h"
24#include "dispatch.h"
25
26#include "io.h"
27
28/* @@@ keep it small for now - we're running out of RAM :-( */
29//#define MAX_PACKET 120 /* <- MAX_PSDU -3 (hdr) -2 (CRC) */
30#define MAX_PACKET 50 /* <- MAX_PSDU -3 (hdr) -2 (CRC) */
31
32static uint8_t buf[MAX_PACKET+3] = { SAMPLES, 0, 0 };
33static uint16_t *p;
34static bool expect_x;
35
36
37static void handler(bool x, uint16_t v)
38{
39    uint32_t t;
40
41    if (x != expect_x)
42        return;
43    t = 0; //uptime_irq();
44    if (p == (uint16_t *) (buf+3))
45        *p++ = t >> 16;
46    *p++ = t;
47    *p++ = v;
48    expect_x = !expect_x;
49
50    if (x)
51        return;
52    if ((uint8_t *) (p+4) <= buf+MAX_PACKET)
53        return;
54
55    rf_send(buf, (uint8_t *) p-buf);
56    buf[1]++;
57    p = (uint16_t *) (buf+3);
58}
59
60
61static bool sample_first(uint8_t limit, const uint8_t *payload)
62{
63    if (payload[0]) {
64        buf[1] = 0;
65        cli();
66        sample = handler;
67        p = (uint16_t *) (buf+3);
68        expect_x = 1;
69        sei();
70    } else {
71        cli();
72        sample = NULL;
73        sei();
74    }
75    return 1;
76}
77
78
79static bool sample_more(uint8_t seq, uint8_t limit, const uint8_t *payload)
80{
81    return 0;
82}
83
84
85struct handler sample_handler = {
86        .type = SAMPLE,
87        .first = sample_first,
88        .more = sample_more,
89};
90

Archive Download this file

Branches:
master
tornado-v1



interactive