-
Notifications
You must be signed in to change notification settings - Fork 2
/
main-fps.c
executable file
·170 lines (140 loc) · 4 KB
/
main-fps.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Wired light sensor
*
* Copyright 2013 Tuomas Kulve, <[email protected]>
*
* 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 "common.h"
#include "adc.h"
#include "led.h"
#include "fps.h"
#include "uart.h"
#include "utils.h"
#include "msp430.h"
#include "RF1A.h"
#include "hal_pmm.h"
#include <stdint.h>
/*
The wiring for this test is as follows:
3.3V - LDR----
|
A3 ----------+
|
GND - 10kR --
The relative size of the LDR and normal resistor calibrates the level A0 reads.
LDR ohms on my 22" IPS monitor:
- white xterm: 7k
- black xterm: 45k
*/
int main(void)
{
uint8_t led_count = 0;
uint32_t timestamp_counter = 0;
uint32_t timestamp_last_frame = 0;
uint8_t channels[1] = {ADC12INCH_3};
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
// Increase PMMCOREV level to 2 for proper radio operation
SetVCore(2);
// Initialize all ports to output low
// FIXME: implement a GPIO class
P1OUT = 0x00;
P1DIR = 0xFF;
P2OUT = 0x00;
P2DIR = 0xFF;
P3OUT = 0x00;
P3DIR = 0xFF;
P5OUT = 0x00;
P5DIR = 0xFF;
PJOUT = 0x00;
PJDIR = 0xFF;
// P2.3 (A3) as ADC
P2SEL |= BIT3;
// P2.3 (A3) as input
P2DIR &= ~BIT3;
led_init();
uart_init();
#if SC_USE_SLEEP == 0
// Enable interrupts
__bis_status_register(GIE);
#endif
// Initiate channel A3 measurement @ 1000 Hz
adc_start(sizeof(channels), channels, ADC12SHT03 | ADC12SHT02, ADC_MODE_CONT);
while(1) {
uint8_t fps;
uint16_t missed_adc;
uint16_t adc_value;
uint32_t timestamp_ms;
uint16_t low;
uint16_t low_limit;
uint16_t high_limit;
uint16_t high;
// Wait for the ADC interrupt
while (adc_state != ADC_STATE_DATA) {}
adc_get_data(1, &adc_value, ×tamp_ms);
++timestamp_counter;
missed_adc += timestamp_ms - timestamp_counter;
led_count += timestamp_ms - timestamp_counter;
if (led_count++ > 100) {
led_count = 0;
led_toggle(1);
}
timestamp_counter = timestamp_ms;
#if 0
{
uint8_t buf[64];
uint8_t len = 0;
len += sc_itoa(missed_adc, &buf[len], 64 - len);
buf[len++] = '\r';
buf[len++] = '\n';
uart_tx_append_msg(buf, len);
uart_send_next_msg();
}
#endif
if (handle_adc(adc_value, timestamp_ms, &fps, &low, &low_limit, &high_limit, &high)) {
uint8_t buf[64];
uint8_t len;
len = create_message(buf, 64,
timestamp_ms,
fps,
missed_adc,
(uint16_t)(timestamp_ms - timestamp_last_frame),
low,
low_limit,
high_limit,
high);
uart_tx_append_msg(buf, len);
uart_send_next_msg();
timestamp_last_frame = timestamp_ms;
missed_adc = 0;
}
}
}
/* Emacs indentatation information
Local Variables:
indent-tabs-mode:nil
tab-width:2
c-basic-offset:2
End:
*/