|
| 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 */ |
0 commit comments