jswrap_pin.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
  3. *
  4. * Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * ----------------------------------------------------------------------------
  11. * This file is designed to be parsed during the build process
  12. *
  13. * JavaScript Pin Object Functions
  14. * ----------------------------------------------------------------------------
  15. */
  16. #include "jswrap_pin.h"
  17. /*JSON{ "type":"class",
  18. "class" : "Pin",
  19. "check" : "jsvIsPin(var)",
  20. "description" : ["This is the built-in class for Pins, such as D0,D1,LED1, or BTN",
  21. "You can call the methods on Pin, or you can use Wiring-style functions such as digitalWrite" ]
  22. }*/
  23. /*JSON{ "type":"method", "class": "Pin", "name" : "read",
  24. "description" : "Returns the input state of the pin as a boolean",
  25. "generate" : "jswrap_pin_read",
  26. "return" : ["bool", "Whether pin is a logical 1 or 0"]
  27. }*/
  28. bool jswrap_pin_read(JsVar *parent) {
  29. Pin pin = jshGetPinFromVar(parent);
  30. return jshPinInput(pin);
  31. }
  32. /*JSON{ "type":"method", "class": "Pin", "name" : "set",
  33. "description" : "Sets the output state of the pin to a 1",
  34. "generate" : "jswrap_pin_set"
  35. }*/
  36. void jswrap_pin_set(JsVar *parent) {
  37. Pin pin = jshGetPinFromVar(parent);
  38. jshPinOutput(pin, 1);
  39. }
  40. /*JSON{ "type":"method", "class": "Pin", "name" : "reset",
  41. "description" : "Sets the output state of the pin to a 0",
  42. "generate" : "jswrap_pin_reset"
  43. }*/
  44. void jswrap_pin_reset(JsVar *parent) {
  45. Pin pin = jshGetPinFromVar(parent);
  46. jshPinOutput(pin, 0);
  47. }
  48. /*JSON{ "type":"method", "class": "Pin", "name" : "write",
  49. "description" : "Sets the output state of the pin to the parameter given",
  50. "generate" : "jswrap_pin_write",
  51. "params" : [ [ "value", "bool", "Whether to set output high (true/1) or low (false/0)"] ]
  52. }*/
  53. void jswrap_pin_write(JsVar *parent, bool value) {
  54. Pin pin = jshGetPinFromVar(parent);
  55. jshPinOutput(pin, value);
  56. }
  57. /*JSON{ "type":"method", "class": "Pin", "name" : "writeAtTime", "ifndef" : "SAVE_ON_FLASH",
  58. "description" : "Sets the output state of the pin to the parameter given at the specified time",
  59. "generate" : "jswrap_pin_writeAtTime",
  60. "params" : [ [ "value", "bool", "Whether to set output high (true/1) or low (false/0)"],
  61. ["time", "float", "Time at which to write"] ]
  62. }*/
  63. void jswrap_pin_writeAtTime(JsVar *parent, bool value, JsVarFloat time) {
  64. Pin pin = jshGetPinFromVar(parent);
  65. JsSysTime sTime = jshGetTimeFromMilliseconds(time*1000);
  66. jshPinOutputAtTime(sTime, pin, value);
  67. }