ioext.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Output extender
  3. *
  4. * Copyright (c) 2013 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "ioext.h"
  21. struct ioext_context ioext_ctx;
  22. /* Write the I/O-extender state out to the hardware
  23. * shift register.
  24. */
  25. void ioext_commit(void)
  26. {
  27. struct ioext_context *ctx = &ioext_ctx;
  28. uint8_t chip;
  29. /* For each chip. */
  30. for (chip = 0; chip < EXTOUT_NR_CHIPS; chip++) {
  31. /* Write the new state to the shift register,
  32. * if it did not change.
  33. */
  34. if (ctx->states[chip] != ctx->old_states[chip]) {
  35. ctx->old_states[chip] = ctx->states[chip];
  36. pcf8574_write(&ctx->chips[chip],
  37. ctx->states[chip]);
  38. }
  39. }
  40. }
  41. /* Initialize the I/O-extender.
  42. * This also initializes the shift register.
  43. * If "all_ones" is true, all state bits are initialized to 1.
  44. */
  45. void ioext_init(bool all_ones)
  46. {
  47. struct ioext_context *ctx = &ioext_ctx;
  48. uint8_t chip;
  49. /* Reset data structures. */
  50. memset(ctx, 0, sizeof(*ctx));
  51. if (all_ones) {
  52. memset(ctx->states, 0xFF, sizeof(ctx->states));
  53. memset(ctx->old_states, 0xFF, sizeof(ctx->states));
  54. }
  55. /* Initialize the shift register. */
  56. for (chip = 0; chip < EXTOUT_NR_CHIPS; chip++)
  57. pcf8574_init(&ctx->chips[chip], chip, 1, all_ones);
  58. }