Geen omschrijving

tm1637.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
  3. * Copyright (c) 2015 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. // Some vars
  25. var colon = true;
  26. var interval;
  27. // Load display
  28. var tm1637 = require('jsupm_tm1637');
  29. // Instantiate on pins 0 (Clk) and 1 (Dio)
  30. var display = new tm1637.TM1637(0, 1);
  31. // Get the current time
  32. var now = new Date();
  33. console.log("System time: " + now.getHours() + ":" + ("0" + now.getMinutes()).slice(-2));
  34. console.log("Time zone can be changed by setting the TZ environment variable.");
  35. // Display and time update function
  36. function update(){
  37. now = new Date();
  38. var time = now.getHours().toString() + ("0" + now.getMinutes().toString()).slice(-2);
  39. display.writeString(time);
  40. display.setColon(colon = !colon);
  41. }
  42. // Start with a 7-segment encoded box on the display
  43. display.write(0x39, 0x09, 0x09, 0x0f);
  44. // Start displaying the clock after 3 seconds
  45. setTimeout(function(){
  46. // And update every second thereafter
  47. interval = setInterval(update, 1000);
  48. }, 3000)
  49. // Exit handler
  50. process.on('SIGINT', function()
  51. {
  52. clearInterval(interval);
  53. display = null;
  54. tm1637.cleanUp();
  55. tm1637 = null;
  56. console.log("Interrupt received, exiting...");
  57. process.exit(0);
  58. });