-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyfunc.h
165 lines (131 loc) · 6.32 KB
/
myfunc.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
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
/*- -----------------------------------------------------------------------------------------------------------------------
* FDL-2 arduino implementation
* 2018-01-17 <[email protected]> Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* - -----------------------------------------------------------------------------------------------------------------------
* - all supporting functions, like pin setup, pin change interrupt handling, etc ------------------------------------------
* special thanks to Jesse Kovarovics http://www.projectfdl.com to make this happen
* - -----------------------------------------------------------------------------------------------------------------------
*/
#ifndef _MYFUNC_h
#define _MYFUNC_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <stdint.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
class waittimer {
private: //---------------------------------------------------------------------------------------------------------
uint32_t startTime;
uint32_t checkTime;
public: //---------------------------------------------------------------------------------------------------------
waittimer();
uint8_t done(void);
void set(uint32_t wait_millis);
uint32_t remain(void);
uint8_t completed(void);
};
//- pin definition ----------------------------------------------------------------------------------------------------------
#define pc_interrupt_vectors 3 // amount of pin change interrupt vectors
#define pinD0 (0) // &DDRD, &PORTD, &PIND, 16, PCINT16, &PCICR, &PCMSK2, PCIE2, 2
#define pinD1 (1)
#define pinD2 (2)
#define pinD3 (3)
#define pinD4 (4)
#define pinD5 (5)
#define pinD6 (6)
#define pinD7 (7)
#define pinB0 (8) // &DDRB, &PORTB, &PINB, 0, PCINT0, &PCICR, &PCMSK0, PCIE0, 0
#define pinB1 (9)
#define pinB2 (10)
#define pinB3 (11)
#define pinB4 (12)
#define pinB5 (13)
#define pinC0 (14)
#define pinC1 (15)
#define pinC2 (16)
#define pinC3 (17)
#define pinC4 (18)
#define pinC5 (19)
#define pinC6 (20)
#define pinC7 (21)
//- -------------------------------------------------------------------------------------------------------------------------
/*-- pin functions --------------------------------------------------------------------------------------------------------
* all pins defined as a struct, holding all information regarding port, pin, ddr, etc.
* as we support different arduino hw i tried to make it as flexible as possible. everything is defined in seperate
* hw specific files. the struct and pin manipulation function is defined in HAL_atmega.h because it is similar for all
* ATMEL hardware, the pin structs are defined in HAL_atmega_<model> while different for each cpu type. here we reference
* only on the functions defined in HAL_<type>_<model>.
*/
void set_pin_output(uint8_t pin_def);
void set_pin_input(uint8_t pin_def);
void set_pin_high(uint8_t pin_def);
void set_pin_low(uint8_t pin_def);
uint8_t get_pin_status(uint8_t pin_def);
//- -----------------------------------------------------------------------------------------------------------------------
/*-- interrupt functions --------------------------------------------------------------------------------------------------
* interrupts again are very hardware supplier related, therefor we define her some external functions which needs to be
* defined in the hardware specific HAL file. for ATMEL it is defined in HAL_atmega.h.
* you can also use the arduino standard timer for a specific hardware by interlinking the function call to getmillis()
*/
#define DEBOUNCE 5 // debounce time for periodic check if an interrupt was raised
extern void(*pci_ptr)(uint8_t vec, uint8_t pin, uint8_t flag);
void register_PCINT(uint8_t pin_def);
uint8_t check_PCINT(uint8_t pin_def, uint8_t debounce);
void maintain_PCINT(uint8_t vec);
//- -----------------------------------------------------------------------------------------------------------------------
/*-- timer functions ------------------------------------------------------------------------------------------------------
* as i need timer0 interrupt for the encoder service i have to define an own millis() timer here.
*/
// https://github.com/zkemble/millis/blob/master/millis/
extern volatile uint32_t milliseconds;
void init_millis_timer0(); // initialize timer0
uint32_t get_millis(void); // get the current time in millis
/*-- eeprom functions -----------------------------------------------------------------------------------------------------
* eeprom is very hardware supplier related, therefor we define her some external functions which needs to be defined
* in the hardware specific HAL file. for ATMEL it is defined in HAL_atmega.h.
*/
void init_eeprom(void);
void get_eeprom(uint16_t addr, uint8_t len, void *ptr);
void set_eeprom(uint16_t addr, uint8_t len, void *ptr);
void clear_eeprom(uint16_t addr, uint16_t len);
/*-- serial print functions -----------------------------------------------------------------------------------------------
* template and some functions for debugging over serial interface
* based on arduino serial class, so should work with all hardware served in arduino
* http://aeroquad.googlecode.com/svn/branches/pyjamasam/WIFIReceiver/Streaming.h
*/
class NullSerial : public Print {
public:
virtual size_t write(uint8_t) { return (1); }
void begin(int16_t) {}
};
NullSerial static Noserial;
#ifdef DEBUG
#define dbg_m Serial
#else
#define dbg_m Noserial
#endif
#define dbg Serial
template<class T> Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
const char num2char[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', };
struct _HEX {
uint8_t *val;
uint8_t len;
_HEX(uint8_t v) : val(&v), len(1) {}
_HEX(uint8_t *v, uint8_t l = 1) : val(v), len(l) {}
};
inline Print &operator <<(Print &obj, const _HEX &arg) {
for (uint8_t i = 0; i<arg.len; i++) {
if (i) obj.print(' ');
obj.print(num2char[arg.val[i] >> 4]);
obj.print(num2char[arg.val[i] & 0xF]);
}
return obj;
}
enum _eTIME { _TIME };
inline Print &operator <<(Print &obj, _eTIME arg) { obj.print('('); obj.print(get_millis()); obj.print(')'); return obj; }
//- -----------------------------------------------------------------------------------------------------------------------
#endif