-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Maxbotix.h
118 lines (104 loc) · 2.15 KB
/
Maxbotix.h
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
#ifndef Maxbotix_h
#define Maxbotix_h
// Configuration
//#define MAXBOTIX_WITH_SOFTWARE_SERIAL 1
#include <Arduino.h>
#ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL
#include "RxSoftwareSerial.h"
#endif
class Maxbotix
{
public:
// models
typedef enum {
LV,
XL,
HRLV
}
MAXBOTIX_MODEL_t;
// inputs
typedef enum {
PW,
AN,
#ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL
TX
#endif
}
MAXBOTIX_INPUT_t;
// filters
typedef enum {
NONE,
MEDIAN,
HIGHEST_MODE,
LOWEST_MODE,
BEST,
SIMPLE
}
MAXBOTIX_FILTER_t;
// init/delete
Maxbotix(uint8_t pin, MAXBOTIX_INPUT_t input, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter = NONE,
uint8_t sample_size = 0);
#ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL
Maxbotix(Stream* serial, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter = NONE, uint8_t sample_size = 0);
#endif
~Maxbotix();
// simple api
float getRange();
uint8_t getSampleSize()
{
return sample_size;
};
// advanced api
void readSample();
float* getSample()
{
return sample;
};
float getSampleMedian();
float getSampleMode(bool highest = true);
float getSampleBest();
// getters
MAXBOTIX_MODEL_t getModel()
{
return model;
}
MAXBOTIX_INPUT_t getInput()
{
return input;
}
MAXBOTIX_FILTER_t getFilter()
{
return filter;
}
// setters
void setADSampleDelay(uint8_t delay)
{
ad_sample_delay = delay;
}
// utilities
static float toCentimeters(float inches)
{
return 2.54 * inches;
};
static float toInches(float centimeters)
{
return centimeters / 2.54;
};
private:
// config variables
uint8_t pin;
MAXBOTIX_INPUT_t input;
MAXBOTIX_MODEL_t model;
MAXBOTIX_FILTER_t filter;
uint8_t sample_size;
uint8_t ad_sample_delay;
Stream* serial;
// core
float* sample;
void init();
float readSensor();
unsigned short readSensorSerial(uint8_t length);
void pushToSample(float value);
void sortSample();
};
#endif