-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinescanner.cpp
107 lines (91 loc) · 2.15 KB
/
Linescanner.cpp
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
#include "Linescanner.h"
void Linescanner::scan(int expose) {
//Initialize scan (SI)
digitalWrite(CLK,LOW);
digitalWrite(sync,HIGH);
digitalWrite(CLK,HIGH);
digitalWrite(sync,LOW);
digitalWrite(CLK,LOW);
//CLK all pixels
for (int i=0;i<128;i++) {
digitalWrite(CLK,HIGH);
digitalWrite(CLK,LOW);
}
//Pixel charge transfer time (t_qt)
delayMicroseconds(expose);
digitalWrite(sync,HIGH);
digitalWrite(CLK,HIGH);
digitalWrite(sync,LOW);
digitalWrite(CLK,LOW);
}
void Linescanner::read(int* pixels) {
//Reading data from cam
for (int i=0;i<128;i++) {
pixels[i] = analogRead(data);
digitalWrite(CLK,HIGH);
digitalWrite(CLK,LOW);
}
}
int Linescanner::calibrate(int expose, int* pixels) {
// scan(expose);
// read(pixels);
byte maxIdx = findMax(pixels);
float high_avg = average(maxIdx-5,maxIdx+5, pixels);
float low_avg = average(0, 5, 123, 128, pixels);
return (high_avg-low_avg)/2 + low_avg;
}
byte Linescanner::findMax(const int* pixels) {
int maxim = 0;
byte idx;
for (int i = 0; i < 128; i++) {
if (pixels[i] > maxim) {
maxim = pixels[i];
idx = i;
}
}
return idx;
}
float Linescanner::average(int lowIdx, int highIdx, const int* pixels) {
int sum = 0;
for (int i = lowIdx; i < highIdx+1; i++) {
sum += pixels[i];
}
return sum/11;
}
float Linescanner::average(int low_lowIdx, int low_highIdx,
int high_lowIdx, int high_highIdx, const int* pixels) {
int sum = 0;
for (int i = low_lowIdx; i < low_highIdx; i++) {
sum += pixels[i];
}
for (int i = high_lowIdx; i < high_highIdx; i++) {
sum+= pixels[i];
}
return sum/10;
}
void Linescanner::printLine(int array[]) {
if (array[64] > 1) {
for (int i=0;i<128;i++) {
Serial.print(array[i]);
Serial.print(" ");
}
} else {
for (int i=0;i<128;i++) {
Serial.print(array[i]);
}
}
Serial.println();
}
void Linescanner::printLine(byte array[]) {
if (array[64] > 1) {
for (int i=0;i<128;i++) {
Serial.print(array[i]);
Serial.print(" ");
}
} else {
for (int i=0;i<128;i++) {
Serial.print(array[i]);
}
}
Serial.println();
}