20_FRM_PWM.pm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. ##############################################
  2. # $Id: 20_FRM_PWM.pm 5927 2014-05-21 21:56:37Z ntruchsess $
  3. ##############################################
  4. package main;
  5. use strict;
  6. use warnings;
  7. #add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
  8. BEGIN {
  9. if (!grep(/FHEM\/lib$/,@INC)) {
  10. foreach my $inc (grep(/FHEM$/,@INC)) {
  11. push @INC,$inc."/lib";
  12. };
  13. };
  14. };
  15. use Device::Firmata::Constants qw/ :all /;
  16. use SetExtensions qw/ :all /;
  17. #####################################
  18. my %gets = (
  19. "dim" => 0,
  20. "value" => 0,
  21. "devStateIcon" => 0,
  22. );
  23. my %sets = (
  24. "on" => 0,
  25. "off" => 0,
  26. "toggle" => 0,
  27. "value" => 1,
  28. "dim:slider,0,1,100" => 1,
  29. "fadeTo" => 2,
  30. "dimUp" => 0,
  31. "dimDown" => 0,
  32. );
  33. sub
  34. FRM_PWM_Initialize($)
  35. {
  36. my ($hash) = @_;
  37. $hash->{SetFn} = "FRM_PWM_Set";
  38. $hash->{GetFn} = "FRM_PWM_Get";
  39. $hash->{DefFn} = "FRM_Client_Define";
  40. $hash->{InitFn} = "FRM_PWM_Init";
  41. $hash->{UndefFn} = "FRM_Client_Undef";
  42. $hash->{AttrFn} = "FRM_PWM_Attr";
  43. $hash->{StateFn} = "FRM_PWM_State";
  44. $hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev $main::readingFnAttributes";
  45. main::LoadModule("FRM");
  46. }
  47. sub
  48. FRM_PWM_Init($$)
  49. {
  50. my ($hash,$args) = @_;
  51. my $ret = FRM_Init_Pin_Client($hash,$args,PIN_PWM);
  52. return $ret if (defined $ret);
  53. my $firmata = $hash->{IODev}->{FirmataDevice};
  54. my $name = $hash->{NAME};
  55. my $resolution = $firmata->{metadata}{pwm_resolutions}{$hash->{PIN}} if (defined $firmata->{metadata}{pwm_resolutions});
  56. $hash->{".max"} = defined $resolution ? (1<<$resolution)-1 : 255;
  57. $hash->{".dim"} = 0;
  58. $hash->{".toggle"} = "off";
  59. if (! (defined AttrVal($name,"stateFormat",undef))) {
  60. $main::attr{$name}{"stateFormat"} = "value";
  61. }
  62. my $value = ReadingsVal($name,"value",undef);
  63. if (defined $value and AttrVal($hash->{NAME},"restoreOnReconnect","on") eq "on") {
  64. FRM_PWM_Set($hash,$name,$value);
  65. }
  66. main::readingsSingleUpdate($hash,"state","Initialized",1);
  67. return undef;
  68. }
  69. sub
  70. FRM_PWM_Set($@)
  71. {
  72. my ($hash, $name, $cmd, @a) = @_;
  73. my @match = grep( $_ =~ /^$cmd($|:)/, keys %sets );
  74. #-- check argument
  75. return SetExtensions($hash, join(" ", keys %sets), $name, $cmd, @a) unless @match == 1;
  76. return "$cmd expects $sets{$match[0]} parameters" unless (@a eq $sets{$match[0]});
  77. eval {
  78. SETHANDLER: {
  79. my $value = $a[0] if @a;
  80. $cmd eq "on" and do {
  81. FRM_PWM_writeOut($hash,$hash->{".max"});
  82. $hash->{".toggle"} = "on";
  83. last;
  84. };
  85. $cmd eq "off" and do {
  86. FRM_PWM_writeOut($hash,0);
  87. $hash->{".toggle"} = "off";
  88. last;
  89. };
  90. $cmd eq "toggle" and do {
  91. my $toggle = $hash->{".toggle"};
  92. TOGGLEHANDLER: {
  93. $toggle eq "off" and do {
  94. FRM_PWM_writeOut($hash,$hash->{".dim"});
  95. $hash->{".toggle"} = "up";
  96. last;
  97. };
  98. $toggle eq "up" and do {
  99. FRM_PWM_writeOut($hash,$hash->{".max"});
  100. $hash->{".toggle"} = "on";
  101. last;
  102. };
  103. $toggle eq "on" and do {
  104. FRM_PWM_writeOut($hash,$hash->{".dim"});
  105. $hash->{".toggle"} = "down";
  106. last;
  107. };
  108. $toggle eq "down" and do {
  109. FRM_PWM_writeOut($hash,0);
  110. $hash->{".toggle"} = "off";
  111. last;
  112. };
  113. };
  114. last;
  115. };
  116. $cmd eq "value" and do {
  117. my $max = $hash->{".max"};
  118. die "maximum value of $max exceeded: $value" if ($value > $max);
  119. FRM_PWM_writeOut($hash,$value);
  120. TOGGLEHANDLER: {
  121. $value == $max and do {
  122. $hash->{".toggle"} = "on";
  123. last;
  124. };
  125. $value == 0 and do {
  126. $hash->{".toggle"} = "off";
  127. last;
  128. };
  129. $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
  130. $hash->{".dim"} = $value;
  131. };
  132. last;
  133. };
  134. $cmd eq "dim" and do {
  135. die "maximum value of 100 exceeded: $value" if ($value > 100);
  136. my $dim = int($hash->{".max"}*$value/100);
  137. FRM_PWM_writeOut($hash,$dim);
  138. TOGGLEHANDLER: {
  139. $value == 100 and do {
  140. $hash->{".toggle"} = "on";
  141. last;
  142. };
  143. $value == 0 and do {
  144. $hash->{".toggle"} = "off";
  145. last;
  146. };
  147. $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
  148. $hash->{".dim"} = $dim;
  149. };
  150. last;
  151. };
  152. $cmd eq "fadeTo" and do {
  153. die "fadeTo not implemented yet";
  154. };
  155. $cmd eq "dimUp" and do {
  156. my $dim = $hash->{".dim"};
  157. my $max = $hash->{".max"};
  158. if ($dim > $max * 0.9) {
  159. $dim = $max;
  160. $hash->{".toggle"} = "on";
  161. } else {
  162. $dim = $dim + $max / 10;
  163. $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
  164. }
  165. FRM_PWM_writeOut($hash,$dim);
  166. $hash->{".dim"} = $dim;
  167. last;
  168. };
  169. $cmd eq "dimDown" and do {
  170. my $step = $hash->{".max"} / 10;
  171. my $dim = $hash->{".dim"};
  172. if ($dim < $step) {
  173. $dim = 0;
  174. $hash->{".toggle"} = "off";
  175. } else {
  176. $dim = $dim - $step;
  177. $hash->{".toggle"} = "down" unless $hash->{".toggle"} eq "up";
  178. }
  179. FRM_PWM_writeOut($hash,$dim);
  180. $hash->{".dim"} = $dim;
  181. last;
  182. };
  183. }
  184. };
  185. if ($@) {
  186. $@ =~ /^(.*)( at.*FHEM.*)$/;
  187. $hash->{STATE} = "error setting '$cmd': ".(defined $1 ? $1 : $@);
  188. return "error setting '$hash->{NAME} $cmd': ".(defined $1 ? $1 : $@);
  189. }
  190. return undef;
  191. }
  192. sub
  193. FRM_PWM_writeOut($$)
  194. {
  195. my ($hash,$value) = @_;
  196. FRM_Client_FirmataDevice($hash)->analog_write($hash->{PIN},$value);
  197. readingsBeginUpdate($hash);
  198. readingsBulkUpdate($hash,"value",$value, 1);
  199. readingsBulkUpdate($hash,"dim",int($value*100/$hash->{".max"}), 1);
  200. readingsEndUpdate($hash, 1);
  201. }
  202. sub
  203. FRM_PWM_Get($@)
  204. {
  205. my ($hash, $name, $cmd, @a) = @_;
  206. return "FRM_PWM: Get with unknown argument $cmd, choose one of ".join(" ", sort keys %gets)
  207. unless defined($gets{$cmd});
  208. GETHANDLER: {
  209. $cmd eq 'dim' and do {
  210. return ReadingsVal($name,"dim",undef);
  211. };
  212. $cmd eq 'value' and do {
  213. return ReadingsVal($name,"value",undef);
  214. };
  215. $cmd eq 'devStateIcon' and do {
  216. return return "not implemented yet";
  217. };
  218. }
  219. }
  220. sub
  221. FRM_PWM_State($$$$)
  222. {
  223. my ($hash, $tim, $sname, $sval) = @_;
  224. if ($sname eq "value") {
  225. FRM_PWM_Set($hash,$hash->{NAME},$sname,$sval);
  226. }
  227. }
  228. sub
  229. FRM_PWM_Attr($$$$)
  230. {
  231. my ($command,$name,$attribute,$value) = @_;
  232. my $hash = $main::defs{$name};
  233. eval {
  234. if ($command eq "set") {
  235. ARGUMENT_HANDLER: {
  236. $attribute eq "IODev" and do {
  237. if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
  238. FRM_Client_AssignIOPort($hash,$value);
  239. FRM_Init_Client($hash) if (defined ($hash->{IODev}));
  240. }
  241. last;
  242. };
  243. }
  244. }
  245. };
  246. if ($@) {
  247. $@ =~ /^(.*)( at.*FHEM.*)$/;
  248. $hash->{STATE} = "error setting $attribute to $value: ".$1;
  249. return "cannot $command attribute $attribute to $value for $name: ".$1;
  250. }
  251. }
  252. 1;
  253. =pod
  254. =begin html
  255. <a name="FRM_PWM"></a>
  256. <h3>FRM_PWM</h3>
  257. <ul>
  258. represents a pin of an <a href="http://www.arduino.cc">Arduino</a> running <a href="http://www.firmata.org">Firmata</a>
  259. configured for analog output.<br>
  260. The value set will be output by the specified pin as a pulse-width-modulated signal.<br>
  261. Requires a defined <a href="#FRM">FRM</a>-device to work.<br><br>
  262. <a name="FRM_PWMdefine"></a>
  263. <b>Define</b>
  264. <ul>
  265. <code>define &lt;name&gt; FRM_PWM &lt;pin&gt;</code> <br>
  266. Defines the FRM_PWM device. &lt;pin&gt> is the arduino-pin to use.
  267. </ul>
  268. <br>
  269. <a name="FRM_PWMset"></a>
  270. <b>Set</b><br>
  271. <ul>
  272. <code>set &lt;name&gt; on</code><br>
  273. sets the pulse-width to 100%<br>
  274. </ul>
  275. <ul>
  276. <code>set &lt;name&gt; off</code><br>
  277. sets the pulse-width to 0%<br>
  278. </ul>
  279. <ul>
  280. <a href="#setExtensions">set extensions</a> are supported<br>
  281. </ul>
  282. <ul>
  283. <code>set &lt;name&gt; toggle</code><br>
  284. toggles the pulse-width in between to the last value set by 'value' or 'dim' and 0 respectivly 100%<br>
  285. </ul>
  286. <ul>
  287. <code>set &lt;name&gt; value &lt;value&gt;</code><br>
  288. sets the pulse-width to the value specified<br>
  289. Range is from 0 to 255 (for 8-bit resolution) (see <a href="http://arduino.cc/en/Reference/AnalogWrite">analogWrite()</a> for details)<br>
  290. </ul>
  291. <ul>
  292. <code>set &lt;name&gt; dim &lt;value&gt;</code><br>
  293. sets the pulse-width to the value specified in percent<br>
  294. Range is from 0 to 100<br>
  295. </ul>
  296. <ul>
  297. <code>set &lt;name&gt; dimUp</code><br>
  298. increases the pulse-width by 10%<br>
  299. </ul>
  300. <ul>
  301. <code>set &lt;name&gt; dimDown</code><br>
  302. decreases the pulse-width by 10%<br>
  303. </ul>
  304. <a name="FRM_PWMget"></a>
  305. <b>Get</b><br>
  306. <ul>
  307. N/A
  308. </ul><br>
  309. <a name="FRM_PWMattr"></a>
  310. <b>Attributes</b><br>
  311. <ul>
  312. <li>restoreOnStartup &lt;on|off&gt;</li>
  313. <li>restoreOnReconnect &lt;on|off&gt;</li>
  314. <li><a href="#IODev">IODev</a><br>
  315. Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
  316. than one FRM-device defined.)
  317. </li>
  318. <li><a href="#eventMap">eventMap</a><br></li>
  319. <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
  320. </ul>
  321. </ul>
  322. <br>
  323. =end html
  324. =cut