Skip to content

Instantly share code, notes, and snippets.

Hersh Bhargava hbhargava7

Block or report user

Report or block hbhargava7

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@hbhargava7
hbhargava7 / rPi_backup.sh
Last active Aug 9, 2016
Script to automate Raspberry Pi server backups.
View rPi_backup.sh
### 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
@hbhargava7
hbhargava7 / RandomStringWithLength.m
Last active Nov 11, 2016
NSString extension to generate a random alphanumeric string of length n.
View RandomStringWithLength.m
/* 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 / SieveOfEratosthenes.py
Created May 22, 2016
Basic, concise primes sieve in Python.
View SieveOfEratosthenes.py
# 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 / ESCArduinoDriver.ino
Last active May 22, 2016
Basics of controlling an electronic speed controller with an Arduino.
View ESCArduinoDriver.ino
/* 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 / DynamicallyExpandingForm.js
Created May 22, 2016
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 (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 / OptimizeAndEncodeUIImage.m
Last active May 22, 2016
Iteratively optimize and compress UIImage and Convert to Base64 for Server Upload (Objective C).
View OptimizeAndEncodeUIImage.m
/* 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 / SendDictionaryToScript.m
Last active May 22, 2016
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 (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 / ProcessTiming.m
Last active May 22, 2016
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 (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 / UIRefreshControlImplementation.m
Last active May 22, 2016
Code to implement a UIRefreshControl in a UITableView (Objective C).
View UIRefreshControlImplementation.m
/* 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];
You can’t perform that action at this time.