-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_Digital.h
760 lines (667 loc) · 21.5 KB
/
fast_Digital.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/**
* \file fast_Digital.h
*
* This is a port-manipulation library for doing things similar to
* what you'd find in \c <wiring_digital.c> , e.g. \c digitalWrite() ,
* \c digitalRead() and \c pinMode() .
*
* The primary benefit of using this library is that it uses templates
* to compile-time optimize the operations.
*
* Included as well are some extra convenience structures and functions.
*
* \date Feb 1, 2015
* \author Lyle Moffitt
* \copyright MIT License
*/
#ifndef FAST_DIGITAL_H_
#define FAST_DIGITAL_H_
#include <stddef.h>
#include <avr/io.h>
#ifdef CONSTEXPR_ARDUINO_PINS
# include <Arduino.h>
#endif
/**
* A simple struct representing a byte as a list of
*/
template< typename value_t = byte>
struct bit_list{
typedef value_t value_type;
/// The value constructed from the initializer list
value_type value;
/// Cast to \c value_type
inline constexpr operator value_type() const{
return value;
}
/// The size of the value in bits
size_t inline static constexpr bit_size(){
return (sizeof(value_type)*8);
}
/// Default construct with \c NULL value
constexpr bit_list() : value(0x0){
}
/**
* Construct from a list of bit positions.
* \param b0...bN An initializer list of bit positions to set as true.
* Ex: { 7,3,1 } == B10001010
* \note Index values must be positive and less than \c bit_size()
* \note The number of indexes must be less than \c bit_size()
*/
template<typename t0, typename ... tN>
constexpr bit_list( t0 b0, tN ... bN ) : value( (1<<((value_type(b0))%bit_size())) | bit_list(bN...).value ){
static_assert( (sizeof...(bN) + 1) < bit_size(),"Too many members in initializer array.");
}
};
using _B = bit_list<byte>;
using _W = bit_list<uint16_t>;
static_assert(bit_list<>({7,3,1})==((1<<1)|(1<<3)|(1<<7)),"Bit order error.");
static_assert(bit_list<uint16_t>({15,14,7,3,1})==uint16_t((1<<15)|(1<<14)|(1<<7)|(1<<3)|(1<<1)),"Byte order error.");
/**
* An 8-bit union with a plethora of different breakdowns.
*/
union Bit_Mask {
/// The whole value of the byte
byte value;
/// The individual bits
struct {
byte b0 : 1; ///< B00000001
byte b1 : 1; ///< B00000010
byte b2 : 1; ///< B00000100
byte b3 : 1; ///< B00001000
byte b4 : 1; ///< B00010000
byte b5 : 1; ///< B00100000
byte b6 : 1; ///< B01000000
byte b7 : 1; ///< B10000000
};
/// The upper and lower half
struct {
byte lower_nibble : 4; ///< 0x0f
byte upper_nibble : 4; ///< 0xf0
};
/// Default construct with NULL value;
constexpr Bit_Mask() : value(0x0){
}
/**
* Value-construct from a byte value
*/
template<typename value_type = byte>
constexpr Bit_Mask( value_type v) : value(v){
}
// template<typename value_type = byte>
// constexpr Bit_Mask(const value_type & v) : value(static_cast<byte>(v)){
// }
/**
* Construct from an list of bits positions:
* ex. 0b01000101 == { 6,2,1 }
* @param bits The indexes of the bits that will be true
*/
constexpr Bit_Mask( bit_list<byte> bits ) : value(bits.value){
}
/**
* Cast to byte from \c (*this).value
*/
constexpr inline operator byte(){
return value;
}
// template<typename value_type = byte>
// constexpr inline operator value_type (){
// return static_cast<value_type>( value );
// }
}; // union Bit_Mask
static_assert(sizeof(Bit_Mask)==1,"Size of Bit_Mask incorrect: should be 1 byte.");
static_assert(Bit_Mask({0,4,7}).value == ((1<<0)|(1<<4)|(1<<7)),"Initializer bit-order mismatch.");
static_assert(Bit_Mask((1<<0)).b0,"Struct bit-field packing error: bit[0] mismatch.");
static_assert(Bit_Mask((1<<1)).b1,"Struct bit-field packing error: bit[1] mismatch.");
static_assert(Bit_Mask((1<<2)).b2,"Struct bit-field packing error: bit[2] mismatch.");
static_assert(Bit_Mask((1<<3)).b3,"Struct bit-field packing error: bit[3] mismatch.");
static_assert(Bit_Mask((1<<4)).b4,"Struct bit-field packing error: bit[4] mismatch.");
static_assert(Bit_Mask((1<<5)).b5,"Struct bit-field packing error: bit[5] mismatch.");
static_assert(Bit_Mask((1<<6)).b6,"Struct bit-field packing error: bit[6] mismatch.");
static_assert(Bit_Mask((1<<7)).b7,"Struct bit-field packing error: bit[7] mismatch.");
static_assert(Bit_Mask(0x30).upper_nibble==0x3,"Struct bit-field packing error: nibble mismatch.");
static_assert(Bit_Mask(0x07).lower_nibble==0x7,"Struct bit-field packing error: nibble mismatch.");
#ifdef CONSTEXPR_ARDUINO_PINS
#define ARDUINO_MAIN
#define __PGMSPACE_H_
#undef PROGMEM
#define PROGMEM constexpr
#undef Pins_Arduino_h
#include <pins_arduino.h>
#undef __PGMSPACE_H_
#undef PROGMEM
constexpr uint16_t inline pin_to_port(uint8_t pin){
return *(digital_pin_to_port_PGM + pin);
}
constexpr uint16_t inline pin_to_bit_mask(uint8_t pin){
return *(digital_pin_to_bit_mask_PGM + pin );
}
constexpr uint16_t inline pin_to_timer(uint8_t pin){
return *(digital_pin_to_timer_PGM + pin );
}
constexpr uint16_t inline pin_to_output_register(uint8_t pin){
return *(port_to_output_PGM + *(digital_pin_to_port_PGM + pin) );
}
constexpr uint16_t inline pin_to_input_register(uint8_t pin){
return *(port_to_input_PGM + *(digital_pin_to_port_PGM + pin) );
}
constexpr uint16_t inline pin_to_mode_register(uint8_t pin){
return *(port_to_mode_PGM + *(digital_pin_to_port_PGM + pin) );
}
template<uint16_t PORT_, uint16_t PIN_, uint16_t DDR_>
constexpr uint8_t inline mask_to_pin(uint16_t _mask,uint16_t indx=0){
return NUM_DIGITAL_PINS<=indx ? 0 :
( *(digital_pin_to_bit_mask_PGM + indx) != _mask
|| pin_to_output_register(indx) != DDR_
|| pin_to_input_register(indx) != PORT_
|| pin_to_mode_register(indx) != PIN_
) ? mask_to_pin<PORT_,PIN_,DDR_>(_mask,indx++) : indx;
}
#endif
/// Get the address of the PORTx which ends with specified letter
uint16_t constexpr inline letter_to_PORT_addr(char ltr){
return
#ifdef PORTA
ltr=='A' ? (uint16_t) & PORTA :
#endif
#ifdef PORTB
ltr=='B' ? (uint16_t) & PORTB :
#endif
#ifdef PORTC
ltr=='C' ? (uint16_t) & PORTC :
#endif
#ifdef PORTD
ltr=='D' ? (uint16_t) & PORTD :
#endif
#ifdef PORTE
ltr=='E' ? (uint16_t) & PORTE :
#endif
#ifdef PORTF
ltr=='F' ? (uint16_t) & PORTF :
#endif
0;
}
/// Get the address of the PINx which ends with specified letter
uint16_t constexpr inline letter_to_PIN_addr(char ltr){
return
#ifdef PINA
ltr=='A' ? (uint16_t) & PINA :
#endif
#ifdef PINB
ltr=='B' ? (uint16_t) & PINB :
#endif
#ifdef PINC
ltr=='C' ? (uint16_t) & PINC :
#endif
#ifdef PIND
ltr=='D' ? (uint16_t) & PIND :
#endif
#ifdef PINE
ltr=='E' ? (uint16_t) & PINE :
#endif
#ifdef PINF
ltr=='F' ? (uint16_t) & PINF :
#endif
0;
}
/// Get the address of the DDRx which ends with specified letter
uint16_t constexpr inline letter_to_DDR_addr(char ltr){
return
#ifdef DDRA
ltr=='A' ? (uint16_t) & DDRA :
#endif
#ifdef DDRB
ltr=='B' ? (uint16_t) & DDRB :
#endif
#ifdef DDRC
ltr=='C' ? (uint16_t) & DDRC :
#endif
#ifdef DDRD
ltr=='D' ? (uint16_t) & DDRD :
#endif
#ifdef DDRE
ltr=='E' ? (uint16_t) & DDRE :
#endif
#ifdef DDRF
ltr=='F' ? (uint16_t) & DDRF :
#endif
0;
}
// Simplifies defining all the operators necessary for MMIO
#define define_operator( _op_ ) \
template< typename data_t = value_type > \
value_type inline operator _op_ (data_t val){ \
return ((* (volatile data_t *)(addr) ) _op_ val); \
} \
#define REG( _PORT_ , _TYPE_ ) MMIO< (uint16_t)&_PORT_ , _TYPE_ >
/**
* Memory-Mapped Input/Output
* \param <addr> The address of the port to be manipulated.
* \param <val_t> The type of value that can be read from and written to the port. [default \c uint8_t]
*/
template< uint16_t addr, class val_t=uint8_t>
struct MMIO{
/// The type of value that can be read from and written to the port. [default \c uint8_t]
typedef val_t value_type;
/**
* Read the Memory-Mapped register
* @return The value of the register cast to \c value_type
*/
value_type inline static read(){
return (* (volatile value_type *)(addr) );
}
/**
* Write to the Memory-Mapped register
* @param value The value to write to the register.
* @return The value that was written.
*/
void inline static write(value_type value){
(* (volatile value_type *)(addr) ) = value;
}
/* ************************************************************************* */
// "Fancy" operators
/// Cast operator; performs the same operation as \c read()
template<typename data_t = value_type>
inline operator data_t (){
return (* (volatile data_t *)(addr) );
}
/// Pointer to member access operator; useful for accessing bit-field members.
inline value_type * operator->() {
return ((value_type *)(addr)) ;
}
/// Assignment operator; performs the same operation as \c write()
template< typename data_t = value_type >
value_type inline operator = (data_t val){
return (* (( value_type *)(addr)) ) = static_cast<value_type>(val);
}
// define_operator( = )
/* ************************************************************************* */
// Bitwise operators
/// Bitwise NOT operator
value_type inline operator~() {
return ~static_cast<unsigned>(* (value_type *)(addr) );
}
/// Bitwise AND operator.
define_operator( & )
/// Bitwise OR operator.
define_operator( | )
/// Bitwise XOR operator.
define_operator( ^ )
/// Bitwise shift-left operator.
define_operator( << )
/// Bitwise shift-right operator.
define_operator( >> )
/// Bitwise AND-EQUALS operator.
define_operator( &= )
/// Bitwise OR-EQUALS operator.
define_operator( |= )
/// Bitwise XOR-EQUALS operator.
define_operator( ^= )
/// Bitwise SHIFT-LEFT-EQUALS operator.
define_operator( <<= )
/// Bitwise SHIFT-RIGHT-EQUALS operator.
define_operator( >>= )
/* ************************************************************************* */
// Arithmetic operators
/// Arithmetic ADD operator.
define_operator( + )
/// Arithmetic MINUS operator.
define_operator( - )
/// Arithmetic MULTIPLY operator.
define_operator( * )
/// Arithmetic DIVIDE operator.
define_operator( / )
/// Arithmetic MODULUS operator.
define_operator( % )
/// Arithmetic ADD-EQUALS operator.
define_operator( += )
/// Arithmetic MINUS-EQUALS operator.
define_operator( -= )
/// Arithmetic MULTIPLY-EQUALS operator.
define_operator( *= )
/// Arithmetic DIVIDE-EQUALS operator.
define_operator( /= )
/// Arithmetic MODULUS-EQUALS operator.
define_operator( %= )
};// struct MMIO
#undef define_operator
/**
* Macro to easily specify PORTx, PINx, and DDRx by letter.
* \param _X_ The letter of the port, ex: A, B, C, etc..
* \sa PIN_base, PORT_base, PIN, PORT
*/
#define P( _X_ ) (uint16_t)&PORT##_X_, (uint16_t)&PIN##_X_, (uint16_t)&DDR##_X_
/**
* A PORT manipulator using:
* - a port-letter { A, B, C, etc...} <or>
* - a triplet of port-addresses { & PORTx, & PINx, & DDRx }
*/
template<uint16_t ...>
struct PIN_base;
#ifdef CONSTEXPR_ARDUINO_PINS
template<uint16_t _pin>
struct PIN_base<_pin>
{
static_assert(pin_to_output_register(_pin)!=NOT_A_PORT,"Invalid pin number: NOT_A_PORT");
static_assert(pin_to_input_register(_pin)!=NOT_A_PORT,"Invalid pin number: NOT_A_PORT");
static_assert(pin_to_mode_register(_pin)!=NOT_A_PORT,"Invalid pin number: NOT_A_PORT");
typedef MMIO<pin_to_output_register(_pin)> PORTX;
typedef MMIO<pin_to_input_register(_pin)> PINX ;
typedef MMIO<pin_to_mode_register(_pin)> DDRX ;
constexpr const static uint8_t MASK = pin_to_bit_mask(_pin);
constexpr const static uint8_t INDEX = _pin;
};
#endif //CONSTEXPR_ARDUINO_PINS
/**
* Base the \c PIN definition off of a specified letter
* \param _letter The address of DDRx
* \param bit_place The bit position of the pin within the PORTx
*/
template<uint16_t _letter, uint16_t bit_place>
struct PIN_base<_letter, bit_place>
{
static_assert(letter_to_PORT_addr(_letter)!=0, "Invalid pin letter: NOT_A_PORT");
static_assert(letter_to_PIN_addr(_letter)!=0, "Invalid pin letter: NOT_A_PORT");
static_assert(letter_to_DDR_addr(_letter)!=0, "Invalid pin letter: NOT_A_PORT");
typedef MMIO<letter_to_PORT_addr(_letter)> PORTX;
typedef MMIO<letter_to_PIN_addr(_letter)> PINX ;
typedef MMIO<letter_to_DDR_addr(_letter)> DDRX ;
constexpr const static uint8_t MASK = (0x01<<bit_place);
constexpr const static uint8_t INDEX = bit_place;
};
/**
* Base the \c PIN definition off of specified addresses
* \param PORT_addr The address of PORTx
* \param PIN_addr The address of PINx
* \param DDR_addr The address of DDRx
* \param bit_place The bit position of the pin within the PORTx
*/
template<uint16_t PORT_addr, uint16_t PIN_addr, uint16_t DDR_addr, uint16_t bit_place>
struct PIN_base<PORT_addr, PIN_addr, DDR_addr, bit_place>
{
typedef MMIO<PORT_addr> PORTX;
typedef MMIO<PIN_addr> PINX ;
typedef MMIO<DDR_addr> DDRX ;
constexpr const static uint8_t MASK = (0x01<<bit_place);
constexpr const static uint8_t INDEX = bit_place;
};
/**
* A PIN manipulator using:
* - a port-letter { A, B, C, etc...} <or>
* - a triplet of port-addresses { & PORTx, & PINx, & DDRx }
*/
template<uint16_t ... args>
struct PIN : PIN_base<args...>
{
typedef typename PIN_base<args...>::PORTX PORTX ;
typedef typename PIN_base<args...>::PINX PINX ;
typedef typename PIN_base<args...>::DDRX DDRX ;
using PIN_base<args...>::MASK ;
using PIN_base<args...>::INDEX ;
/// Read the value of the pin
bool inline static read(){
return (PINX:: read() & MASK);
}
/// Write a value to the pin
void inline static write(bool val){
switch(val){
case true: PORTX::write(PORTX::read() | MASK); break;
case false: PORTX::write(PORTX::read() & ~MASK); break;
}
}
/**
* Toggle the value of the pin.
* Equivalent to: \code PIN = value \endcode \em OR \code PIN = !PIN \endcode
*/
void inline static toggle(){
PINX::write( MASK );
}
/// Cast to byte via \c read()
inline operator bool(){
return (PINX::read() & MASK);
}
/// Assignment via \c write()
void inline operator = (bool val){
switch(val){
case true: PORTX::write(PORTX::read() | MASK); break;
case false: PORTX::write(PORTX::read() & ~MASK); break;
}
}
/**
* Set the mode of selected pin.
* \sa \c pinMode() in \c <Arduino.h>
* @param _mode The mode to set; one of: \c INPUT, \c INPUT_PULLUP, or \c OUTPUT
*/
void inline static mode(byte _mode){
switch(_mode){
case INPUT:{
uint8_t oldSREG (SREG); cli();
PORTX::write( PORTX::read() & ~MASK );
DDRX::write( DDRX::read() & ~MASK );
SREG = oldSREG;
return;
}
case INPUT_PULLUP:{
uint8_t oldSREG (SREG); cli();
DDRX::write( DDRX::read() & ~MASK );
PORTX::write( PORTX::read() | MASK );
SREG = oldSREG;
return;
}
case OUTPUT:{
uint8_t oldSREG (SREG); cli();
DDRX::write( DDRX::read() | MASK );
SREG = oldSREG;
return;
}
}
}
};// struct PIN
/**
* A PORT manipulator using:
* - a port-letter { A, B, C, etc...} <or>
* - a triplet of port-addresses { & PORTx, & PINx, & DDRx }
*/
template<uint16_t ... args>
struct PORT_base;
/**
* Base the \c PORT definition off of specified addresses
* \param PORT_addr The address of PORTx
* \param PIN_addr The address of PINx
* \param DDR_addr The address of DDRx
*/
template<uint16_t _letter>
struct PORT_base <_letter>
{
static_assert(letter_to_PORT_addr(_letter)!=0,"Invalid pin letter: NOT_A_PORT");
static_assert(letter_to_PIN_addr(_letter)!=0,"Invalid pin letter: NOT_A_PORT");
static_assert(letter_to_DDR_addr(_letter)!=0,"Invalid pin letter: NOT_A_PORT");
typedef MMIO<letter_to_PORT_addr(_letter)> PORTX;
typedef MMIO<letter_to_PIN_addr(_letter)> PINX ;
typedef MMIO<letter_to_DDR_addr(_letter)> DDRX ;
};
/**
* Base the \c PORT definition off of specified addresses
* \param PORT_addr The address of PORTx
* \param PIN_addr The address of PINx
* \param DDR_addr The address of DDRx
*/
template<uint16_t PORT_addr, uint16_t PIN_addr, uint16_t DDR_addr>
struct PORT_base <PORT_addr, PIN_addr, DDR_addr>
{
typedef MMIO<PORT_addr> PORTX;
typedef MMIO<PIN_addr> PINX ;
typedef MMIO<DDR_addr> DDRX ;
};
/**
* A PORT manipulator using:
* - a port-letter { A, B, C, etc...} <or>
* - a triplet of port-addresses { & PORTx, & PINx, & DDRx }
*/
template<uint16_t ... args>
struct PORT : PORT_base<args...>
{
typedef typename PORT_base<args...>::PORTX PORTX;
typedef typename PORT_base<args...>::PINX PINX;
typedef typename PORT_base<args...>::DDRX DDRX;
/// Read the value of the port
Bit_Mask inline static read(){
return Bit_Mask(PINX:: read());
}
/// Write a value to the port
void inline static write(Bit_Mask val){
PORTX::write(val.value);
}
/// Write only the true bits of the mask, i.e. \code PORT |= value \endcode
void inline static set(Bit_Mask val){
PORTX::write(PORTX::read() | val.value);
}
/// Clear only the true bits of the mask, i.e. \code PORT &= ~value \endcode
void inline static clr(Bit_Mask val){
PORTX::write(PORTX::read() & ~val.value);
}
/// Toggle only the true bits of the mask, i.e. \code PIN = value \endcode
void inline static toggle(Bit_Mask val){
PINX::write( val.value );
}
/// Cast to byte by reading from PIN
inline operator byte(){
return PINX::read();
}
/// Assignment via writing to PORT
Bit_Mask inline operator = (Bit_Mask val){
return PORTX::write(val.value);
}
/**
* Set the mode(s) of selected pin(s) the PORT.
* \sa \c pinMode() in \c <Arduino.h>
* @param _mode The mode to set; one of: \c INPUT, \c INPUT_PULLUP, or \c OUTPUT
* @param val Which pins (based on the true bits) to set with that mode.
*/
void inline static mode(byte _mode,Bit_Mask val){
switch(_mode){
case INPUT:{
uint8_t oldSREG (SREG); cli();
PORTX::write( PORTX::read() & ~val.value );
DDRX::write( DDRX::read() & ~val.value );
SREG = oldSREG;
return;
}
case INPUT_PULLUP:{
uint8_t oldSREG (SREG); cli();
DDRX::write( DDRX::read() & ~val.value );
PORTX::write( PORTX::read() | val.value );
SREG = oldSREG;
return;
}
case OUTPUT:{
uint8_t oldSREG (SREG); cli();
DDRX::write( DDRX::read() | val.value );
SREG = oldSREG;
return;
}
}
}
};// struct PORT
/* ************************************************************************* *\
* Digital Read, Write, & Mode Functions
* ************************************************************************* *
*
* These functions are template-optimized versions of the same from <Arduino.h>
*
\* ************************************************************************* */
/**
* Turn off a specific PWM.
* \param timer The name of the timer used to control the PWM.
* \sa Function \c turnOffPWM() in \c <Arduino.h>
*/
template<uint8_t timer>
void turnOffPWM(){}
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
//Inlined empty function call if \c NOT_ON_TIMER amounts to a NOP
template<> void inline turnOffPWM<0>(){}
#if defined(TCCR1A) && defined(COM1A1)
template<> void inline turnOffPWM<TIMER1A>(){ cbi(TCCR1A, COM1A1); }
#endif
#if defined(TCCR1A) && defined(COM1B1)
template<> void inline turnOffPWM<TIMER1B>(){ cbi(TCCR1A, COM1B1); }
#endif
#if defined(TCCR1A) && defined(COM1C1)
template<> void inline turnOffPWM<TIMER1C>(){ cbi(TCCR1A, COM1C1); }
#endif
#if defined(TCCR2) && defined(COM21)
template<> void inline turnOffPWM<TIMER2>(){ cbi(TCCR2, COM21); }
#endif
#if defined(TCCR0A) && defined(COM0A1)
template<> void inline turnOffPWM<TIMER0A>(){ cbi(TCCR0A, COM0A1); }
#endif
#if defined(TCCR0A) && defined(COM0B1)
template<> void inline turnOffPWM<TIMER0B>(){ cbi(TCCR0A, COM0B1); }
#endif
#if defined(TCCR2A) && defined(COM2A1)
template<> void inline turnOffPWM<TIMER2A>(){ cbi(TCCR2A, COM2A1); }
#endif
#if defined(TCCR2A) && defined(COM2B1)
template<> void inline turnOffPWM<TIMER2B>(){ cbi(TCCR2A, COM2B1); }
#endif
#if defined(TCCR3A) && defined(COM3A1)
template<> void inline turnOffPWM<TIMER3A>(){ cbi(TCCR3A, COM3A1); }
#endif
#if defined(TCCR3A) && defined(COM3B1)
template<> void inline turnOffPWM<TIMER3B>(){ cbi(TCCR3A, COM3B1); }
#endif
#if defined(TCCR3A) && defined(COM3C1)
template<> void inline turnOffPWM<TIMER3C>(){ cbi(TCCR3A, COM3C1); }
#endif
#if defined(TCCR4A) && defined(COM4A1)
template<> void inline turnOffPWM<TIMER4A>(){ cbi(TCCR4A, COM4A1); }
#endif
#if defined(TCCR4A) && defined(COM4B1)
template<> void inline turnOffPWM<TIMER4B>(){ cbi(TCCR4A, COM4B1); }
#endif
#if defined(TCCR4A) && defined(COM4C1)
template<> void inline turnOffPWM<TIMER4C>(){ cbi(TCCR4A, COM4C1); }
#endif
#if defined(TCCR4C) && defined(COM4D1)
template<> void turnOffPWM<TIMER4D>(){ cbi(TCCR4C, COM4D1); }
#endif
#if defined(TCCR5A)
template<> void inline turnOffPWM<TIMER5A>(){ cbi(TCCR5A, COM5A1); }
template<> void inline turnOffPWM<TIMER5B>(){ cbi(TCCR5A, COM5B1); }
template<> void inline turnOffPWM<TIMER5C>(){ cbi(TCCR5A, COM5C1); }
#endif
#ifdef CONSTEXPR_ARDUINO_PINS
/**
* Modify the value of a specific pin.
* \param _pin The number of the pin to be modified.
* \param val The value to write to the pin.
* \sa Function \c digitalWrite() in \c <Arduino.h>
*/
template<uint8_t _pin>
void inline static digitalWrite(bool val){
turnOffPWM<pin_to_timer(_pin)>();//no function call if NOT_ON_TIMER
PIN<_pin>::write(val);
}
/**
* Read the value of a specific pin.
* \param _pin The number of the pin to be read.
* \return The value of the pin; true == HIGH , false == LOW
* \sa Function \c digitalRead() in \c <Arduino.h>
*/
template<uint8_t _pin>
bool inline static digitalRead(){
turnOffPWM<pin_to_timer(_pin)>();//no function call if NOT_ON_TIMER
return PIN<_pin>::read();
}
/**
* Set the mode of a specific pin.
* \param _pin The number of the pin to be set.
* \sa Function \c pinMode() in \c <Arduino.h>
*/
template<uint8_t _pin>
void inline static pinMode(uint8_t _mode){
PIN<_pin>::mode(_mode);
}
#endif // CONSTEXPR_ARDUINO_PINS
#endif /* FAST_DIGITAL_H_ */