-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYMBase.h
77 lines (62 loc) · 1.6 KB
/
YMBase.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
#pragma once
#include <Arduino.h>
/*
* YMBase.h
*
* Created on: 22.09.2016
* Author: tly
*/
enum YMDrum{
HIHAT = 0,
CYMBAL= 1,
TOM = 2,
SNARE = 3,
BASS = 4
};
//base class for synth chips.
template<typename YMDerived,typename Writer> struct YMBase{
uint8_t csPin, a0Pin, icPin;
Writer w;
YMBase(uint8_t csPin, uint8_t a0Pin, uint8_t icPin, Writer w = {}):csPin(csPin), a0Pin(a0Pin), icPin(icPin), w(w){}
//initializes the ic by resetting (pulling the IC pin low) and setting all registers to 0.
void init(){
w.init();
pinMode(csPin,OUTPUT);
pinMode(a0Pin,OUTPUT);
pinMode(icPin,OUTPUT);
digitalWrite(icPin,LOW);
delay(50);
digitalWrite(icPin,HIGH);
delay(50);
for(int i = 0; i < sizeof(static_cast<YMDerived*>(this)->registers); i++){
write(i,0x00);
}
}
void write(uint8_t address,uint8_t value){
w.beginWrite();
//set write mode to address
digitalWrite(a0Pin,LOW);
//write address
w.write(address);
//disable writing mode
digitalWrite(csPin,LOW);
_delay_us(YMDerived::ADDRESS_WRITE_DELAY);
digitalWrite(csPin,HIGH);
//set write mode to content
digitalWrite(a0Pin,HIGH);
//write value
w.write(value);
//disable writing mode
digitalWrite(csPin,LOW);
_delay_us(YMDerived::VALUE_WRITE_DELAY);
digitalWrite(csPin,HIGH);
static_cast<YMDerived*>(this)->registers[address] = value;
w.endWrite();
}
template<typename T> void writeRegister(T& reg){
write(
(intptr_t)(® - (intptr_t)&static_cast<YMDerived*>(this)->registerData),
*(uint8_t*)(®)
);
}
};