![](/media/cache/profiles/80729472_p0.jpg.50x50_q85.jpg)
qweqwe
Dependencies: Si5351A NeoPixel
main.cpp@3:05c2c10b6bb3, 2021-06-16 (annotated)
- Committer:
- fezine
- Date:
- Wed Jun 16 17:12:18 2021 +0000
- Revision:
- 3:05c2c10b6bb3
- Parent:
- 2:2c78f22e6081
as
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tichise | 0:124662fe9af9 | 1 | #include "mbed.h" |
tichise | 0:124662fe9af9 | 2 | #include "neopixel.h" |
fezine | 1:34ec04b8a8dd | 3 | #include "si5351a.h" |
fezine | 1:34ec04b8a8dd | 4 | |
fezine | 3:05c2c10b6bb3 | 5 | I2C i2c(D0, D1); //通信Si5351A |
fezine | 3:05c2c10b6bb3 | 6 | SI5351A clk(i2c, 25000000UL); //基础时钟25MHz |
fezine | 3:05c2c10b6bb3 | 7 | Pixel Column[20]; //单个数组 |
fezine | 3:05c2c10b6bb3 | 8 | Pixel Column_All[280]; //全局数组 |
fezine | 3:05c2c10b6bb3 | 9 | NeoPixelOut neoPixel(D9); //数据输出 |
fezine | 3:05c2c10b6bb3 | 10 | Timeout PeakDecrease; |
fezine | 1:34ec04b8a8dd | 11 | |
fezine | 3:05c2c10b6bb3 | 12 | DigitalOut reset=D4; //重置MSGEQ7 |
fezine | 3:05c2c10b6bb3 | 13 | DigitalOut strobe=D3; //选择MSGEQ7输出 |
fezine | 3:05c2c10b6bb3 | 14 | AnalogIn analog1_7=A0; //数据读取1-7 |
fezine | 3:05c2c10b6bb3 | 15 | AnalogIn analog8_14=A1; //数据读取8-14 |
fezine | 3:05c2c10b6bb3 | 16 | //AnalogIn mod=A2; //选择模式输入 |
fezine | 3:05c2c10b6bb3 | 17 | AnalogIn sensitive=A5; |
fezine | 3:05c2c10b6bb3 | 18 | AnalogIn brightness=A6; |
fezine | 1:34ec04b8a8dd | 19 | |
tichise | 0:124662fe9af9 | 20 | |
fezine | 3:05c2c10b6bb3 | 21 | int NumColumn=14; //灯柱数量 |
fezine | 3:05c2c10b6bb3 | 22 | int Peak[14]={0}; |
fezine | 3:05c2c10b6bb3 | 23 | int PeakDecreaseTime_us=50*1e3; //peak下降延迟时间 |
fezine | 3:05c2c10b6bb3 | 24 | int colorPeak; //peak颜色 |
fezine | 3:05c2c10b6bb3 | 25 | int __mode; |
fezine | 3:05c2c10b6bb3 | 26 | float freqData[14]; |
fezine | 3:05c2c10b6bb3 | 27 | float noise=0.1; |
tichise | 0:124662fe9af9 | 28 | |
fezine | 3:05c2c10b6bb3 | 29 | float mode=0.1; |
fezine | 3:05c2c10b6bb3 | 30 | int num=0; |
fezine | 3:05c2c10b6bb3 | 31 | |
fezine | 3:05c2c10b6bb3 | 32 | |
fezine | 3:05c2c10b6bb3 | 33 | //-------------------------------设置Peak降落的互钳函数 |
fezine | 1:34ec04b8a8dd | 34 | void toggleOff(void); |
fezine | 1:34ec04b8a8dd | 35 | void toggleOn(void){ |
fezine | 1:34ec04b8a8dd | 36 | for(int i=0;i<NumColumn;i++){ |
fezine | 1:34ec04b8a8dd | 37 | if(Peak[i]>-2) Peak[i]--; |
fezine | 1:34ec04b8a8dd | 38 | } |
fezine | 3:05c2c10b6bb3 | 39 | num++; |
fezine | 3:05c2c10b6bb3 | 40 | if(num==200){ |
fezine | 3:05c2c10b6bb3 | 41 | if(mode<=0.5) mode=mode+0.1; |
fezine | 3:05c2c10b6bb3 | 42 | else mode=0.1; |
fezine | 3:05c2c10b6bb3 | 43 | num=0; |
fezine | 3:05c2c10b6bb3 | 44 | } |
fezine | 1:34ec04b8a8dd | 45 | PeakDecrease.attach_us(toggleOff,PeakDecreaseTime_us); |
fezine | 1:34ec04b8a8dd | 46 | } |
fezine | 1:34ec04b8a8dd | 47 | void toggleOff(void){ |
fezine | 1:34ec04b8a8dd | 48 | for(int i=0;i<NumColumn;i++){ |
fezine | 1:34ec04b8a8dd | 49 | if(Peak[i]>-2) Peak[i]--; |
fezine | 1:34ec04b8a8dd | 50 | } |
fezine | 1:34ec04b8a8dd | 51 | PeakDecrease.attach_us(toggleOn,PeakDecreaseTime_us); |
fezine | 1:34ec04b8a8dd | 52 | } |
fezine | 1:34ec04b8a8dd | 53 | //-----------------------------------------取最大值函数 |
fezine | 1:34ec04b8a8dd | 54 | int max(int i,int j){ |
fezine | 1:34ec04b8a8dd | 55 | if(i>j)return i; |
fezine | 1:34ec04b8a8dd | 56 | else return j; |
fezine | 1:34ec04b8a8dd | 57 | } |
fezine | 3:05c2c10b6bb3 | 58 | //---------------------------------------------读取MSGEQ7的返回值 |
fezine | 1:34ec04b8a8dd | 59 | void Read() { |
fezine | 1:34ec04b8a8dd | 60 | reset = 1; |
fezine | 3:05c2c10b6bb3 | 61 | //wait_ns(150); //最小值100ns |
fezine | 1:34ec04b8a8dd | 62 | reset = 0; |
fezine | 3:05c2c10b6bb3 | 63 | wait_us(200); |
fezine | 1:34ec04b8a8dd | 64 | for (int i = 0; i < 7; i++){ |
fezine | 1:34ec04b8a8dd | 65 | strobe = 0; |
fezine | 3:05c2c10b6bb3 | 66 | wait_us(50); //待输出稳定 |
fezine | 3:05c2c10b6bb3 | 67 | freqData[i*2]=analog1_7*(0.5+2*sensitive); |
fezine | 3:05c2c10b6bb3 | 68 | freqData[i*2+1]=analog8_14*(0.5+2*sensitive); |
fezine | 1:34ec04b8a8dd | 69 | strobe = 1; |
fezine | 3:05c2c10b6bb3 | 70 | wait_us(40); //strobe最小间隔72us,此处取90us |
fezine | 3:05c2c10b6bb3 | 71 | } |
fezine | 3:05c2c10b6bb3 | 72 | } |
fezine | 3:05c2c10b6bb3 | 73 | //-------------------------------------------初始颜色 |
fezine | 3:05c2c10b6bb3 | 74 | int colorBegin(){ |
fezine | 3:05c2c10b6bb3 | 75 | int __mode=(int)(mode*10); //__mode范围为1,2,3,4,5,6,7,8,9 |
fezine | 3:05c2c10b6bb3 | 76 | switch(__mode){ |
fezine | 3:05c2c10b6bb3 | 77 | case 1: |
fezine | 3:05c2c10b6bb3 | 78 | colorPeak=0xFF0000; |
fezine | 3:05c2c10b6bb3 | 79 | return 0x00FF00;// |
fezine | 3:05c2c10b6bb3 | 80 | case 2: |
fezine | 3:05c2c10b6bb3 | 81 | colorPeak=0xFF0000; |
fezine | 3:05c2c10b6bb3 | 82 | return 0x0000FF;// |
fezine | 3:05c2c10b6bb3 | 83 | case 3: |
fezine | 3:05c2c10b6bb3 | 84 | colorPeak=0xFFFF00; |
fezine | 3:05c2c10b6bb3 | 85 | return 0xFF33CC;// |
fezine | 3:05c2c10b6bb3 | 86 | case 4: |
fezine | 3:05c2c10b6bb3 | 87 | colorPeak=0x990099; |
fezine | 3:05c2c10b6bb3 | 88 | return 0xFF3366;// |
fezine | 3:05c2c10b6bb3 | 89 | case 5: |
fezine | 3:05c2c10b6bb3 | 90 | colorPeak=0x0000FF; |
fezine | 3:05c2c10b6bb3 | 91 | return 0x000000;// |
fezine | 3:05c2c10b6bb3 | 92 | case 6: |
fezine | 3:05c2c10b6bb3 | 93 | colorPeak=0xFF0000; |
fezine | 3:05c2c10b6bb3 | 94 | return 0x00FF00; |
fezine | 3:05c2c10b6bb3 | 95 | case 7: |
fezine | 3:05c2c10b6bb3 | 96 | colorPeak=0xFF0000; |
fezine | 3:05c2c10b6bb3 | 97 | return 0x00FF00; |
fezine | 3:05c2c10b6bb3 | 98 | case 8: |
fezine | 3:05c2c10b6bb3 | 99 | colorPeak=0xFF0000; |
fezine | 3:05c2c10b6bb3 | 100 | return 0x00FF00; |
fezine | 3:05c2c10b6bb3 | 101 | case 9: |
fezine | 3:05c2c10b6bb3 | 102 | colorPeak=0xFF0000; |
fezine | 3:05c2c10b6bb3 | 103 | return 0x00FF00; |
fezine | 3:05c2c10b6bb3 | 104 | default: |
fezine | 3:05c2c10b6bb3 | 105 | colorPeak=0xFF0000; |
fezine | 3:05c2c10b6bb3 | 106 | return 0x00FF00; |
fezine | 3:05c2c10b6bb3 | 107 | |
fezine | 1:34ec04b8a8dd | 108 | } |
fezine | 1:34ec04b8a8dd | 109 | } |
fezine | 3:05c2c10b6bb3 | 110 | //------------------------------------------设置模式,每个数字对应一种颜色变换方式 |
fezine | 3:05c2c10b6bb3 | 111 | int colorTransformation(int __color,int i){ |
fezine | 3:05c2c10b6bb3 | 112 | switch(__mode){ |
fezine | 3:05c2c10b6bb3 | 113 | case 1: |
fezine | 3:05c2c10b6bb3 | 114 | return __color+0x060009-0x000B00;//绿色 |
fezine | 3:05c2c10b6bb3 | 115 | case 2: |
fezine | 3:05c2c10b6bb3 | 116 | return __color+0x0B0000-0x00000C;//蓝色 |
fezine | 3:05c2c10b6bb3 | 117 | case 3: |
fezine | 3:05c2c10b6bb3 | 118 | switch(i){ |
fezine | 3:05c2c10b6bb3 | 119 | case 0:return 0xFF33CC; case 1:return 0xFF33FF; case 2:return 0xCC33FF; case 3:return 0x9933FF; case 4:return 0x6633FF; |
fezine | 3:05c2c10b6bb3 | 120 | case 5:return 0x3333FF; case 6:return 0x3366FF; case 7:return 0x3399FF; case 8:return 0x33CCFF; case 9:return 0x33FFFF; |
fezine | 3:05c2c10b6bb3 | 121 | case 10:return 0x33FFCC; case 11:return 0x33FF99; case 12:return 0x33FF66; case 13:return 0x33FF33; case 14:return 0x66FF33; |
fezine | 3:05c2c10b6bb3 | 122 | case 15:return 0x99FF33; case 16:return 0xCCFF33; case 17:return 0xFFFF33; case 18:return 0xFFCC33; case 19:return 0xFF9933; |
fezine | 3:05c2c10b6bb3 | 123 | default: return 0x000000; |
fezine | 3:05c2c10b6bb3 | 124 | }//粉黄 |
fezine | 3:05c2c10b6bb3 | 125 | case 4: |
fezine | 3:05c2c10b6bb3 | 126 | switch(i){ |
fezine | 3:05c2c10b6bb3 | 127 | case 0:return 0xFF3366; case 1:return 0xFF33CC; case 2:return 0xCC33FF; case 3:return 0x6633FF; case 4:return 0x3366FF; |
fezine | 3:05c2c10b6bb3 | 128 | case 5:return 0x33CCFF; case 6:return 0x33FFCC; case 7:return 0x33FF66; case 8:return 0x66FF33; case 9:return 0xCCFF33; |
fezine | 3:05c2c10b6bb3 | 129 | case 10:return 0xFFCC33; case 11:return 0xFF6633; case 12:return 0xFF33CC; case 13:return 0xFF33FF; case 14:return 0x9933FF; |
fezine | 3:05c2c10b6bb3 | 130 | case 15:return 0x3333FF; case 16:return 0x3399FF; case 17:return 0x33FFFF; case 18:return 0x33FF99; case 19:return 0x33FF33; |
fezine | 3:05c2c10b6bb3 | 131 | default: return 0x000000; |
fezine | 3:05c2c10b6bb3 | 132 | }// |
fezine | 3:05c2c10b6bb3 | 133 | case 5: |
fezine | 3:05c2c10b6bb3 | 134 | return 0X000000;//红色无 |
fezine | 3:05c2c10b6bb3 | 135 | case 6: |
fezine | 3:05c2c10b6bb3 | 136 | return __color; |
fezine | 3:05c2c10b6bb3 | 137 | break; |
fezine | 3:05c2c10b6bb3 | 138 | case 7: |
fezine | 3:05c2c10b6bb3 | 139 | return __color; |
fezine | 3:05c2c10b6bb3 | 140 | break; |
fezine | 3:05c2c10b6bb3 | 141 | case 8: |
fezine | 3:05c2c10b6bb3 | 142 | return __color; |
fezine | 3:05c2c10b6bb3 | 143 | break; |
fezine | 3:05c2c10b6bb3 | 144 | case 9: |
fezine | 3:05c2c10b6bb3 | 145 | return __color; |
fezine | 3:05c2c10b6bb3 | 146 | default: |
fezine | 3:05c2c10b6bb3 | 147 | return __color+0x06000C; |
fezine | 3:05c2c10b6bb3 | 148 | |
fezine | 3:05c2c10b6bb3 | 149 | } |
fezine | 3:05c2c10b6bb3 | 150 | } |
fezine | 3:05c2c10b6bb3 | 151 | //------------------------------------------确定单个column的颜色 |
fezine | 3:05c2c10b6bb3 | 152 | void run(int j){ |
fezine | 3:05c2c10b6bb3 | 153 | __mode=mode*10; |
fezine | 3:05c2c10b6bb3 | 154 | int __color=colorBegin(); |
fezine | 3:05c2c10b6bb3 | 155 | int H=((freqData[j]-noise)*19); |
fezine | 3:05c2c10b6bb3 | 156 | for(int i=0; i<20; i++) { |
fezine | 3:05c2c10b6bb3 | 157 | if(i>H) Column[i].hex=0; |
fezine | 3:05c2c10b6bb3 | 158 | else Column[i].hex=__color; |
fezine | 3:05c2c10b6bb3 | 159 | __color=colorTransformation(__color,i); |
fezine | 3:05c2c10b6bb3 | 160 | } |
fezine | 3:05c2c10b6bb3 | 161 | Column[max(Peak[j],H)].hex=colorPeak; |
fezine | 3:05c2c10b6bb3 | 162 | if(Peak[j]<H) Peak[j]=H;//当Peak小于H时,上拉Peak到最顶端 |
fezine | 3:05c2c10b6bb3 | 163 | } |
fezine | 1:34ec04b8a8dd | 164 | //-------------------------------------------main |
fezine | 1:34ec04b8a8dd | 165 | int main() |
fezine | 1:34ec04b8a8dd | 166 | { |
fezine | 1:34ec04b8a8dd | 167 | strobe=1; |
fezine | 3:05c2c10b6bb3 | 168 | reset=0; //initialize |
fezine | 1:34ec04b8a8dd | 169 | clk.set_frequency(SI5351_CLK1, 104570); |
fezine | 3:05c2c10b6bb3 | 170 | clk.set_frequency(SI5351_CLK0, 190000); |
fezine | 2:2c78f22e6081 | 171 | wait_us(200*1000); // 等待HSE稳定 |
fezine | 1:34ec04b8a8dd | 172 | neoPixel.normalize = false; // 使r+g+b=255 均匀每个led亮度 |
fezine | 1:34ec04b8a8dd | 173 | //Peakdecrease.detach(); |
fezine | 1:34ec04b8a8dd | 174 | toggleOn();//打开PeakDecrease |
fezine | 1:34ec04b8a8dd | 175 | while(1) { |
fezine | 1:34ec04b8a8dd | 176 | Read(); |
fezine | 3:05c2c10b6bb3 | 177 | neoPixel.global_scale = brightness; //亮度调节 |
fezine | 3:05c2c10b6bb3 | 178 | //for(int x=0;x<14;x++) printf("%4.d ",(int)(freqData[x]*100));//test |
fezine | 3:05c2c10b6bb3 | 179 | //printf("\n");//test |
fezine | 1:34ec04b8a8dd | 180 | for(int j=0;j<NumColumn;j++){ |
fezine | 1:34ec04b8a8dd | 181 | run(j); |
fezine | 1:34ec04b8a8dd | 182 | for(int k=20*j;k<20*(j+1);k++){ |
fezine | 1:34ec04b8a8dd | 183 | Column_All[k].hex=Column[k%20].hex; |
fezine | 1:34ec04b8a8dd | 184 | } |
fezine | 1:34ec04b8a8dd | 185 | } |
fezine | 1:34ec04b8a8dd | 186 | neoPixel.send(Column_All, NumColumn*20); |
fezine | 1:34ec04b8a8dd | 187 | } |
tichise | 0:124662fe9af9 | 188 | } |