switch.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*********************************************************************
  2. *
  3. * Switch library for Fraise pic18f device
  4. *
  5. *
  6. *********************************************************************
  7. * Author Date Comment
  8. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. * Antoine Rousseau march 2013 Original.
  10. ********************************************************************/
  11. /*
  12. # This program is free software; you can redistribute it and/or
  13. # modify it under the terms of the GNU General Public License
  14. # as published by the Free Software Foundation; either version 2
  15. # of the License, or (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  24. # MA 02110-1301, USA.
  25. */
  26. #include <config.h>
  27. #include <switch.h>
  28. #define MAX_CHANNELS 16
  29. #define CHANNELS_GROUPS ((MAX_CHANNELS+7)/8)
  30. static unsigned char Pins[MAX_CHANNELS];
  31. static unsigned char States[CHANNELS_GROUPS];
  32. static unsigned char oldStates[CHANNELS_GROUPS];
  33. #define CHANISSET(num) (bittst(States[num>>3],num&7))
  34. #define CHANCLR(num) (bitclr(States[num>>3],num&7))
  35. #define CHANSET(num) (bitset(States[num>>3],num&7))
  36. #define OLDCHANISSET(num) (bittst(oldStates[num>>3],num&7))
  37. #define OLDCHANCLR(num) (bitclr(oldStates[num>>3],num&7))
  38. #define OLDCHANSET(num) (bitset(oldStates[num>>3],num&7))
  39. void switchInit()
  40. {
  41. unsigned char i;
  42. for(i=0;i<CHANNELS_GROUPS;i++) {
  43. oldStates[i] = States[i] = 1;
  44. }
  45. for(i=0;i<MAX_CHANNELS;i++) {
  46. Pins[i] = 255;
  47. }
  48. }
  49. void switchSelectHW(unsigned char channel, unsigned char *port, unsigned char bit)
  50. {
  51. Pins[channel] = (((unsigned int)(port-&PORTA)&7)<<4) + (bit&7);
  52. }
  53. void switchDeselect(unsigned char channel)
  54. {
  55. Pins[channel] = 255;
  56. }
  57. void switchService(void)
  58. {
  59. unsigned char i,pin;
  60. for(i=0;i<MAX_CHANNELS;i++) {
  61. pin=Pins[i];
  62. if(pin != 255) {
  63. if(bittst(*(&PORTA+(pin>>4)),pin&7)) CHANSET(i);
  64. else CHANCLR(i);
  65. }
  66. }
  67. }
  68. char switchSend(void)
  69. {
  70. static unsigned char chan=0;
  71. unsigned char count=0,pin,set;
  72. while(count<4) {
  73. pin=Pins[chan];
  74. if(pin != 255) {
  75. set=CHANISSET(chan);
  76. if(set!=OLDCHANISSET(chan)) {
  77. if(set) OLDCHANSET(chan);
  78. else OLDCHANCLR(chan);
  79. printf("C s %d %d\n",chan,set==0);
  80. count++;
  81. }
  82. }
  83. chan++;
  84. if(chan>=MAX_CHANNELS) {
  85. chan=0;
  86. break;
  87. }
  88. }
  89. return count;
  90. }
  91. char switchGet(unsigned char chan)
  92. {
  93. return CHANISSET(chan);
  94. }