forked from KRproject-tech/binlog4teensy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
106 lines (72 loc) · 2.86 KB
/
main.ino
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
// The SdFat library supports FAT16, FAT32, and exFAT file systems on Standard SD, SDHC, and SDXC cards.
// * SD library does not support exFAT. *
#include "SdFat.h"
#define INT_T 2 // Interrupt time [ms]
IntervalTimer intTimer;
SdExFat sd;
ExFile fp;
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
int global_time_ms = 0; // time [ms]
void setup() {
// put your setup code here, to run once:
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
// USB serial (to PC)
Serial.begin( 921600);
// SD access
sd.begin( SdioConfig( FIFO_SDIO));
sd.remove( "data.log"); // delete if "data.log" file is aleady exist
fp = sd.open( "data.log", FILE_WRITE);
// timer interrupt
//FlexiTimer2::set( INT_T, flip);
//FlexiTimer2::start();
// for Teensy 4.x, FlexiTimer2 doesn't exactly interrupt.
intTimer.begin( flip, INT_T*1000); // [us]
}
//--------------------------------------------------------------
//---------------------- timer interrupt function ----------------------
//--------------------------------------------------------------
void flip(){
float out[3] = { 0 };
short w_data[5] = { 0 }; // logging data(2byte)
char w_cdata[5*2] = { 0 }; // logging data(1byte)
int idx = 0;
//[*] ------------- time [ms] -------------------------
global_time_ms += INT_T;
global_time_ms = (global_time_ms < 1000*1000) ? global_time_ms : 0;
//[*] ------------- data [-] -------------------------
out[0] = 2*sin( 2*PI*(float)global_time_ms/1000.0);
out[1] = cos( 2*PI*(float)global_time_ms/1000.0);
out[2] = 0.5*sin( 2*PI*2.0*(float)global_time_ms/1000.0);
//[*] -------- short int(2bytes) convert ---------------
w_data[0] = 0xAAAA;
w_data[1] = (short)( global_time_ms );
for(idx=0;idx<3;idx++)
w_data[2+idx] = (short)( 100*out[idx] );
//[*] ------------- char(1bytes) convert ---------------
// 2byte -> 1byte (Little Endian)
for(idx=0;idx<5;idx++){
w_cdata[2*idx+1] = (char)( (w_data[idx] >> 8) & 0x00FF );
w_cdata[2*idx] = (char)( w_data[idx] & 0x00FF );
}
//[*] ----------- Write to binary file --------------
if( global_time_ms <= 60*1000 ){
// write
if( fp )
fp.write( w_cdata, 5*2); // Write 1 bytes at a time.
}else{
if( fp )
fp.close();
}
//[*] ---------- send to PC -----------------------------
Serial.printf( "AA,%f[s],%f[-],%f[-],%f[-]\r\n", (float)global_time_ms/1000.0, out[0], out[1], out[2]);
}
void loop() {
// put your main code here, to run repeatedly:
// set the LED with the ledState of the variable:
digitalWrite(ledPin, 1);
delay(100); // [ms]
digitalWrite(ledPin, 0);
delay(100); // [ms]
}