YMZ294 Player. modified from "Yamaguchi's YMZ294 Library" for LPC1114.

Dependencies:   mbed

Committer:
kohacraft
Date:
Wed Dec 09 01:38:08 2015 +0000
Revision:
0:7a56bf0441ea
YMZ294 Player. modified from "Yamaguchi's YMZ294 Library" for LPC1114

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kohacraft 0:7a56bf0441ea 1 #include "YMZ294.h"
kohacraft 0:7a56bf0441ea 2
kohacraft 0:7a56bf0441ea 3 const int ADDR_FREQ[] = {0, 2, 4};
kohacraft 0:7a56bf0441ea 4 const int ADDR_NOISE = 6;
kohacraft 0:7a56bf0441ea 5 const int ADDR_MIXER = 7;
kohacraft 0:7a56bf0441ea 6 const int ADDR_VOL[] = {8, 9, 10};
kohacraft 0:7a56bf0441ea 7 const int ADDR_ENV_FREQ = 11;
kohacraft 0:7a56bf0441ea 8 const int ADDR_ENV_SHAPE = 13;
kohacraft 0:7a56bf0441ea 9
kohacraft 0:7a56bf0441ea 10 YMZ294::YMZ294(PinName dataPin0, PinName dataPin1, PinName dataPin2, PinName dataPin3,
kohacraft 0:7a56bf0441ea 11 PinName dataPin4, PinName dataPin5, PinName dataPin6, PinName dataPin7,
kohacraft 0:7a56bf0441ea 12 PinName csPin, PinName wrPin, PinName a0Pin) :
kohacraft 0:7a56bf0441ea 13 dataPins(dataPin0, dataPin1, dataPin2, dataPin3, dataPin4, dataPin5, dataPin6, dataPin7),
kohacraft 0:7a56bf0441ea 14 csPin(csPin), wrPin(wrPin), a0Pin(a0Pin) {}
kohacraft 0:7a56bf0441ea 15
kohacraft 0:7a56bf0441ea 16 void YMZ294::setTone(Ch channel, float freq) {
kohacraft 0:7a56bf0441ea 17 if (freqs[channel] != freq) {
kohacraft 0:7a56bf0441ea 18 int tp = 125000.0 / freq; // tp <= 4095 or freq > 30.525
kohacraft 0:7a56bf0441ea 19 writeData(ADDR_FREQ[channel], tp, 2);
kohacraft 0:7a56bf0441ea 20 freqs[channel] = freq;
kohacraft 0:7a56bf0441ea 21 }
kohacraft 0:7a56bf0441ea 22 }
kohacraft 0:7a56bf0441ea 23
kohacraft 0:7a56bf0441ea 24 void YMZ294::setVolume(Ch channel, int volume) {
kohacraft 0:7a56bf0441ea 25 if (volumes[channel] != volume || volume == 16) {
kohacraft 0:7a56bf0441ea 26 writeData(ADDR_VOL[channel], volume & 0x1F);
kohacraft 0:7a56bf0441ea 27 volumes[channel] = volume;
kohacraft 0:7a56bf0441ea 28 }
kohacraft 0:7a56bf0441ea 29 }
kohacraft 0:7a56bf0441ea 30
kohacraft 0:7a56bf0441ea 31 void YMZ294::setNoise(int freq) {
kohacraft 0:7a56bf0441ea 32 if (noiseFreq != freq) {
kohacraft 0:7a56bf0441ea 33 writeData(ADDR_NOISE, freq & 0x1F);
kohacraft 0:7a56bf0441ea 34 noiseFreq = freq;
kohacraft 0:7a56bf0441ea 35 }
kohacraft 0:7a56bf0441ea 36 }
kohacraft 0:7a56bf0441ea 37
kohacraft 0:7a56bf0441ea 38 void YMZ294::setEnvelope(int freq, int shape) {
kohacraft 0:7a56bf0441ea 39 writeData(ADDR_ENV_FREQ, freq, 2);
kohacraft 0:7a56bf0441ea 40 writeData(ADDR_ENV_SHAPE, shape);
kohacraft 0:7a56bf0441ea 41 }
kohacraft 0:7a56bf0441ea 42
kohacraft 0:7a56bf0441ea 43 void YMZ294::setMixer(Mixer m0, Mixer m1, Mixer m2, Mixer m3, Mixer m4, Mixer m5) {
kohacraft 0:7a56bf0441ea 44 writeData(ADDR_MIXER, m0 & m1 & m2 & m3 & m4 & m5);
kohacraft 0:7a56bf0441ea 45 }
kohacraft 0:7a56bf0441ea 46
kohacraft 0:7a56bf0441ea 47 void YMZ294::write(char data, bool dataWrite) {
kohacraft 0:7a56bf0441ea 48 wrPin = 0;
kohacraft 0:7a56bf0441ea 49 csPin = 0;
kohacraft 0:7a56bf0441ea 50 a0Pin = dataWrite;
kohacraft 0:7a56bf0441ea 51 dataPins = data;
kohacraft 0:7a56bf0441ea 52 wrPin = 1;
kohacraft 0:7a56bf0441ea 53 csPin = 1;
kohacraft 0:7a56bf0441ea 54 }
kohacraft 0:7a56bf0441ea 55
kohacraft 0:7a56bf0441ea 56 void YMZ294::writeData(char address, int data, int nBytes) {
kohacraft 0:7a56bf0441ea 57 write(address);
kohacraft 0:7a56bf0441ea 58 write(data & 0xFF, true);
kohacraft 0:7a56bf0441ea 59 while (--nBytes) {
kohacraft 0:7a56bf0441ea 60 write(++address);
kohacraft 0:7a56bf0441ea 61 write((data >>= 8) & 0xFF, true);
kohacraft 0:7a56bf0441ea 62 }
kohacraft 0:7a56bf0441ea 63 }