Skip to content

Instantly share code, notes, and snippets.

View eddieespinal's full-sized avatar

Eddie Espinal eddieespinal

View GitHub Profile
@eddieespinal
eddieespinal / getIPAddress
Created May 12, 2014 19:05
Get user's IP Address iOS
static NSString* getIPAddress() {
id myhost =[NSClassFromString(@"NSHost") performSelector:@selector(currentHost)];
if (myhost) {
for (NSString* address in [myhost performSelector:@selector(addresses)]) {
if ([address rangeOfString:@"::"].location == NSNotFound) {
return address;
}
}
}
@eddieespinal
eddieespinal / NSPredicate Timestamp
Created October 13, 2014 20:34
NSPredicate compare string less than other string (NSDate)
NSPredicate *predicate = [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings) {
return ([[NSDate dateWithTimeIntervalSince1970:[[evaluatedObject timestamp] doubleValue] timeIntervalSinceDate:oldDate] < 0);
}];
@eddieespinal
eddieespinal / BytesToSize
Created November 6, 2014 22:20
Convert Bytes to KB, MB, GB, TB
function bytesToSize(bytes)
{
if (bytes == 0)
{
return "0 Byte";
}
local k = 1000;
local sizes = ["Bytes", "KB", "MB", "GB", "TB"];
local i = math.floor(math.log(bytes) / math.log(k));
return math.abs(bytes / math.pow(k, i)) + "" + sizes[i];
@eddieespinal
eddieespinal / Push Button Logic
Created December 11, 2014 18:40
Electric Imp Push button logic
// configure the button
button <- hardware.pin1;
button.configure(DIGITAL_IN_PULLUP, function() {
imp.sleep(0.05); // software debounce
local state = button.read();
// button down
if (state == 1) {
server.log("button is pressed");
}
@eddieespinal
eddieespinal / Imp Clock
Created January 14, 2015 19:02
Display Date/Time on an LCD with Electric Imp
// create an ImpLcd instance. Our LCD is connected to I2C on ports 8,9 with a base address of 0x27
lcd <- ImpLcd(hardware.i2c89, 0x27, 2, 0);
function updateClock()
{
local t = date();
// move to the first row, first column
lcd.setCursor(0, 0);
lcd.writeString(format("%02d:%02d:%02d", t.hour, t.min, t.sec));
@eddieespinal
eddieespinal / getIPAddress
Created March 11, 2015 15:50
getIPAddress iOS
#include <ifaddrs.h>
#include <arpa/inet.h>
- (NSString *)getIPAddress {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
@eddieespinal
eddieespinal / renderTimeOfDay
Created March 11, 2015 17:35
renderTimeOfDay
private function renderTimeOfDay($user){
$now = DateTime::now();
$now->timezone($user['timezone']); // convert to user's timezone
$hour = $now->time()->hour();
$result = 'night';
if($hour > 3 && $hour < 12){
$result = 'morning';
}elseif($hour < 17){
$result = 'afternoon';
}elseif($hour < 22){
@eddieespinal
eddieespinal / deviceFamily
Created March 11, 2015 18:13
deviceFamily
- (UIDeviceFamily) deviceFamily
{
NSString *platform = [self platform];
if ([platform hasPrefix:@"iPhone"]) return UIDeviceFamilyiPhone;
if ([platform hasPrefix:@"iPod"]) return UIDeviceFamilyiPod;
if ([platform hasPrefix:@"iPad"]) return UIDeviceFamilyiPad;
if ([platform hasPrefix:@"AppleTV"]) return UIDeviceFamilyAppleTV;
return UIDeviceFamilyUnknown;
}
@eddieespinal
eddieespinal / getTimeOfDay
Created March 14, 2015 21:04
Get Time Of Day, e.g. Morning, Noon, Afternoon, Evening, Night, Late Night
+ (NSString *)getTimeOfDay
{
// By: Eddie Espinal
// morning 5AM-11:59AM
// Noon 12PM-1PM
// afternoon 1PM-5:59PM
// evening 6PM-8:30PM
// Night 8:30PM-11:59PM
// Midnight 12AM
// late night 12AM-4AM
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]