digital_led_bar.c 799 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include "rp.h"
  5. int main (int argc, char **argv) {
  6. float percent;
  7. // percentage can be provided as an argument
  8. if (argc > 1) {
  9. percent = atof(argv[1]);
  10. } else {
  11. percent = 50.0;
  12. }
  13. printf("Bar showing %.1f%%\n", percent);
  14. // Initialization of API
  15. if (rp_Init() != RP_OK) {
  16. fprintf(stderr, "Red Pitaya API init failed!\n");
  17. return EXIT_FAILURE;
  18. }
  19. // Turning on leds based on parameter percent
  20. for (int i=0; i<8; i++) {
  21. if (percent > (i*(100.0/8))) {
  22. rp_DpinSetState(i+RP_LED0, RP_HIGH);
  23. } else {
  24. rp_DpinSetState(i+RP_LED0, RP_LOW);
  25. }
  26. }
  27. // Releasing resources
  28. rp_Release();
  29. return EXIT_SUCCESS;
  30. }