98_WIFILED.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # ############################################################################
  2. #
  3. # FHEM Modue for WLAN based LED Driver
  4. #
  5. # ############################################################################
  6. #
  7. # This is absolutley open source. Please feel free to use just as you
  8. # like. Please note, that no warranty is given and no liability
  9. # granted
  10. #
  11. # ############################################################################
  12. #
  13. # we have the following readings
  14. # state on|off
  15. #
  16. # ############################################################################
  17. #
  18. # we have the following attributes
  19. # timeout the timeout in seconds for the TCP connection
  20. #
  21. # ############################################################################
  22. # we have the following internals (all UPPERCASE)
  23. # RED last red value
  24. # GREEN last green value
  25. # BLUE last blue value
  26. # IP the IP of the device
  27. # RGB for the RGB Values of color-picker
  28. # MODE the last number of the built in modes
  29. #
  30. # ############################################################################
  31. # TODO: the speed of the animation: 0xBB, ??, ??, 0x44
  32. # ############################################################################
  33. package main;
  34. use strict;
  35. use warnings;
  36. use IO::Socket;
  37. # include this for the self-calling timer we use later on
  38. use Time::HiRes qw(gettimeofday);
  39. # for the color picker module
  40. use Color;
  41. use SetExtensions;
  42. # ----------------------------------------------------------------------------
  43. # Initialisation routine called upon start-up of FHEM
  44. # ----------------------------------------------------------------------------
  45. sub WIFILED_Initialize( $ ) {
  46. my ($hash) = @_;
  47. # the commands we provide to FHEM
  48. # installs the respecitive call-backs for FHEM. The call back in quotes
  49. # must be realised as a sub later on in the file
  50. $hash->{DefFn} = "WIFILED_Define";
  51. $hash->{SetFn} = "WIFILED_Set";
  52. $hash->{GetFn} = "WIFILED_Get";
  53. # the attributes we have. Space separated list of attribute values in
  54. # the form name:default1,default2
  55. $hash->{AttrList} = "timeout loglevel:0,1,2,3,4,5,6 " . $readingFnAttributes;
  56. # initialize the color picker
  57. FHEM_colorpickerInit();
  58. }
  59. # ----------------------------------------------------------------------------
  60. # Definition of a module instance
  61. # called when defining an element via fhem.cfg
  62. # ----------------------------------------------------------------------------
  63. sub WIFILED_Define( $$ ) {
  64. my ( $hash, $def ) = @_;
  65. my $name = $hash->{NAME};
  66. my @a = split("[ \t][ \t]*", $def);
  67. # do we have the right number of arguments?
  68. if( @a != 3 ) {
  69. Log( $attr{$name}{loglevel}, "WIFILED_Define: falsche Anzahl an Argumenten" );
  70. return( "wrong syntax: define <name> WIFILED <serverip> " );
  71. }
  72. # preset the internals
  73. $hash->{IP} = $a[ 2 ];
  74. $hash->{RED} = 255;
  75. $hash->{GREEN} = 255;
  76. $hash->{BLUE} = 255;
  77. $hash->{MODE} = 0;
  78. if( !defined( $attr{$name}{timeout} ) ) {
  79. $attr{$name}{timeout} = 2;
  80. }
  81. if( !defined( $attr{$name}{loglevel} ) ) {
  82. $attr{$name}{loglevel} = 4;
  83. }
  84. # Preset our readings if undefined
  85. my $tn = TimeNow();
  86. if( !defined( $hash->{READINGS}{state}{VAL} ) ) {
  87. $hash->{READINGS}{state}{VAL} = "?";
  88. $hash->{READINGS}{state}{TIME} = $tn;
  89. }
  90. if( !defined( $hash->{READINGS}{rgb}{VAL} ) ) {
  91. $hash->{READINGS}{rgb}{VAL} = "FFFFFF";
  92. $hash->{READINGS}{rgb}{TIME} = $tn;
  93. }
  94. if( !defined( $hash->{READINGS}{RGB}{VAL} ) ) {
  95. $hash->{READINGS}{RGB}{VAL} = "FFFFFF";
  96. $hash->{READINGS}{RGB}{TIME} = $tn;
  97. }
  98. if( !defined( $hash->{READINGS}{dim}{VAL} ) ) {
  99. $hash->{READINGS}{dim}{VAL} = 100;
  100. $hash->{READINGS}{dim}{TIME} = $tn;
  101. }
  102. return( undef );
  103. }
  104. # ----------------------------------------------------------------------------
  105. # Set of a module
  106. # called upon set <name> cmd, arg1, arg2, ....
  107. # ----------------------------------------------------------------------------
  108. sub WIFILED_Set( $@ ) {
  109. my ( $hash, $name, $cmd, @arg ) = @_;
  110. # check if we have received a command
  111. if( !defined( $cmd ) ) {
  112. return( "$name: set needs at least one parameter" );
  113. }
  114. my $cmdList = "" .
  115. "on off next:noArg prev:noArg mode " .
  116. "color brightness:slider,0,1,100 dim:slider,0,1,100 " .
  117. "rgb:colorpicker,RGB ";
  118. # now parse the commands
  119. if( $cmd eq "?" ) {
  120. # this one should give us a drop down list
  121. return SetExtensions( $hash, $cmdList, $name, $cmd, @arg );
  122. } elsif( $cmd eq "on" ) {
  123. WIFILED_Write( $hash, "\x{CC}\x{23}\x{33}" );
  124. # and update the state
  125. readingsSingleUpdate( $hash,
  126. "state",
  127. "on",
  128. 1 );
  129. Log( GetLogLevel( $name, 4 ), "$name switched on" );
  130. } elsif( $cmd eq "off" ) {
  131. WIFILED_Write( $hash, "\x{CC}\x{24}\x{33}" );
  132. # and update the state
  133. readingsSingleUpdate( $hash,
  134. "state",
  135. "off",
  136. 1 );
  137. Log( GetLogLevel( $name, 4 ), "$name switched off" );
  138. } elsif( $cmd eq "run" ) {
  139. WIFILED_Write( $hash, "\x{CC}\x{21}\x{33}" );
  140. } elsif( $cmd eq "stop" ) {
  141. WIFILED_Write( $hash, "\x{CC}\x{22}\x{33}" );
  142. } elsif( $cmd eq "next" ) {
  143. my $offset = 38;
  144. my $mode = $offset + $hash->{MODE};
  145. if( $mode > ( $offset + 20 ) ) {
  146. $mode = $offset;
  147. }
  148. $hash->{MODE} = $mode;
  149. WIFILED_Write( $hash, "\x{BB}" . chr( $mode ) . "\x{19}\x{44}" );
  150. } elsif( $cmd eq "prev" ) {
  151. my $offset = 38;
  152. my $mode = $offset + $hash->{MODE};
  153. if( $mode < $offset ) {
  154. $mode = $offset + 20;
  155. }
  156. $hash->{MODE} = $mode;
  157. WIFILED_Write( $hash, "\x{BB}" . chr( $mode ) . "\x{19}\x{44}" );
  158. } elsif( $cmd eq "mode" ) {
  159. my $offset = 38;
  160. if( ( $arg[ 0 ] < 0 ) || ( $arg[ 0 ] > 19 ) ) {
  161. my $msg = "WIFILED_Set: wrong mode number given";
  162. Log( $attr{$name}{loglevel}, $msg );
  163. return( $msg );
  164. }
  165. $hash->{MODE} = $arg[ 0 ] + $offset;
  166. WIFILED_Write( $hash, "\x{BB}" . chr( $hash->{MODE} ) . "\x{19}\x{44}" );
  167. } elsif( $cmd eq "color" ) {
  168. if( @arg != 3 ) {
  169. my $msg = "WIFILED_Set: wrong number of arguments for set color";
  170. Log( $attr{$name}{loglevel}, $msg );
  171. return( $msg );
  172. } else {
  173. $hash->{RED} = $arg[ 0 ];
  174. $hash->{GREEN} = $arg[ 1 ];
  175. $hash->{BLUE} = $arg[ 2 ];
  176. WIFILED_Write( $hash, "\x{56}" .
  177. chr( $arg[ 0 ] ) .
  178. chr( $arg[ 1 ] ) .
  179. chr( $arg[ 2 ] ) .
  180. "\x{AA}" );
  181. WIFILED_UpdateRGB( $hash );
  182. Log( GetLogLevel( $name, 4 ), "$name set to " .
  183. "$hash->{RED} $hash->{GREEN} $hash->{BLUE}" );
  184. }
  185. } elsif( $cmd eq "rgb" ) {
  186. if( @arg != 1 ) {
  187. my $msg = "WIFILED_Set: wrong number of arguments for set rgb";
  188. Log( $attr{$name}{loglevel}, $msg );
  189. return( $msg );
  190. } else {
  191. $arg[ 0 ] = uc( $arg[ 0 ] );
  192. my @colors = ( $arg[ 0 ] =~ m/..?/g );
  193. if( @colors != 3 ) {
  194. my $msg = "WIFILED_Set: malformed RBG given.";
  195. Log( $attr{$name}{loglevel}, $msg );
  196. return( $msg );
  197. } else {
  198. $hash->{RED} = hex( $colors[ 0 ] );
  199. $hash->{GREEN} = hex( $colors[ 1 ] );
  200. $hash->{BLUE} = hex( $colors[ 2 ] );
  201. WIFILED_Write( $hash, "\x{56}" .
  202. chr( $hash->{RED} ) .
  203. chr( $hash->{GREEN} ) .
  204. chr( $hash->{BLUE} ) .
  205. "\x{AA}" );
  206. WIFILED_UpdateRGB( $hash );
  207. Log( GetLogLevel( $name, 4 ), "$name set to " .
  208. "$hash->{RED} $hash->{GREEN} $hash->{BLUE}" );
  209. }
  210. }
  211. } elsif( ( $cmd eq "brightness" ) || ( $cmd eq "dim" ) ) {
  212. if( @arg != 1 ) {
  213. my $msg = "WIFILED_Set: wrong number of arguments for set brightness";
  214. Log( $attr{$name}{loglevel}, $msg );
  215. return( $msg );
  216. } else {
  217. # brightness is in percent (0..100)
  218. my $bright = $arg[ 0 ];
  219. my $red = $hash->{RED};
  220. my $green = $hash->{GREEN};
  221. my $blue = $hash->{BLUE};
  222. if( ( $bright > 100 ) || ( $bright < 0 ) ) {
  223. $bright = 50;
  224. }
  225. # we need to determine what is 100%
  226. my $upscale = 0;
  227. # what is the smallest upscale factor?
  228. if( $red > $green ) {
  229. if( $red > $blue ) {
  230. $upscale = 255 / $red;
  231. } else {
  232. $upscale = 255 / $blue;
  233. }
  234. } else {
  235. if( $green > $blue ) {
  236. $upscale = 255 / $green;
  237. } else {
  238. $upscale = 255 / $blue;
  239. }
  240. }
  241. $red = int( ( ( $red * $upscale ) * $bright ) / 100 );
  242. $blue = int( ( ( $blue * $upscale ) * $bright ) / 100 );
  243. $green = int( ( ( $green * $upscale ) * $bright ) / 100 );
  244. WIFILED_Write( $hash, "\x{56}" . chr( $red ) . chr( $green ) .
  245. chr( $blue ) . "\x{AA}\n" );
  246. $hash->{RED} = $red;
  247. $hash->{GREEN} = $green;
  248. $hash->{BLUE} = $blue;
  249. WIFILED_UpdateRGB( $hash );
  250. readingsSingleUpdate( $hash, "dim", $bright, 1 );
  251. }
  252. } else {
  253. # my $msg = "WIFILED_Set: unsupported command given $cmd @arg";
  254. # Log( $attr{$name}{loglevel}, $msg );
  255. # return( $msg );
  256. return SetExtensions ($hash, $cmdList, $name, $cmd, @arg);
  257. }
  258. return( undef );
  259. }
  260. # ----------------------------------------------------------------------------
  261. # Get of a module
  262. # called upon get <name> arg1
  263. # ----------------------------------------------------------------------------
  264. sub WIFILED_Get( $@ ) {
  265. my ($hash, @a) = @_;
  266. my $name = $a[ 0 ];
  267. if( int( @a ) != 2 ) {
  268. my $msg = "WIFILED_Get: wrong number of arguments";
  269. Log( $attr{$name}{loglevel}, $msg );
  270. return( $msg );
  271. }
  272. if( ( $a[ 1 ] eq "rgb" ) || ( $a[ 1 ] eq "RGB" ) ) {
  273. return( ReadingsVal( "$name", "rgb", "F0F0F0" ) );
  274. } elsif( ( $a[ 1 ] eq "dim" ) || ( $a[ 1 ] eq "DIM" ) ) {
  275. return( ReadingsVal( "$name", "dim", "50" ) );
  276. } else {
  277. my $msg = "WIFILED_Get: unkown argument";
  278. Log( $attr{$name}{loglevel}, $msg );
  279. return( $msg );
  280. }
  281. }
  282. # ----------------------------------------------------------------------------
  283. # write something to the WIFI LED
  284. # ----------------------------------------------------------------------------
  285. sub WIFILED_Write( $$ ) {
  286. my ( $hash, $out ) = @_;
  287. my $name = $hash->{NAME};
  288. my $s = new IO::Socket::INET( PeerAddr => $hash->{IP},
  289. PeerPort => 5577,
  290. Proto => 'tcp',
  291. Timeout => int( $attr{$name}{timeout} ) );
  292. if( defined $s ) {
  293. my $res = "";
  294. $s->autoflush( 1 );
  295. print $s $out;
  296. close( $s );
  297. }
  298. }
  299. # ----------------------------------------------------------------------------
  300. # Update the RGB Readings for the color picker
  301. # ----------------------------------------------------------------------------
  302. sub WIFILED_UpdateRGB( $ ) {
  303. my ( $hash, @rest ) = @_;
  304. my $name = $hash->{NAME};
  305. my $buf = sprintf( "%02X%02X%02X",
  306. $hash->{RED},
  307. $hash->{GREEN},
  308. $hash->{BLUE} );
  309. readingsSingleUpdate( $hash,
  310. "RGB",
  311. $buf,
  312. 1 );
  313. readingsSingleUpdate( $hash,
  314. "rgb",
  315. $buf,
  316. 1 );
  317. CommandTrigger( "", "$hash->{NAME} RGB: $buf" );
  318. return;
  319. }
  320. # DO NOT WRITE BEYOND THIS LINE
  321. 1;
  322. =pod
  323. =begin html
  324. <a name="WIFILED"></a>
  325. <h3>WIFILED</h3>
  326. <ul>
  327. Define a WIFI LED Controler.
  328. <br><br>
  329. <a name="WIFILEDdefine"></a>
  330. <b>Define</b>
  331. <ul>
  332. <code>define &lt;name&gt; WIFILED &lt;ip&gt;</code>
  333. <br><br>
  334. Example:
  335. define myled WIFILED 192.168.38.17
  336. <ul>
  337. </ul>
  338. </ul>
  339. <br>
  340. <a name="WIFILEDset"></a>
  341. <b>Set</b>
  342. <ul>
  343. <code>set &lt;name&gt; &lt;value&gt</code><br>
  344. Set any value.
  345. </ul>
  346. <br>
  347. <a name="WIFILEDget"></a>
  348. <b>Get</b> <ul>N/A</ul><br>
  349. <a name="WIFILEDattr"></a>
  350. <b>Attributes</b>
  351. <ul>
  352. <li><a name="setList">setList</a><br>
  353. Space separated list of commands, which will be returned upon "set name ?",
  354. so the FHEMWEB frontend can construct a dropdown and offer on/off
  355. switches. Example: attr WIFILEDName setList on off
  356. </li>
  357. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  358. </ul>
  359. <br>
  360. </ul>
  361. =end html
  362. =cut