From cb02e386c7ff29f51cd808ddb1fc082fe2b11551 Mon Sep 17 00:00:00 2001 From: Imants Pulkstenis Date: Tue, 19 May 2020 23:27:35 +0300 Subject: [PATCH] Brightness control completed. --- ArduinoCode/matrix/matrix.ino | 31 ++++++++++--- FormMain.Designer.cs | 82 +++++++++++++++++++++++++++++++++-- FormMain.cs | 59 ++++++++++++++++++++++--- 3 files changed, 156 insertions(+), 16 deletions(-) diff --git a/ArduinoCode/matrix/matrix.ino b/ArduinoCode/matrix/matrix.ino index a0f549b..6e95446 100644 --- a/ArduinoCode/matrix/matrix.ino +++ b/ArduinoCode/matrix/matrix.ino @@ -5,7 +5,7 @@ #define COLOR_ORDER GRB #define CHIPSET WS2812B -#define BRIGHTNESS 5 +byte BRIGHTNESS = 255; // Helper functions for an two-dimensional XY matrix of pixels. // Simple 2-D demo code is included as well. @@ -165,6 +165,8 @@ uint16_t XYsafe( uint8_t x, uint8_t y) String inputString = ""; // a String to hold incoming data bool stringComplete = false; // whether the string is complete +int counter = 0; + void loop() { @@ -173,9 +175,10 @@ void loop() if (stringComplete) { - int bPosition = inputString.indexOf('b'); + int lPosition = inputString.indexOf('l'); int cPosition = inputString.indexOf('c'); - if (bPosition == 0 ){ + int bPosition = inputString.indexOf('b'); + if (lPosition == 0 ){ byte place = inputString.substring(1, 3).toInt(); byte red = inputString.substring(3, 6).toInt(); byte blue = inputString.substring(6, 9).toInt(); @@ -188,14 +191,31 @@ void loop() } else if (cPosition == 0 ) { fill_solid( leds, NUM_LEDS, CRGB(0,0,0)); Serial.print("Clear!\n"); + + } else if (bPosition == 0 ) { + counter = 0; + BRIGHTNESS = inputString.substring(1, 4).toInt(); + FastLED.setBrightness( BRIGHTNESS ); + delay(1); + Serial.print("b"); + Serial.println(BRIGHTNESS); } //delay(500); - + stringComplete = false; inputString = ""; } -} + if (counter < 5000) { + counter++; + } else { + counter = 0; + Serial.print("b"); + Serial.println(BRIGHTNESS); + //Serial.print("\n"); + } + +} void setup() { Serial.begin(9600); @@ -217,4 +237,5 @@ void serialEvent() { stringComplete = true; } } + } diff --git a/FormMain.Designer.cs b/FormMain.Designer.cs index 2e16c05..afa3936 100644 --- a/FormMain.Designer.cs +++ b/FormMain.Designer.cs @@ -113,9 +113,16 @@ this.ColorYelow = new System.Windows.Forms.Button(); this.ColorOrange = new System.Windows.Forms.Button(); this.buttonClear = new System.Windows.Forms.Button(); + this.groupBoxBrightness = new System.Windows.Forms.GroupBox(); + this.BrightnessNowTextBox = new System.Windows.Forms.RichTextBox(); + this.buttonPlus = new System.Windows.Forms.Button(); + this.buttonMinus = new System.Windows.Forms.Button(); + this.BrightnessBar = new System.Windows.Forms.TrackBar(); this.groupSettings.SuspendLayout(); this.groupBoxCOM.SuspendLayout(); this.groupBoxColor.SuspendLayout(); + this.groupBoxBrightness.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.BrightnessBar)).BeginInit(); this.SuspendLayout(); // // groupSettings @@ -185,7 +192,7 @@ this.groupBoxCOM.Controls.Add(this.COMTextBox); this.groupBoxCOM.Location = new System.Drawing.Point(32, 181); this.groupBoxCOM.Name = "groupBoxCOM"; - this.groupBoxCOM.Size = new System.Drawing.Size(354, 154); + this.groupBoxCOM.Size = new System.Drawing.Size(354, 252); this.groupBoxCOM.TabIndex = 1; this.groupBoxCOM.TabStop = false; this.groupBoxCOM.Text = "COM port monitor"; @@ -198,7 +205,7 @@ this.COMTextBox.Name = "COMTextBox"; this.COMTextBox.ReadOnly = true; this.COMTextBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical; - this.COMTextBox.Size = new System.Drawing.Size(312, 112); + this.COMTextBox.Size = new System.Drawing.Size(312, 218); this.COMTextBox.TabIndex = 0; this.COMTextBox.Text = ""; // @@ -1186,7 +1193,7 @@ // // buttonClear // - this.buttonClear.Location = new System.Drawing.Point(422, 357); + this.buttonClear.Location = new System.Drawing.Point(410, 345); this.buttonClear.Name = "buttonClear"; this.buttonClear.Size = new System.Drawing.Size(75, 23); this.buttonClear.TabIndex = 5; @@ -1194,11 +1201,70 @@ this.buttonClear.UseVisualStyleBackColor = true; this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click); // + // groupBoxBrightness + // + this.groupBoxBrightness.Controls.Add(this.BrightnessNowTextBox); + this.groupBoxBrightness.Controls.Add(this.buttonPlus); + this.groupBoxBrightness.Controls.Add(this.buttonMinus); + this.groupBoxBrightness.Controls.Add(this.BrightnessBar); + this.groupBoxBrightness.Location = new System.Drawing.Point(503, 345); + this.groupBoxBrightness.Name = "groupBoxBrightness"; + this.groupBoxBrightness.Size = new System.Drawing.Size(229, 88); + this.groupBoxBrightness.TabIndex = 67; + this.groupBoxBrightness.TabStop = false; + this.groupBoxBrightness.Text = "Brightness"; + // + // BrightnessNowTextBox + // + this.BrightnessNowTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None; + this.BrightnessNowTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.BrightnessNowTextBox.Location = new System.Drawing.Point(53, 52); + this.BrightnessNowTextBox.Multiline = false; + this.BrightnessNowTextBox.Name = "BrightnessNowTextBox"; + this.BrightnessNowTextBox.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None; + this.BrightnessNowTextBox.Size = new System.Drawing.Size(128, 22); + this.BrightnessNowTextBox.TabIndex = 4; + this.BrightnessNowTextBox.Text = "Brightness now:"; + // + // buttonPlus + // + this.buttonPlus.Location = new System.Drawing.Point(187, 52); + this.buttonPlus.Name = "buttonPlus"; + this.buttonPlus.Size = new System.Drawing.Size(22, 22); + this.buttonPlus.TabIndex = 2; + this.buttonPlus.Text = "+"; + this.buttonPlus.UseVisualStyleBackColor = true; + this.buttonPlus.Click += new System.EventHandler(this.buttonPlus_Click); + // + // buttonMinus + // + this.buttonMinus.Location = new System.Drawing.Point(25, 52); + this.buttonMinus.Name = "buttonMinus"; + this.buttonMinus.Size = new System.Drawing.Size(22, 22); + this.buttonMinus.TabIndex = 1; + this.buttonMinus.Text = "-"; + this.buttonMinus.UseVisualStyleBackColor = true; + this.buttonMinus.Click += new System.EventHandler(this.buttonMinus_Click); + // + // BrightnessBar + // + this.BrightnessBar.LargeChange = 10; + this.BrightnessBar.Location = new System.Drawing.Point(11, 19); + this.BrightnessBar.Maximum = 255; + this.BrightnessBar.Minimum = 1; + this.BrightnessBar.Name = "BrightnessBar"; + this.BrightnessBar.Size = new System.Drawing.Size(212, 45); + this.BrightnessBar.TabIndex = 0; + this.BrightnessBar.TickFrequency = 51; + this.BrightnessBar.Value = 50; + this.BrightnessBar.Scroll += new System.EventHandler(this.BrightnessBar_Scroll); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(745, 407); + this.ClientSize = new System.Drawing.Size(745, 445); + this.Controls.Add(this.groupBoxBrightness); this.Controls.Add(this.buttonClear); this.Controls.Add(this.groupBoxColor); this.Controls.Add(this.butto556); @@ -1272,6 +1338,9 @@ this.groupSettings.ResumeLayout(false); this.groupBoxCOM.ResumeLayout(false); this.groupBoxColor.ResumeLayout(false); + this.groupBoxBrightness.ResumeLayout(false); + this.groupBoxBrightness.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.BrightnessBar)).EndInit(); this.ResumeLayout(false); } @@ -1363,6 +1432,11 @@ private System.Windows.Forms.Button ColorOrange; internal System.Windows.Forms.ColorDialog colorDialog1; private System.Windows.Forms.Button buttonClear; + private System.Windows.Forms.GroupBox groupBoxBrightness; + private System.Windows.Forms.TrackBar BrightnessBar; + private System.Windows.Forms.Button buttonPlus; + private System.Windows.Forms.Button buttonMinus; + private System.Windows.Forms.RichTextBox BrightnessNowTextBox; } } diff --git a/FormMain.cs b/FormMain.cs index 6805cbe..3898b76 100644 --- a/FormMain.cs +++ b/FormMain.cs @@ -150,13 +150,13 @@ namespace MyNewApp byte[] txBuffer = new byte[64]; AddTextIn(this, COMTextBox, rxString); switch (rxString[0]) - { + { case 'b': - - //SetText(this, COMTextBoxIn, rxString); - break; - case 'v': - + string lastBrightness = rxString.Substring(1); + //BrightnessBar.Value = Int32.Parse(lastBrightness); + //SetText(this, BrightnessValueBox, lastBrightness.ToString()); + lastBrightness = "Brightness now:" + lastBrightness; + SetText(this, BrightnessNowTextBox, lastBrightness); break; case 'f': @@ -168,7 +168,34 @@ namespace MyNewApp SetText(this, TextBoxSettings, ex.Message); } } + + private void BrightnessBar_Scroll(object sender, EventArgs e) + { + updateBrightness(); + } + + private void updateBrightness() + { + + if (mySerialPort.IsOpen == false) + { + MessageBox.Show("Please connect to COM port first!"); + return; + } else { + double BrightnessNew = BrightnessBar.Value; + SetText(this, BrightnessNowTextBox, string.Format("Brightness now:{0}", BrightnessNew)); + string str = "b" + (int)BrightnessBar.Value; + //txBuffer[0] = Convert.ToByte('b'); + //txBuffer[1] = (byte)BrightnessBar.Value; + //txBuffer[2] = 0x0A; //Newline + AddTextOut(this, COMTextBox, str); + mySerialPort.Write(str + "\n"); + } + + + } + // all Color buttons: private void PrintColor(Control ctrl) { @@ -177,7 +204,7 @@ namespace MyNewApp // Get first three characters. // string sub = input.Substring(0, 3); str = str.Substring(str.Length - 2); - str = "b" + str + ctrl.BackColor.R.ToString("D3") + ctrl.BackColor.G.ToString("D3") + ctrl.BackColor.B.ToString("D3"); + str = "l" + str + ctrl.BackColor.R.ToString("D3") + ctrl.BackColor.G.ToString("D3") + ctrl.BackColor.B.ToString("D3"); if (mySerialPort.IsOpen) @@ -300,5 +327,23 @@ namespace MyNewApp SetText(this, TextBoxSettings, "First connect to COM port!"); } } + + private void buttonPlus_Click(object sender, EventArgs e) + { + if (BrightnessBar.Value < 255) + { + BrightnessBar.Value++; + } + updateBrightness(); + } + + private void buttonMinus_Click(object sender, EventArgs e) + { + if (BrightnessBar.Value > 1) + { + BrightnessBar.Value--; + } + updateBrightness(); + } } }