Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
Add Color flow abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Langlais committed Apr 2, 2021
1 parent de9e052 commit 106b3bb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Interfaces/YeePHPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public function toggle(): self;
*/
public function setColor(int $color, array $params): self;

/**
* Start a color flow
*
* @param array $flowExpression Array of expressions, they must be profide duration (ms), mode (1, 2 or 7), value (color temperature or rgb hexa) and bright (0 - 100) in order
* @param string $action The action when flow is finished
* @return $this
*/
public function startColorFlow(array $flowExpression, string $action): self;


/**
* Define the desired light brightness.
*
Expand Down
80 changes: 79 additions & 1 deletion src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class YeePHP implements YeePHPInterface
'set_power',
'set_default',
'set_ct_abx',
'set_hsv'
'set_hsv',
'start_cf'
];

/**
Expand Down Expand Up @@ -73,6 +74,15 @@ class YeePHP implements YeePHPInterface
'hsv'
];

/**
* The list of allowed color flow actions
*/
public const ALLOWED_FLOW_ACTIONS = [
"recover",
"stay",
"turnoff"
];

/**
* The default fade effect
*/
Expand All @@ -83,6 +93,11 @@ class YeePHP implements YeePHPInterface
*/
public const DEDAULT_FADE_DELAY = 300;

/**
* The default color flow action
*/
public const DEDAULT_FLOW_ACTION = "recover";

/**
* The socket connected to the light
*
Expand Down Expand Up @@ -216,6 +231,34 @@ public function setColor(int $color, array $params = []): self
return $this;
}

/**
* @inheritDoc
*/
public function startColorFlow(array $flowExpression, string $action = self::DEDAULT_FLOW_ACTION): self
{
if (count($flowExpression) == 0)
throw new Exception('Flow expression can\'t be empty !');

if (!in_array($action, self::ALLOWED_FLOW_ACTIONS, true))
throw new Exception('Invalid action type ' . $action . ' available actions : ( ' . implode(" , ", self::ALLOWED_FLOW_ACTIONS) . ' )');

try {
self::array_every(fn ($expression) => $this->checkExpression($expression), $flowExpression);
} catch (Exception $e) {
throw new Exception('Invalid flow expression ! ' . $e->getMessage());
}

$params = [
count($flowExpression),
array_search($action, self::ALLOWED_FLOW_ACTIONS),
implode(",", array_merge(...$flowExpression))
];

$this->createJob('start_cf', $params);

return $this;
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -520,6 +563,41 @@ private function checkFadeParams(array $params, string $defaultFadeEffect = self
return $params;
}

private function checkExpression($expression)
{
if (!is_array($expression))
throw new Exception("Expression must be an aray !");

if (count($expression) !== 4)
throw new Exception("Expression must have 4 values !");

$duration = $expression[0];
$mode = $expression[1];
$value = $expression[2];
$bright = $expression[3];

if (!in_array($mode, [1, 2, 7], true))
throw new Exception('Invalid expression mode ' . $mode . ' available modes : ( ' . implode(" , ", [1, 2, 7]) . ' )');

if (!is_integer($duration))
throw new Exception('Invalid expression duration ' . $duration . ', must be an integer !');

if (!is_integer($value))
throw new Exception('Invalid expression value ' . $value . ', must be an integer !');

if (!is_integer($bright))
throw new Exception('Invalid expression bright ' . $bright . ', must be an integer !');

if ($bright != -1 && ($bright < 1 || $bright > 100))
throw new Exception('Invalid expression bright ' . $bright . ', must -1 to be skipped or be range from 1 to 100 !');

if ($mode == 1 && ($value < 0 || $value > 16777215))
throw new Exception('Invalid expression value ' . $value . ', must be range from 0 to 16777215 !');

if ($mode == 2 && ($value < 1700 || $value > 6500))
throw new Exception('Invalid expression value ' . $value . ', must be range from 1700 to 6500 !');
}

/**
* Create a job for color changing
*
Expand Down

0 comments on commit 106b3bb

Please sign in to comment.