#ifndef MY_UTIL_H_ #define MY_UTIL_H_ #ifndef F_CPU # warning "F_CPU not defined" # define F_CPU 3686400 #endif #include #include #include #include #include #define min(a, b) ({ \ __typeof__(a) __a = (a); \ __typeof__(b) __b = (b); \ __a < __b ? __a : __b; \ }) #define max(a, b) ({ \ __typeof__(a) __a = (a); \ __typeof__(b) __b = (b); \ __a > __b ? __a : __b; \ }) #define clamp(value, min_val, max_val) \ max(min(value, max_val), min_val) #define abs(val) ({ \ __typeof__(val) __val = (val); \ __val >= 0 ? __val : -__val; \ }) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) /* Progmem pointer annotation. */ #define PROGPTR /* progmem pointer */ #define mb() __asm__ __volatile__("" : : : "memory") #define noinline __attribute__((__noinline__)) #define _packed __attribute__((__packed__)) typedef _Bool bool; static inline void irq_disable(void) { cli(); mb(); } static inline void irq_enable(void) { mb(); sei(); } static inline uint8_t irq_disable_save(void) { uint8_t sreg = SREG; cli(); mb(); return sreg; } static inline void irq_restore(uint8_t sreg_flags) { mb(); SREG = sreg_flags; } static inline bool __irqs_enabled(uint8_t sreg_flags) { return !!(sreg_flags & (1 << SREG_I)); } static inline bool irqs_enabled(void) { return __irqs_enabled(SREG); } #endif /* MY_UTIL_H_ */