20_FRM_PWM.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. ########################################################################################
  2. #
  3. # $Id: 20_FRM_PWM.pm 15929 2018-01-19 21:11:06Z jensb $
  4. #
  5. # FHEM module for one Firmata PWM output pin
  6. #
  7. ########################################################################################
  8. #
  9. # LICENSE AND COPYRIGHT
  10. #
  11. # Copyright (C) 2013 ntruchess
  12. # Copyright (C) 2016 jensb
  13. #
  14. # All rights reserved
  15. #
  16. # This script is free software; you can redistribute it and/or modify
  17. # it under the terms of the GNU General Public License as published by
  18. # the Free Software Foundation; either version 2 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # The GNU General Public License can be found at
  22. # http://www.gnu.org/copyleft/gpl.html.
  23. # A copy is found in the textfile GPL.txt and important notices to the license
  24. # from the author is found in LICENSE.txt distributed with these scripts.
  25. #
  26. # This script is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. #
  31. # This copyright notice MUST APPEAR in all copies of the script!
  32. #
  33. ########################################################################################
  34. package main;
  35. use strict;
  36. use warnings;
  37. #add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
  38. BEGIN {
  39. if (!grep(/FHEM\/lib$/,@INC)) {
  40. foreach my $inc (grep(/FHEM$/,@INC)) {
  41. push @INC,$inc."/lib";
  42. };
  43. };
  44. };
  45. use Device::Firmata::Constants qw/ :all /;
  46. use SetExtensions qw/ :all /;
  47. #####################################
  48. my %gets = (
  49. "dim" => 0,
  50. "value" => 0,
  51. "devStateIcon" => 0,
  52. );
  53. my %sets = (
  54. "on" => 0,
  55. "off" => 0,
  56. "toggle" => 0,
  57. "value" => 1,
  58. "dim:slider,0,1,100" => 1,
  59. "fadeTo" => 2,
  60. "dimUp" => 0,
  61. "dimDown" => 0,
  62. );
  63. sub
  64. FRM_PWM_Initialize($)
  65. {
  66. my ($hash) = @_;
  67. $hash->{SetFn} = "FRM_PWM_Set";
  68. $hash->{GetFn} = "FRM_PWM_Get";
  69. $hash->{DefFn} = "FRM_Client_Define";
  70. $hash->{InitFn} = "FRM_PWM_Init";
  71. $hash->{UndefFn} = "FRM_Client_Undef";
  72. $hash->{AttrFn} = "FRM_PWM_Attr";
  73. $hash->{StateFn} = "FRM_PWM_State";
  74. $hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev $main::readingFnAttributes";
  75. main::LoadModule("FRM");
  76. }
  77. sub
  78. FRM_PWM_Init($$)
  79. {
  80. my ($hash,$args) = @_;
  81. my $ret = FRM_Init_Pin_Client($hash,$args,PIN_PWM);
  82. return $ret if (defined $ret);
  83. my $firmata = $hash->{IODev}->{FirmataDevice};
  84. my $name = $hash->{NAME};
  85. my $resolution = 8;
  86. if (defined $firmata->{metadata}{pwm_resolutions}) {
  87. $resolution = $firmata->{metadata}{pwm_resolutions}{$hash->{PIN}}
  88. }
  89. $hash->{resolution} = $resolution;
  90. $hash->{".max"} = defined $resolution ? (1<<$resolution)-1 : 255;
  91. $hash->{".dim"} = 0;
  92. $hash->{".toggle"} = "off";
  93. if (! (defined AttrVal($name,"stateFormat",undef))) {
  94. $main::attr{$name}{"stateFormat"} = "value";
  95. }
  96. my $value = ReadingsVal($name,"value",undef);
  97. if (defined $value and AttrVal($hash->{NAME},"restoreOnReconnect","on") eq "on") {
  98. FRM_PWM_Set($hash,$name,"value",$value);
  99. }
  100. main::readingsSingleUpdate($hash,"state","Initialized",1);
  101. return undef;
  102. }
  103. sub
  104. FRM_PWM_Set($@)
  105. {
  106. my ($hash, $name, $cmd, @a) = @_;
  107. my @match = grep( $_ =~ /^$cmd($|:)/, keys %sets );
  108. #-- check argument
  109. return SetExtensions($hash, join(" ", keys %sets), $name, $cmd, @a) unless @match == 1;
  110. return "$cmd expects $sets{$match[0]} parameters" unless (@a eq $sets{$match[0]});
  111. eval {
  112. SETHANDLER: {
  113. my $value = $a[0] if @a;
  114. $cmd eq "on" and do {
  115. FRM_PWM_writeOut($hash,$hash->{".max"});
  116. $hash->{".toggle"} = "on";
  117. last;
  118. };
  119. $cmd eq "off" and do {
  120. FRM_PWM_writeOut($hash,0);
  121. $hash->{".toggle"} = "off";
  122. last;
  123. };
  124. $cmd eq "toggle" and do {
  125. my $toggle = $hash->{".toggle"};
  126. TOGGLEHANDLER: {
  127. $toggle eq "off" and do {
  128. FRM_PWM_writeOut($hash,$hash->{".dim"});
  129. $hash->{".toggle"} = "up";
  130. last;
  131. };
  132. $toggle eq "up" and do {
  133. FRM_PWM_writeOut($hash,$hash->{".max"});
  134. $hash->{".toggle"} = "on";
  135. last;
  136. };
  137. $toggle eq "on" and do {
  138. FRM_PWM_writeOut($hash,$hash->{".dim"});
  139. $hash->{".toggle"} = "down";
  140. last;
  141. };
  142. $toggle eq "down" and do {
  143. FRM_PWM_writeOut($hash,0);
  144. $hash->{".toggle"} = "off";
  145. last;
  146. };
  147. };
  148. last;
  149. };
  150. $cmd eq "value" and do {
  151. my $max = $hash->{".max"};
  152. die "maximum value of $max exceeded: $value" if ($value > $max);
  153. FRM_PWM_writeOut($hash,$value);
  154. TOGGLEHANDLER: {
  155. $value == $max and do {
  156. $hash->{".toggle"} = "on";
  157. last;
  158. };
  159. $value == 0 and do {
  160. $hash->{".toggle"} = "off";
  161. last;
  162. };
  163. $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
  164. $hash->{".dim"} = $value;
  165. };
  166. last;
  167. };
  168. $cmd eq "dim" and do {
  169. die "maximum value of 100 exceeded: $value" if ($value > 100);
  170. my $dim = int($hash->{".max"}*$value/100);
  171. FRM_PWM_writeOut($hash,$dim);
  172. TOGGLEHANDLER: {
  173. $value == 100 and do {
  174. $hash->{".toggle"} = "on";
  175. last;
  176. };
  177. $value == 0 and do {
  178. $hash->{".toggle"} = "off";
  179. last;
  180. };
  181. $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
  182. $hash->{".dim"} = $dim;
  183. };
  184. last;
  185. };
  186. $cmd eq "fadeTo" and do {
  187. die "fadeTo not implemented yet";
  188. };
  189. $cmd eq "dimUp" and do {
  190. my $dim = $hash->{".dim"};
  191. my $max = $hash->{".max"};
  192. if ($dim > $max * 0.9) {
  193. $dim = $max;
  194. $hash->{".toggle"} = "on";
  195. } else {
  196. $dim = $dim + $max / 10;
  197. $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
  198. }
  199. FRM_PWM_writeOut($hash,$dim);
  200. $hash->{".dim"} = $dim;
  201. last;
  202. };
  203. $cmd eq "dimDown" and do {
  204. my $step = $hash->{".max"} / 10;
  205. my $dim = $hash->{".dim"};
  206. if ($dim < $step) {
  207. $dim = 0;
  208. $hash->{".toggle"} = "off";
  209. } else {
  210. $dim = $dim - $step;
  211. $hash->{".toggle"} = "down" unless $hash->{".toggle"} eq "up";
  212. }
  213. FRM_PWM_writeOut($hash,$dim);
  214. $hash->{".dim"} = $dim;
  215. last;
  216. };
  217. }
  218. };
  219. if ($@) {
  220. $@ =~ /^(.*)( at.*FHEM.*)$/;
  221. $hash->{STATE} = "error setting '$cmd': ".(defined $1 ? $1 : $@);
  222. return "error setting '$hash->{NAME} $cmd': ".(defined $1 ? $1 : $@);
  223. }
  224. return undef;
  225. }
  226. sub
  227. FRM_PWM_writeOut($$)
  228. {
  229. my ($hash,$value) = @_;
  230. FRM_Client_FirmataDevice($hash)->analog_write($hash->{PIN},$value);
  231. readingsBeginUpdate($hash);
  232. readingsBulkUpdate($hash,"value",$value, 1);
  233. readingsBulkUpdate($hash,"dim",int($value*100/$hash->{".max"}), 1);
  234. readingsEndUpdate($hash, 1);
  235. }
  236. sub
  237. FRM_PWM_Get($@)
  238. {
  239. my ($hash, $name, $cmd, @a) = @_;
  240. return "FRM_PWM: Get with unknown argument $cmd, choose one of ".join(" ", sort keys %gets)
  241. unless defined($gets{$cmd});
  242. GETHANDLER: {
  243. $cmd eq 'dim' and do {
  244. return ReadingsVal($name,"dim",undef);
  245. };
  246. $cmd eq 'value' and do {
  247. return ReadingsVal($name,"value",undef);
  248. };
  249. $cmd eq 'devStateIcon' and do {
  250. return return "not implemented yet";
  251. };
  252. }
  253. }
  254. sub
  255. FRM_PWM_State($$$$)
  256. {
  257. my ($hash, $tim, $sname, $sval) = @_;
  258. my $name = $hash->{NAME};
  259. if ($sname eq "value") {
  260. # depending on the FHEM startup timing and the Arduino connection type, FHEM statefile restore and Arduino connect take place in arbitrary order
  261. if (AttrVal($name, "restoreOnStartup", "on") eq "on") {
  262. $hash->{READINGS}{$sname}{VAL} = $sval;
  263. $hash->{READINGS}{$sname}{TIME} = $tim;
  264. if (defined($hash->{IODev}) && defined($hash->{IODev}->{FirmataDevice} && $hash->{IODev}->{FirmataDevice}->{state} eq "Initialized")) {
  265. FRM_PWM_Set($hash, $name, "value", $sval);
  266. }
  267. } else {
  268. $hash->{READINGS}{$sname}{VAL} = undef;
  269. $hash->{READINGS}{$sname}{TIME} = gettimeofday();
  270. }
  271. }
  272. return 0; # default processing by fhem.pl
  273. }
  274. sub
  275. FRM_PWM_Attr($$$$)
  276. {
  277. my ($command,$name,$attribute,$value) = @_;
  278. my $hash = $main::defs{$name};
  279. eval {
  280. if ($command eq "set") {
  281. ARGUMENT_HANDLER: {
  282. $attribute eq "IODev" and do {
  283. if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
  284. FRM_Client_AssignIOPort($hash,$value);
  285. FRM_Init_Client($hash) if (defined ($hash->{IODev}));
  286. }
  287. last;
  288. };
  289. }
  290. }
  291. };
  292. if ($@) {
  293. $@ =~ /^(.*)( at.*FHEM.*)$/;
  294. $hash->{STATE} = "error setting $attribute to $value: ".$1;
  295. return "cannot $command attribute $attribute to $value for $name: ".$1;
  296. }
  297. }
  298. 1;
  299. =pod
  300. CHANGES
  301. 2016 jensb
  302. o modified subs FRM_PWM_Init and FRM_PWM_State to support attribute "restoreOnStartup"
  303. =cut
  304. =pod
  305. =item device
  306. =item summary Firmata: PWM output
  307. =item summary_DE Firmata: PWM Ausgang
  308. =begin html
  309. <a name="FRM_PWM"></a>
  310. <h3>FRM_PWM</h3>
  311. <ul>
  312. This module represents a pin of a <a href="http://www.firmata.org">Firmata device</a>
  313. that should be configured as a pulse width modulated output (PWM).<br><br>
  314. Requires a defined <a href="#FRM">FRM</a> device to work. The pin must be listed in the internal reading "<a href="#FRMinternals">pwm_pins</a>"<br>
  315. of the FRM device (after connecting to the Firmata device) to be used as PWM output.<br><br>
  316. <a name="FRM_PWMdefine"></a>
  317. <b>Define</b>
  318. <ul>
  319. <code>define &lt;name&gt; FRM_PWM &lt;pin&gt;</code><br><br>
  320. Defines the FRM_PWM device. &lt;pin&gt> is the arduino-pin to use.
  321. </ul><br>
  322. <a name="FRM_PWMset"></a>
  323. <b>Set</b><br>
  324. <ul>
  325. <li><code>set &lt;name&gt; on</code><br>
  326. sets the pulse-width to 100%<br>
  327. </li>
  328. <li>
  329. <code>set &lt;name&gt; off</code><br>
  330. sets the pulse-width to 0%<br>
  331. </li>
  332. <li>
  333. <a href="#setExtensions">set extensions</a> are supported<br>
  334. </li>
  335. <li>
  336. <code>set &lt;name&gt; toggle</code><br>
  337. toggles the pulse-width in between to the last value set by 'value' or 'dim' and 0 respectivly 100%<br>
  338. </li>
  339. <li>
  340. <code>set &lt;name&gt; value &lt;value&gt;</code><br>
  341. sets the pulse-width to the value specified<br>
  342. The min value is zero and the max value depends on the Firmata device (see internal reading<br>
  343. "<a href="#FRMinternals">pwm_resolutions</a>" of the FRM device). For 8 bits resolution the range
  344. is 0 to 255 (also see <a href="http://arduino.cc/en/Reference/AnalogWrite">analogWrite()</a> for details)<br>
  345. </li>
  346. <li>
  347. <code>set &lt;name&gt; dim &lt;value&gt;</code><br>
  348. sets the pulse-width to the value specified in percent<br>
  349. Range is from 0 to 100<br>
  350. </li>
  351. <li>
  352. <code>set &lt;name&gt; dimUp</code><br>
  353. increases the pulse-width by 10%<br>
  354. </li>
  355. <li>
  356. <code>set &lt;name&gt; dimDown</code><br>
  357. decreases the pulse-width by 10%<br>
  358. </li>
  359. </ul><br>
  360. <a name="FRM_PWMget"></a>
  361. <b>Get</b><br>
  362. <ul>
  363. N/A
  364. </ul><br>
  365. <a name="FRM_PWMattr"></a>
  366. <b>Attributes</b><br>
  367. <ul>
  368. <li>restoreOnStartup &lt;on|off&gt;</li>
  369. <li>restoreOnReconnect &lt;on|off&gt;</li>
  370. <li><a href="#IODev">IODev</a><br>
  371. Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
  372. than one FRM-device defined.)
  373. </li>
  374. <li><a href="#eventMap">eventMap</a><br></li>
  375. <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
  376. </ul><br>
  377. <a name="FRM_PWMnotes"></a>
  378. <b>Notes</b><br>
  379. <ul>
  380. <li>attribute <i>stateFormat</i><br>
  381. In most cases it is a good idea to assign "value" to the attribute <i>stateFormat</i>. This will show the
  382. current value of the pin in the web interface.
  383. </li>
  384. </ul>
  385. </ul><br>
  386. =end html
  387. =cut