Program.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using WiringPi;
  5. namespace WiringPiBrightnessByPwmAndButtonHold
  6. {
  7. internal class Program
  8. {
  9. private static void Main(string[] args)
  10. {
  11. var ci = File.ReadAllLines("/proc/cpuinfo");
  12. foreach (var s in ci) Console.WriteLine(s);
  13. int ret = Init.WiringPiSetupGpio();
  14. if (ret == -1)
  15. {
  16. Console.WriteLine("Init failed: {0}", ret);
  17. return;
  18. }
  19. const int buttonRightPin = 17;
  20. const int pwmPin = 18;
  21. const int buttonLeftPin = 27;
  22. const uint maxRange = 1000u;
  23. GPIO.pinMode(buttonLeftPin, (int)GPIO.GPIOpinmode.Input);
  24. GPIO.pinMode(buttonRightPin, (int)GPIO.GPIOpinmode.Input);
  25. GPIO.pinMode(pwmPin, (int)GPIO.GPIOpinmode.PWMOutput);
  26. GPIO.pwmSetClock(2);
  27. GPIO.pwmSetRange(maxRange);
  28. var v = 50;
  29. while (true)
  30. {
  31. var l = GPIO.digitalRead(buttonLeftPin);
  32. var r = GPIO.digitalRead(buttonRightPin);
  33. var step = 1;
  34. if (v > 50 && v <= 250) step = 5;
  35. if (v > 250) step = 25;
  36. if (v > 0 && l == 1)
  37. {
  38. if (v > 300) v -= step; else v -= step;
  39. GPIO.pwmWrite(pwmPin, v);
  40. }
  41. if (v < maxRange && r == 1)
  42. {
  43. if (v > 250) v += step; else v += step;
  44. GPIO.pwmWrite(pwmPin, v);
  45. }
  46. Console.WriteLine(
  47. $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}"
  48. + $" V={v} L={l} R={r}"
  49. );
  50. Thread.Sleep(20);
  51. }
  52. }
  53. }
  54. }