My Daily Gist | Ferdinand Silva


A6 GSM/GPRS Module Autoresponder (Auto Reply) Sample Code In Python

 
#!/usr/bin/env python
import serial
import time
import re
if __name__ == "__main__":
ser = serial.Serial('/dev/cu.SLAB_USBtoUART', 9600, timeout=1)
isOk = False
try:
while True:
if not isOk:
ser.write(b"AT+CMGF=1\r\n")
data = ser.readline()
if not isOk:
if data.strip() == "OK":
isOk = True
if isOk:
if data.strip() != "":
print "%s\n" % data.strip()
if re.search('^.*\+CMT', data.strip()):
number = data.strip().replace("+CMT: ", "").split(",")[0].replace('"', '').replace("+63", "0")
ser.write(b'AT+CMGS="%s"\r\n' % number)
time.sleep(1)
ser.write(b'I am busy right now. This is an automated reply.')
ser.write(b'\x1a\r\n')
time.sleep(1)
except KeyboardInterrupt:
ser.close()
view raw test.py hosted with ❤ by GitHub

Evaluate Reverse Polish Notation & Shunting-yard Algorithm Sample Code (Golang)

 
package main
import (
"fmt"
"bufio"
"os"
"errors"
"unicode"
"strconv"
)
type Lexer struct {
Text string
}
func (lexer Lexer) GenerateToken() ([]string, error) {
var tokenArray []string
for x := 0; x < len(lexer.Text); x++ {
currentChar := string(lexer.Text[x])
if(currentChar == "\n" || currentChar == " ") {
//ignore newline and space
continue
} else if(unicode.IsDigit([]rune(currentChar)[0])) {
if(len(tokenArray) == 0) {
tokenArray = append(tokenArray, currentChar)
} else {
if(unicode.IsDigit([]rune(tokenArray[len(tokenArray) - 1])[0])) {
tokenArray[len(tokenArray) - 1] += currentChar
} else {
tokenArray = append(tokenArray, currentChar)
}
}
} else if(currentChar == "(" || currentChar == ")" || currentChar == "+" || currentChar == "-" || currentChar == "/" || currentChar == "*") {
if(currentChar == "+" || currentChar == "-" || currentChar == "/" || currentChar == "*") {
if(len(tokenArray) > 0) {
if(tokenArray[len(tokenArray)-1] == "+" || tokenArray[len(tokenArray)-1] == "-" || tokenArray[len(tokenArray)-1] == "/" || tokenArray[len(tokenArray)-1] == "*") {
return tokenArray, errors.New("Syntax Error")
}
}
}
tokenArray = append(tokenArray, currentChar)
} else {
return tokenArray, errors.New("Syntax Error")
}
}
return tokenArray, nil
}
type Parser struct {
TokenArray []string
}
func (parser *Parser) Parse() error {
precedences := map[string] int{"+": 0, "-": 0, "/": 1, "*": 1} //order of precedences
var operatorStack []string
var outputQueue []string
if(len(parser.TokenArray) > 0) {
if(parser.TokenArray[0] == "+" || parser.TokenArray[0] == "-" || parser.TokenArray[0] == "/" || parser.TokenArray[0] == "*") {
//syntax error if the first token is an operator
return errors.New("Syntax error")
}
if(parser.TokenArray[len(parser.TokenArray)-1] == "+" || parser.TokenArray[len(parser.TokenArray)-1] == "-" || parser.TokenArray[len(parser.TokenArray)-1] == "/" || parser.TokenArray[len(parser.TokenArray)-1] == "*") {
//syntax error if the last token is an operator
return errors.New("Syntax error")
}
//shunting-yard below
//While there are tokens to be read:
for len(parser.TokenArray) > 0 {
//Read a token
currentToken := parser.TokenArray[0]
parser.TokenArray = append(parser.TokenArray[:0], parser.TokenArray[1:]...) //pop the first element
if(unicode.IsDigit([]rune(currentToken)[0])) {
//If it's a number add it to queue
outputQueue = append(outputQueue, currentToken)
}
if(currentToken == "+" || currentToken == "-" || currentToken == "/" || currentToken == "*") {
//If it's an operator
for true {
if(len(operatorStack) > 0) {
if(precedences[operatorStack[len(operatorStack) - 1]] >= precedences[currentToken]) {
/*
While there's an operator on the top of the stack with greater precedence:
Pop operators from the stack onto the output queue
*/
outputQueue = append(outputQueue, operatorStack[len(operatorStack) - 1])
operatorStack = operatorStack[:len(operatorStack)-1]
} else {
break
}
} else {
break
}
}
//Push the current operator onto the stack
operatorStack = append(operatorStack, currentToken)
}
if(currentToken == "(") {
//If it's a left bracket push it onto the stack
operatorStack = append(operatorStack, currentToken)
}
if(currentToken == ")") {
//If it's a right bracket
if(len(operatorStack) > 0) {
for true {
if(operatorStack[len(operatorStack) - 1] != "(") {
/*
While there's not a left bracket at the top of the stack:
Pop operators from the stack onto the output queue.
*/
outputQueue = append(outputQueue, operatorStack[len(operatorStack) - 1])
operatorStack = operatorStack[:len(operatorStack)-1]
} else {
//Pop the left bracket from the stack and discard it
operatorStack = operatorStack[:len(operatorStack)-1]
break
}
if(len(operatorStack) == 0) {
return errors.New("Syntax error")
}
}
} else {
return errors.New("Syntax error")
}
}
}
for len(operatorStack) > 0 {
if(operatorStack[len(operatorStack) - 1] == "(") {
return errors.New("Syntax error")
}
//While there are operators on the stack, pop them to the queue
outputQueue = append(outputQueue, operatorStack[len(operatorStack) - 1])
operatorStack = operatorStack[:len(operatorStack)-1]
}
//now the outputQueue contains the reverse polish notation
//read reverse polish notation below
if(len(outputQueue) > 0) {
var stack []string
for len(outputQueue) > 0 {
currentToken := outputQueue[0]
outputQueue = append(outputQueue[:0], outputQueue[1:]...) //pop the first element
if(currentToken == "+" || currentToken == "-" || currentToken == "/" || currentToken == "*") {
//get right operand
rightOperand, _ := strconv.Atoi(stack[len(stack)-1])
stack = stack[:len(stack)-1]
//get left operand
leftOperand, _ := strconv.Atoi(stack[len(stack)-1])
stack = stack[:len(stack)-1]
var result int
if(currentToken == "+") {
//addition
result = leftOperand + rightOperand
} else if(currentToken == "-") {
//substraction
result = leftOperand - rightOperand
} else if(currentToken == "/") {
//division
result = leftOperand / rightOperand
} else {
//assuming multiplication
result = leftOperand * rightOperand
}
stack = append(stack, strconv.Itoa(result))
} else {
stack = append(stack, currentToken)
}
}
//print evaluated result
fmt.Println("> " + stack[0])
}
}
return nil
}
func main() {
/*
Sample Run
==========
> ((15/(7-(1+1)))*3)-(2+(1+1))
> 5
*/
for true {
reader := bufio.NewReader(os.Stdin)
fmt.Print("> ")
text, _ := reader.ReadString('\n')
//convert to token
lxr := Lexer{Text: text}
tokenArray, tokenErr := lxr.GenerateToken()
if(tokenErr != nil) {
fmt.Println("> " + tokenErr.Error())
} else {
//parse token
parser := Parser{TokenArray: tokenArray}
parseErr := parser.Parse()
if(parseErr != nil) {
fmt.Println("> " + parseErr.Error())
}
}
}
}
view raw main.go hosted with ❤ by GitHub

Line Tracing Bot Pinout

 

Ang Bayan Kong Sinilangan Tune With Arduino In Pure C (Passive Buzzer)

 
/*
Thanks to this post: https://balau82.wordpress.com/2014/10/15/using-a-buzzer-with-arduino-in-pure-c/
*/
#ifndef F_CPU
#define F_CPU 16000000UL //set frequency
#endif
#define __DELAY_BACKWARD_COMPATIBLE__
#include <avr/io.h>
#include <util/delay.h>
int tones[17] = {
247,
330,
330,
370,
330,
370,
392,
392,
392,
440,
494,
440,
440,
370,
392,
370,
330
};
int toneDelays[17] = {
500,
500,
500,
200,
250,
450,
500,
500,
200,
250,
450,
500,
500,
200,
250,
450,
1100
};
void tone(unsigned long frequency) {
unsigned long timer_frequency = (F_CPU + (256/2)) / 256;
OCR0A = (timer_frequency + ((frequency*2)/2)) / (frequency*2);
TCCR0A = _BV(COM0A0) | _BV(WGM01);
}
int main() {
DDRD |= _BV(DDD6); //digital pin 6
TCCR0B = _BV(CS02);
for(;;) {
for(int x=0; x < 17; x++) {
tone(tones[x]);
_delay_ms(toneDelays[x]);
}
_delay_ms(400);
}
return 0;
}
view raw test.c hosted with ❤ by GitHub

Pure C Language In Arduino Uno (Sample Code 3) [Digital Read]

 
/*
Compiling and uploading to Arduino Uno (Sample Code 3) [Digital Read]
=====================================================================
avr-gcc -Os -mmcu=atmega328p -c test.c
avr-gcc -mmcu=atmega328p -o test.elf test.o
avr-objcopy -O ihex -R .eeprom test.elf test.hex
avrdude -F -V -c arduino -p ATMEGA328P -P /dev/cu.usbmodem1421 -b 115200 -U flash:w:test.hex
*/
#ifndef F_CPU
#define F_CPU 16000000UL //set frequency
#endif
#include <avr/io.h>
#include <util/delay.h>
#define MS_DELAY 500 //500 milliseconds
int main() {
DDRB |= _BV(DDB0); //set pin 8 to output
DDRB &= ~_BV(DDB1); //set pin 9 to input
for(;;) {
int val = PINB & _BV(PB1);
if(val == 0) {
PORTB |= _BV(PORTB0); //set pin 8 to high
} else {
PORTB &= ~_BV(PORTB0); //set pin 8 to low
}
_delay_ms(MS_DELAY); //delay
}
return 0;
}
view raw test.c hosted with ❤ by GitHub