Advertisement
Roniere

Creating a library to manipulate a LED busbar

Aug 13th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. // -------------------------------------- Prototype Function --------------------------------------------------------------------------
  2. // Header File: busLeds.h
  3. #ifndef LEDSBUSBAR_H_INCLUDED
  4.     #define LEDSBUSBAR_H_INCLUDED
  5.  
  6.     unsigned char turnOnLeds(unsigned char code, unsigned char stateLeds);
  7.     unsigned char turnOffLeds(unsigned char code, unsigned char stateLeds);
  8.     unsigned currentLedsState(unsigned char stateLeds);
  9.  
  10. #endif // LEDSBUSBAR_H_INCLUDED
  11.  
  12. //---------------------------------------- LEDs's Busbar Library ----------------------------------------------------------------------
  13. // .C File: busLed.h
  14. #include <stdio.h>
  15. #include "ledsBusbar.h"
  16.  
  17. // Receives the parameter "code" which identify what Leds in busbar which will turn on.
  18. // For instance, code = 0xA2 will turn on the lEDs 7, 5, 1 and 0.
  19. // The parameter "stateBits" receive the previous state of LEDs busbar.
  20.     unsigned char turnOnLeds(unsigned char code, unsigned char stateLeds){
  21.         unsigned char getLeds;
  22.         getLeds = stateLeds | code;
  23.         return getLeds;
  24.     }
  25. // Receives the parameter "code" which identify what LEDs's busbar which will turn off.
  26. // For instance, code = 0x55 will turn on the lEDs 6, 4, 2 and 0.
  27. // The parameter "stateBits" receive the previous state of LEDs's busbar.
  28.     unsigned char turnOffLeds(unsigned char code, unsigned char stateLeds){
  29.         unsigned char getLeds = 0x00;
  30.         getLeds = stateLeds & (~code);
  31.     return getLeds;
  32.     }
  33. // Displays the current state of LEDs's busbar.
  34.     unsigned currentLedsState(unsigned char stateLeds){
  35.         unsigned char currentLeds;
  36.         currentLeds = stateLeds;
  37.         return currentLeds;
  38.     }
  39.  
  40. //-------------------------------------------  Test File for this library -------------------------------------------------------------
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include "ledsBusbar.h"
  44.  
  45. int main(){
  46.     int turnOff = 0, exit = 0;
  47.     unsigned char codeOn = 0x00, codeOff = 0x00,  leds = 0x00, stateLeds = 0x00;
  48.  
  49.     while (exit != 1){
  50.         printf("Insert the hexadecimal code to turn on LEDs: ");
  51.         scanf("%x",&codeOn);
  52.  
  53.         leds = turnOnLeds(codeOn, stateLeds);
  54.         printf("%x\n",leds);
  55.  
  56.         printf("\nDo you want to turn off LEDs: Yes - Press 1 / No - Press any Character: ");
  57.         scanf("%d", &turnOff);
  58.  
  59.         if (turnOff == 1){
  60.             printf("Insert the hexadecimal code to turn off LEDs: ");
  61.             scanf("%x",&codeOff);
  62.             leds = turnOffLeds(codeOff, leds);
  63.             printf("%x\n",leds);
  64.         }
  65.  
  66.         stateLeds = currentLedsState(leds);
  67.  
  68.         printf("\n\nThe current leds state is %X.",stateLeds);
  69.  
  70.         printf("\n\nDo you want to continue? Yes - Press any Character / No - Press 1: ");
  71.         scanf("%d",&exit);
  72.     }
  73.     return 0;
  74. }
  75. *** Note: This code display how to develop a code library which have three function:
  76.           1 - turnOnLeds - Receive two paramenters, code and stateLeds. "code" inserts a 8 bits value coded in hexadecimal format to
  77.               turn on 8 leds in busbar. "stateLeds" receives the previous state of the LED busbar.
  78.           2 - turnOffLeds - Receive two paramenters, code and stateLeds. "code" inserts a 8 bits value coded in hexadecimal format to
  79.               turn off 8 leds in busbar. "stateLeds" receives the previous state of the LED busbar.
  80.           3 - currentLedsState - Display the current state of led busbar.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement