From b0f007de6a8c9762026f015baade66747fb849bd Mon Sep 17 00:00:00 2001
From: Jon Bergli Heier <snakebite@jvnv.net>
Date: Sat, 29 Sep 2012 15:43:21 +0200
Subject: Moved audio initialization to the DAC class.

---
 main.cpp | 43 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/main.cpp b/main.cpp
index b335e08..254cece 100644
--- a/main.cpp
+++ b/main.cpp
@@ -82,6 +82,41 @@ auto conf_desc = configuration_desc(2, 1, 0, 0xc0, 0,
 	)
 );
 
+class DAC {
+	Pin& reset;
+	I2C& i2c;
+	uint8_t volume;
+
+	public:
+		DAC(Pin& _reset, I2C& _i2c) : reset(_reset), i2c(_i2c) {
+			reset.set_mode(Pin::Output);
+			reset.off();
+		}
+
+		void init() {
+			// Configure CS43L22.
+			reset.on();
+			
+			i2c.write_reg(0x4a, 0x02, 0x9e);
+			i2c.write_reg(0x4a, 0x06, (0 << 7) | (1 << 6) | (0 << 4) | (1 << 2) | (3 << 0));
+			set_volume(-0x40);
+			//i2c.write_reg(0x4a, 0x1a, 0);
+			//i2c.write_reg(0x4a, 0x1b, 0);
+			//i2c.write_reg(0x4a, 0x1a, -0x40 & 0x7f);
+			//i2c.write_reg(0x4a, 0x1b, -0x40 & 0x7f);
+		}
+		uint8_t get_volume() {
+			return volume;
+		}
+		void set_volume(uint8_t v) {
+			volume = v;
+			i2c.write_reg(0x4a, 0x1a, volume & 0x7f);
+			i2c.write_reg(0x4a, 0x1b, volume & 0x7f);
+		}
+};
+
+DAC dac(dac_nreset, I2C1);
+
 desc_t dev_desc_p = {sizeof(dev_desc), (void*)&dev_desc};
 desc_t conf_desc_p = {sizeof(conf_desc), (void*)&conf_desc};
 
@@ -163,7 +198,6 @@ class USB_Audio : public USB_class_driver {
 
 USB_Audio usb_audio(usb);
 
-
 int main() {
 	// Initialize system timer.
 	STK.LOAD = 168000000 / 8 / 1000; // 1000 Hz.
@@ -206,12 +240,7 @@ int main() {
 	// Initialize I2C.
 	I2C1.enable(i2c_scl, i2c_sda);
 	
-	// Configure CS43L22.
-	dac_nreset.set_mode(Pin::Output);
-	dac_nreset.on();
-	
-	I2C1.write_reg(0x4a, 0x02, 0x9e);
-	I2C1.write_reg(0x4a, 0x06, (0 << 7) | (1 << 6) | (0 << 4) | (1 << 2) | (3 << 0));
+	dac.init();
 	
 	// Initialize USB.
 	usb_vbus.set_mode(Pin::Input);
-- 
cgit v1.2.3