Skip to content

Instantly share code, notes, and snippets.

View hbhargava7's full-sized avatar

Hersh Bhargava hbhargava7

View GitHub Profile
@hbhargava7
hbhargava7 / UIRefreshControlImplementation.m
Last active May 22, 2016 06:29
Code to implement a UIRefreshControl in a UITableView (Objective C).
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */
//Header
UIRefreshControl *refreshControl;
//Main Load
refreshControl = [[UIRefreshControl alloc] init];
[tableView addSubview:refreshControl];
//Main Begin Refresh
[refreshControl beginRefreshing];
[refreshControl setHidden:false];
@hbhargava7
hbhargava7 / ProcessTiming.m
Last active May 22, 2016 06:28
Code to compute and log the execution time in seconds for some process in Objective C.
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */
NSDate *processStart = [NSDate date];
//Process Here!
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"Process Execution Time (s): %f", executionTime);
@hbhargava7
hbhargava7 / SendDictionaryToScript.m
Last active May 22, 2016 06:28
Asynchronously send an NSDictionary to a server PHP (or other) script using JSON with a completion handler (Objective C).
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */
- (void)sendDictionary:(NSDictionary *)dictionary toScript:(NSString *)scriptName completion:(void (^) (id response))completionBlock
{
//Convert the passed dictionary into JSON.
NSString *JSONStringfromDictionary = [self JSONfromDictionary:dictionary];
//Set up the query string for the Server with the API Key inline.
NSString *URLString = YOUR_URL_STRING;
NSString *queryString = [NSString stringWithFormat:@"%@key=%@", URLString, YOUR_OPTIONAL_API_KEY];
//Create a URLRequest using the query string.
@hbhargava7
hbhargava7 / OptimizeAndEncodeUIImage.m
Last active May 22, 2016 06:28
Iteratively optimize and compress UIImage and Convert to Base64 for Server Upload (Objective C).
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */
- (NSString *)compressAndEncodeToBase64String:(UIImage *)image
{
//Scale Image to some width (xFinal)
float ratio = image.size.width/image.size.height;
float xFinal = 700; //Desired image width
float yFinal = xFinal/ratio;
UIImage *scaledImage = [self imageWithImage:image scaledToSize:CGSizeMake(xFinal, yFinal)];
@hbhargava7
hbhargava7 / DynamicallyExpandingForm.js
Created May 22, 2016 06:28
Add additional (removable) fields to HTML form when user tabs out of last field (jQuery).
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */
//Keep track of dynamically added text inputs in 'i'
var i = $('#items p').size() + 1;
//Keyup function to add text input on tab release from last field
$(document).on('keyup', 'input[type="text"]:last', function (e) {
//check if the released key was the tab key
if (e.which == 9) {
//append additional fields to items div
var targetDiv = $('#items');
@hbhargava7
hbhargava7 / ESCArduinoDriver.ino
Last active May 22, 2016 06:55
Basics of controlling an electronic speed controller with an Arduino.
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */
#include <Servo.h>
Servo esc1; //Create a servo object to control a servo.
int xPotPin = 0; //Pins for analog potentiometers.
int esc1Value;
void setup() {
Serial.begin(9600);
@hbhargava7
hbhargava7 / SieveOfEratosthenes.py
Created May 22, 2016 06:58
Basic, concise primes sieve in Python.
# Created by Hersh Bhargava of H2 Micro (www.h2micro.com)
#sieve for primes between 1 and cap.
def prime_sieve(cap):
n = cap + 1
nprime = set()
prime = []
for i in range(2, n):
print("Sieve Operation Completion: " + str((i/n) * 100))
if i in nprime:
@hbhargava7
hbhargava7 / RandomStringWithLength.m
Last active November 11, 2016 23:50
NSString extension to generate a random alphanumeric string of length n.
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */
@interface NSString (Randomize)
+ (NSString *)randomStringWithLength:(int)length;
@end
@implementation NSString (Randomize)
+ (NSString *)randomStringWithLength:(int)length
{
@hbhargava7
hbhargava7 / rPi_backup.sh
Last active August 9, 2016 18:24
Script to automate Raspberry Pi server backups.
### Modified by Hersh Bhargava from user Jaac on Raspberry Pi Forum ###
#!/bin/bash
# Setup directories
SUBDIR=pi_system_backups
DIR=/hdd/$SUBDIR
echo "Starting Raspberry Pi backup process."
# First check if pv package is installed, if not, install it first