Skip to content

Commit 815714e

Browse files
Addition of scratch module
Some little changes
1 parent 0be18b8 commit 815714e

File tree

8 files changed

+461
-4
lines changed

8 files changed

+461
-4
lines changed

src/audio_module.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#include <cdt.h>
4242
#endif
4343

44+
45+
#if (defined SAMPLE_BUFFER_SIZE)
46+
47+
4448
#define RP2350_USE_I2S_ML_LIB
4549

4650
#ifdef ML_SYNTH_INLINE_DECLARATION
@@ -920,3 +924,4 @@ void Audio_Output(const float *left, const float *right)
920924

921925
#endif // ML_SYNTH_INLINE_DEFINITION
922926

927+
#endif /* #if (defined SAMPLE_BUFFER_SIZE) */

src/i2s_interface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#endif
4545

4646

47-
#ifdef SAMPLE_RATE
47+
#if ((defined SAMPLE_BUFFER_SIZE) && (defined SAMPLE_RATE))
4848

4949

5050
#ifdef ML_SYNTH_INLINE_DECLARATION
@@ -592,7 +592,7 @@ void setup_i2s()
592592
#endif /* ESP32 */
593593

594594

595-
#endif /* SAMPLE_RATE */
595+
#endif /* #if ((defined SAMPLE_BUFFER_SIZE) && (defined SAMPLE_RATE)) */
596596

597597

598598
#endif /* ML_SYNTH_INLINE_DEFINITION */

src/ml_as5600_inline.h

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright (c) 2024 Marcel Licence
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
18+
* der GNU General Public License, wie von der Free Software Foundation,
19+
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
20+
* veröffentlichten Version, weiter verteilen und/oder modifizieren.
21+
*
22+
* Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
23+
* OHNE JEDE GEWÄHR,; sogar ohne die implizite
24+
* Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
25+
* Siehe die GNU General Public License für weitere Einzelheiten.
26+
*
27+
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
28+
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
29+
*/
30+
31+
/**
32+
* @file ml_as5600_inline.h
33+
* @author Marcel Licence
34+
* @date 20.10.2024
35+
*
36+
* @brief Implementation to use the AS5600 for scratching via I2C
37+
* @see https://ams.com/documents/20143/36005/AS5600_DS000365_5-00.pdf
38+
* @see https://www.youtube.com/watch?v=Ml6VrlV3hvk
39+
*/
40+
41+
42+
#ifdef __CDT_PARSER__
43+
#include <cdt.h>
44+
#endif
45+
46+
47+
#ifdef AS5600_ENABLED
48+
49+
50+
#ifdef ML_SYNTH_INLINE_DECLARATION
51+
52+
53+
void AS5600_Setup(void);
54+
void AS5600_Loop(void);
55+
56+
57+
#endif /* ML_SYNTH_INLINE_DECLARATION */
58+
59+
60+
#ifdef ML_SYNTH_INLINE_DEFINITION
61+
62+
63+
#include <Wire.h>
64+
65+
66+
//#define AS5600_DEBUG_ENABLED /* use this to print out some debug messages */
67+
68+
#define AS5600_ADDR 0x36
69+
70+
#define REG_CONF 0x07
71+
#define REG_RAW_ANGLE 0x0C
72+
73+
74+
75+
static float pitchOffset = -100.0f;
76+
static int sumCh = 0;
77+
78+
79+
inline
80+
void AS5600_WriteReg(uint8_t reg, uint8_t value)
81+
{
82+
Wire.beginTransmission(AS5600_ADDR);
83+
Wire.write(reg);
84+
Wire.write(value);
85+
Wire.endTransmission();
86+
}
87+
88+
inline
89+
void AS5600_WriteRegU16(uint8_t reg, uint16_t value)
90+
{
91+
Wire.beginTransmission(AS5600_ADDR);
92+
Wire.write(reg);
93+
Wire.write((value >> 8) & 0xFF);
94+
Wire.write(value & 0xFF);
95+
Wire.endTransmission();
96+
}
97+
98+
inline
99+
uint8_t AS5600_ReadReg(uint8_t reg)
100+
{
101+
Wire.beginTransmission(AS5600_ADDR);
102+
Wire.write(reg);
103+
Wire.endTransmission();
104+
105+
Wire.requestFrom(AS5600_ADDR, 1);
106+
return Wire.read();
107+
}
108+
109+
inline
110+
uint16_t AS5600_ReadReg_u16(uint8_t reg)
111+
{
112+
Wire.beginTransmission(AS5600_ADDR);
113+
Wire.write(reg);
114+
Wire.endTransmission();
115+
116+
Wire.requestFrom(AS5600_ADDR, 2);
117+
return (((uint16_t)Wire.read()) << 8) + (uint16_t)Wire.read();
118+
}
119+
120+
void AS5600_Setup(void)
121+
{
122+
#ifdef AS5600_DEBUG_ENABLED
123+
Serial.printf("CONF: 0x%08x\n", AS5600_ReadReg_u16(REG_CONF));
124+
#endif
125+
AS5600_WriteRegU16(REG_CONF, 0x08); // hyst to 2 LSB to avoid static noise
126+
#ifdef AS5600_DEBUG_ENABLED
127+
Serial.printf("CONF: 0x%08x\n", AS5600_ReadReg_u16(REG_CONF));
128+
#endif
129+
}
130+
131+
void AS5600_Loop(void)
132+
{
133+
static uint16_t lastVal = 0;
134+
uint16_t newVal = AS5600_ReadReg_u16(REG_RAW_ANGLE);
135+
bool valUpdate = false;
136+
137+
if (newVal > lastVal)
138+
{
139+
if (newVal - lastVal > 0)
140+
{
141+
valUpdate = true;
142+
}
143+
}
144+
else
145+
{
146+
if (lastVal - newVal > 0)
147+
{
148+
valUpdate = true;
149+
}
150+
}
151+
if (valUpdate)
152+
{
153+
int ch = ((int)newVal) - ((int)lastVal);
154+
if (ch > 2048)
155+
{
156+
ch -= 4096;
157+
}
158+
if (ch < -2048)
159+
{
160+
ch += 4096;
161+
}
162+
#ifdef AS5600_DEBUG_ENABLED
163+
Serial.printf("RAW_ANGLE: %d, %d\n", newVal, ch);
164+
#endif
165+
lastVal = newVal;
166+
sumCh += ch;
167+
}
168+
}
169+
170+
float AS5600_GetPitch(uint8_t oversample)
171+
{
172+
static float lastPitch = 0;
173+
float pitch = sumCh;
174+
175+
sumCh = 0;
176+
pitch *= 0.25f;
177+
pitch /= oversample;
178+
if (pitch > 50)
179+
{
180+
pitch = 50;
181+
}
182+
if (pitch < -50)
183+
{
184+
pitch = -50;
185+
}
186+
187+
for (int i = 0; i < oversample; i++)
188+
{
189+
lastPitch = pitch * 0.05 + lastPitch * 0.95;
190+
}
191+
return max(lastPitch, pitchOffset);
192+
}
193+
194+
void AS5600_SetPitchOffset(float offset)
195+
{
196+
pitchOffset = offset;
197+
}
198+
199+
#endif /* ML_SYNTH_INLINE_DEFINITION */
200+
201+
#endif /* AS5600_ENABLED */

src/ml_inline.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <midi_stream_player.h>
5454
#include <midi_via_ble.h>
5555
#include <midi_via_usb.h>
56+
#include <ml_as5600_inline.h>
5657
#include <ml_board_setup.h>
5758
#include <ml_scope_oled_inline.h>
5859
#include <ml_status.h>

src/ml_phaser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#define SRC_ML_PHASER_H_
4545

4646

47-
#include <inttypes.h>
47+
#include <stdint.h>
4848

4949

5050
#define PHASER_AP0 150

0 commit comments

Comments
 (0)