-
Notifications
You must be signed in to change notification settings - Fork 0
/
grains.h
140 lines (110 loc) · 2.74 KB
/
grains.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
/**
* @file grains.h
* @author Alex Hiam - <alex@graycat.io>
* @copyright Copyright (c) 2018 - Gray Cat Labs, Alex Hiam <alex@graycat.io>
* @license Released under the MIT License
*
* @brief A basic C library for the Ginko Synthese Grains eurorack module.
* Grains: https://www.ginkosynthese.com/product/grains/
*/
#ifndef _GRAINS_H
#define _GRAINS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
// I/O defines:
#define KNOB_1 2
#define KNOB_2 1
#define KNOB_3 0
#define CV_IN 3
#define PWM_PIN 11
#define PWM_OCR (OCR2A)
#define PWM_INTERRUPT (TIMER2_OVF_vect)
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
#define SAMPLE_AVG_LEN 8 ///< Moving average length for analog input sampling:
/**
* Struct for use with #sampleAveraged function.
*/
typedef struct {
uint16_t window[SAMPLE_AVG_LEN];
uint32_t sum;
uint8_t i;
} avg_t;
inline void setPwm(uint8_t value) {
PWM_OCR = value;
}
extern const uint16_t freqTable[];
extern const uint16_t expTable[];
extern const uint16_t logTable[];
extern const uint16_t semitoneTable[];
extern const uint16_t majorTable[];
extern const uint16_t minorTable[];
extern const uint16_t mixolydianTable[];
/**
* Convert CV ADC value to frequency
*/
inline uint16_t mapFreq(uint16_t input) {
return pgm_read_word_near(freqTable + input);
}
/**
* Convert linear ADC input to exponential
*
* (Finer control on ends)
*/
inline uint16_t mapExp(uint16_t input) {
return pgm_read_word_near(expTable + input);
}
/**
* Convert linear ADC input to logarithmic
*
* (Finer control in the middle)
*/
inline uint16_t mapLog(uint16_t input) {
return pgm_read_word_near(logTable + input);
}
inline uint16_t mapSemitone(uint16_t input) {
return pgm_read_word_near(semitoneTable + input);
}
inline uint16_t mapMajor(uint16_t input) {
return pgm_read_word_near(majorTable + input);
}
inline uint16_t mapMinor(uint16_t input) {
return pgm_read_word_near(majorTable + input);
}
inline uint16_t mapMixolydian(uint16_t input) {
return pgm_read_word_near(majorTable + input);
}
/**
* Enalbe the PWM output
*/
void audioOn(void);
/**
* Get the current state of a clock signal on IN1
*/
uint8_t getClock(void);
/**
* Used with #getTrigger to spedify which edge to look for
*/
typedef enum {
EDGE_FALLING=0,
EDGE_RISING
} edge_t;
/**
* Returns 1 if a trigger has been received since the last time
* called, 0 otherwise.
*/
uint8_t getTrigger(edge_t edge);
/**
* Sample the given ADC input and return the output of a moving
* average. (Each input needs its own static avg_t struct)
*/
uint16_t sampleAveraged(uint8_t input, avg_t *avg);
#ifdef __cplusplus
} // extern "C"
#endif
#endif //_GRAINS_H