Skip to content

Instantly share code, notes, and snippets.


Hersh Bhargava hbhargava7

View GitHub Profile
hbhargava7 /
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 ###
# Setup directories
echo "Starting Raspberry Pi backup process."
# First check if pv package is installed, if not, install it first
hbhargava7 / RandomStringWithLength.m
Last active November 11, 2016 23:50
NSString extension to generate a random alphanumeric string of length n.
View RandomStringWithLength.m
/* Created by Hersh Bhargava of H2 Micro ( */
@interface NSString (Randomize)
+ (NSString *)randomStringWithLength:(int)length;
@implementation NSString (Randomize)
+ (NSString *)randomStringWithLength:(int)length
hbhargava7 /
Created May 22, 2016 06:58
Basic, concise primes sieve in Python.
# Created by Hersh Bhargava of H2 Micro (
#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 / ESCArduinoDriver.ino
Last active May 22, 2016 06:55
Basics of controlling an electronic speed controller with an Arduino.
View ESCArduinoDriver.ino
/* Created by Hersh Bhargava of H2 Micro ( */
#include <Servo.h>
Servo esc1; //Create a servo object to control a servo.
int xPotPin = 0; //Pins for analog potentiometers.
int esc1Value;
void setup() {
hbhargava7 / DynamicallyExpandingForm.js
Created May 22, 2016 06:28
Add additional (removable) fields to HTML form when user tabs out of last field (jQuery).
View DynamicallyExpandingForm.js
/* Created by Hersh Bhargava of H2 Micro ( */
//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 / OptimizeAndEncodeUIImage.m
Last active May 22, 2016 06:28
Iteratively optimize and compress UIImage and Convert to Base64 for Server Upload (Objective C).
View OptimizeAndEncodeUIImage.m
/* Created by Hersh Bhargava of H2 Micro ( */
- (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 / 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).
View SendDictionaryToScript.m
/* Created by Hersh Bhargava of H2 Micro ( */
- (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 *queryString = [NSString stringWithFormat:@"%@key=%@", URLString, YOUR_OPTIONAL_API_KEY];
//Create a URLRequest using the query string.
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.
View ProcessTiming.m
/* Created by Hersh Bhargava of H2 Micro ( */
NSDate *processStart = [NSDate date];
//Process Here!
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"Process Execution Time (s): %f", executionTime);
hbhargava7 / UIRefreshControlImplementation.m
Last active May 22, 2016 06:29
Code to implement a UIRefreshControl in a UITableView (Objective C).
View UIRefreshControlImplementation.m
/* Created by Hersh Bhargava of H2 Micro ( */
UIRefreshControl *refreshControl;
//Main Load
refreshControl = [[UIRefreshControl alloc] init];
[tableView addSubview:refreshControl];
//Main Begin Refresh
[refreshControl beginRefreshing];
[refreshControl setHidden:false];