Last active
August 29, 2015 14:03
-
-
Save MartinSadovy/b0a92f122d95ba7d8778 to your computer and use it in GitHub Desktop.
php controller for https://github.com/bahbka/pebble-my-data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/Pebble.php'; | |
$pebble = new Pebble; | |
$pebble->onUp(function ($pebble) { | |
$pebble->notify(); | |
$pebble->setResult("UP!"); | |
}); | |
$pebble->onBottom(function ($pebble) { | |
$pebble->notify(); | |
$pebble->setResult("Down \n" . $pebble->getLatitude() . "\n" . $pebble->getLongitude()); | |
}); | |
$pebble->onMiddle(function ($pebble) { | |
$pebble->notify(); | |
$pebble->setResult("Mid \n" . $pebble->getPebbleId()); | |
}); | |
$pebble->render(function ($pebble) { | |
$pebble->setResult("Hi!"); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @see api: https://github.com/bahbka/pebble-my-data/blob/master/README.md | |
* @author Martin Sadový | |
*/ | |
class Pebble | |
{ | |
/** | |
* Font | |
*/ | |
const GOTHIC_14 = 1, | |
GOTHIC_14_BOLD = 2, | |
GOTHIC_18 = 3, | |
GOTHIC_18_BOLD = 4, | |
GOTHIC_24 = 5, | |
GOTHIC_24_BOLD = 6, | |
GOTHIC_28 = 7, | |
GOTHIC_28_BOLD = 8; | |
/** | |
* Default refresh time | |
*/ | |
const DEFAULT_REFRESH_TIME = 300; | |
/** | |
* Return data | |
* @var array | |
*/ | |
protected $data = array(); | |
/** | |
* @var bool | |
*/ | |
protected $called = FALSE; | |
function __construct() | |
{ | |
$this->data = array( | |
"content" => "Nothing to response", | |
"refresh" => self::DEFAULT_REFRESH_TIME, | |
"vibrate" => 0, | |
"font" => self::GOTHIC_24_BOLD, | |
"scroll" => 1, | |
"light" => 0, | |
"blink" => 0, | |
"updown" => 0, | |
); | |
} | |
/** | |
* Return pebbleId | |
* @return mixed | |
*/ | |
public function getPebbleId() | |
{ | |
return $_SERVER["HTTP_PEBBLE_TOKEN"]; | |
} | |
/** | |
* Return GPS latitude | |
* @return null|string | |
*/ | |
public function getLatitude() | |
{ | |
return isset($_GET["lat"]) ? $_GET["lat"] : NULL; | |
} | |
/** | |
* Return GPS longitude | |
* @return null|string | |
*/ | |
public function getLongitude() | |
{ | |
return isset($_GET["lon"]) ? $_GET["lon"] : NULL; | |
} | |
/** | |
* Set theme to black | |
*/ | |
public function setThemeBlack() | |
{ | |
$this->data['theme'] = 0; | |
} | |
/** | |
* Set theme to white | |
*/ | |
public function setThemeWhite() | |
{ | |
$this->data['theme'] = 1; | |
} | |
/** | |
* Set result | |
* @param $content | |
* @param int $font | |
*/ | |
public function setResult($content, $font = self::GOTHIC_24_BOLD) | |
{ | |
$this->data['content'] = $content; | |
$this->data['font'] = $font; | |
} | |
/** | |
* Set time to next refresh | |
* @param int $seconds | |
*/ | |
public function setRefreshTime($seconds) | |
{ | |
if ($seconds < 1) { | |
$seconds = self::DEFAULT_REFRESH_TIME; | |
} | |
$this->data['refresh'] = (int) $seconds; | |
} | |
/** | |
* Set some notify user | |
* @param int $vibrate | |
* @param bool $light | |
* @param int $blink | |
*/ | |
public function notify($vibrate = 1, $light = TRUE, $blink = 0) | |
{ | |
$this->data['light'] = (int) $light; | |
$this->data['vibrate'] = (int) $vibrate; | |
$this->data['blink'] = (int) $blink; | |
} | |
/** | |
* Event on click on bottom button | |
* @param callable $callback | |
*/ | |
public function onBottom(callable $callback) | |
{ | |
if (isset($_GET["down"])) { | |
$callback($this, $_GET["down"]); | |
$this->called = TRUE; | |
} | |
$this->data['scroll'] = 0; | |
$this->data['updown'] = 1; | |
} | |
/** | |
* Event on click on up button | |
* @param callable $callback | |
*/ | |
public function onUp(callable $callback) | |
{ | |
if (isset($_GET["up"])) { | |
$callback($this, $_GET["up"]); | |
$this->called = TRUE; | |
} | |
$this->data['scroll'] = 0; | |
$this->data['updown'] = 1; | |
} | |
/** | |
* Event on click on middle button | |
* @param callable $callback | |
*/ | |
public function onMiddle(callable $callback) | |
{ | |
if (isset($_GET["select"])) { | |
$callback($this, $_GET["select"]); | |
$this->called = TRUE; | |
} | |
} | |
/** | |
* Render output and call callback when user did not click on buttom | |
* @param callable $defaultCallback | |
*/ | |
public function render(callable $defaultCallback) | |
{ | |
if (!$this->called) { | |
$defaultCallback($this); | |
} | |
echo json_encode($this->data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment