Weisse Kat / Mbed 2 deprecated headphone-amp-selector

Dependencies:   mbed-STM32F103C8T6 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stm32f103c8t6.h"
00002 #include "main.h"
00003 #include "math.h"
00004 #include "mbed.h"
00005 
00006 Ticker dynamic_indication_timer;
00007 Ticker encoder_timer;
00008 
00009 InterruptIn encoder_button(PB_12);
00010 
00011 DigitalIn encoder_left(PB_13);
00012 DigitalIn encoder_right(PB_14);
00013 
00014 int encoder_state = 0;
00015 int encoder_data = 50;
00016 
00017 
00018 BusOut segments(segment_A, segment_B, segment_C, segment_D, segment_E, segment_F, segment_G, segment_DOT);
00019 BusOut common(common_1, common_2);
00020 
00021 I2C i2c(I2C_SDA, I2C_SCL);
00022 
00023 uint8_t digits[] = {192, 207, 162, 134, 141, 148, 144, 199, 128, 132};
00024 
00025 uint8_t inputs[] = {0x03, 0x02};
00026 uint8_t input = 0;
00027 uint8_t volume = 99;
00028 
00029 uint8_t address = 0x88;
00030 
00031 /*
00032 I2C>[0x88 0x00 0x03] - enable edge input
00033 I2C>[0x88 0x00 0x03] - enable middle input
00034 
00035 I2C>[0x88 0x01 0x00] - input gain 0db
00036 
00037 I2C>[0x88 0x02 0x6f] - lowest volume
00038 I2C>[0x88 0x02 0x78] - mute
00039 I2C>[0x88 0x02 0x00] - highest volume
00040 
00041 I2C>[0x88 0x03 0x0f] - bass 0db
00042 I2C>[0x88 0x04 0x0f] - treble 0db
00043 
00044 I2C>[0x88 0x06 0x00] - speaker L att 0db
00045 I2C>[0x88 0x07 0x00] - speaker R att 0db
00046 
00047 
00048 */
00049 
00050 void encoder_button_pressed()
00051 {
00052     input = ((input == 0) ? 1 : 0);
00053 }
00054 
00055 void encoder_timer_task()
00056 {
00057     uint8_t new_state = !encoder_left * 2 + !encoder_right;
00058 
00059     switch(encoder_state) {
00060         case 2: {
00061             if((new_state == 3) && (encoder_data < 99)) encoder_data++;
00062             if((new_state == 0) && (encoder_data > 0)) encoder_data--;
00063             break;
00064         }
00065 
00066         case 0: {
00067             if((new_state == 2) && (encoder_data < 99)) encoder_data++;
00068             if((new_state == 1) && (encoder_data > 0)) encoder_data--;
00069             break;
00070         }
00071         case 1: {
00072             if((new_state == 0) && (encoder_data < 99)) encoder_data++;
00073             if((new_state == 3) && (encoder_data > 0)) encoder_data--;
00074             break;
00075         }
00076         case 3: {
00077             if((new_state == 1) && (encoder_data < 99)) encoder_data++;
00078             if((new_state == 2) && (encoder_data > 0)) encoder_data--;
00079             break;
00080         }
00081     }
00082 
00083     encoder_state = new_state;
00084 }
00085 
00086 
00087 void dynamic_indication_swapper()
00088 {
00089     if (common != 0b01) { // lower is active
00090         common = 0b01;
00091         segments = digits[volume / 10] - ((input == 0) ? 128 : 0);
00092     } else {
00093         common = 0b10;
00094         segments = digits[volume % 10] - ((input == 1) ? 128 : 0);
00095     }
00096 }
00097 
00098 int main()
00099 {
00100     encoder_button.fall(&encoder_button_pressed);
00101     encoder_button.mode(PullUp);
00102 
00103     common = 0b10;
00104 
00105     dynamic_indication_timer.attach(&dynamic_indication_swapper, 0.01);
00106 
00107 
00108     encoder_left.mode(PullUp);
00109     encoder_right.mode(PullUp);
00110     encoder_timer.attach(&encoder_timer_task, 0.01);
00111 
00112     char cmd[2];
00113 
00114     cmd[0] = 0x00;
00115     cmd[1] = inputs[input];
00116     i2c.write(address, cmd, 2);
00117 
00118     cmd[0] = 0x01;
00119     cmd[1] = 0x00;
00120     i2c.write(address, cmd, 2);
00121 
00122     cmd[0] = 0x02;
00123     cmd[1] = 0x00;
00124     i2c.write(address, cmd, 2);
00125 
00126     cmd[0] = 0x03;
00127     cmd[1] = 0x0f;
00128     i2c.write(address, cmd, 2);
00129 
00130     cmd[0] = 0x04;
00131     cmd[1] = 0x0f;
00132     i2c.write(address, cmd, 2);
00133 
00134     cmd[0] = 0x06;
00135     cmd[1] = 0x00;
00136     i2c.write(address, cmd, 2);
00137 
00138     cmd[0] = 0x07;
00139     cmd[1] = 0x00;
00140     i2c.write(address, cmd, 2);
00141 
00142     while (1) {
00143         volume = encoder_data;
00144 
00145         cmd[0] = 0x00;
00146         cmd[1] = inputs[input];
00147         i2c.write(address, cmd, 2);
00148 
00149         cmd[0] = 0x02;
00150         cmd[1] = (volume > 0) ? (0x2f - floor(0.47 * (volume + 1))) : 0x3f;
00151         i2c.write(address, cmd, 2);
00152 
00153         wait_ms(100);
00154     };
00155 }