Created
January 16, 2016 12:10
-
-
Save anonymous/4eec1e939dcdeceed136 to your computer and use it in GitHub Desktop.
TinyTxt - a minimal text interpreter based on Ward Cunningham's Txtzyme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TinyTxt_UART_1 | |
// Ken Boak January 2016 | |
// Based on Ward Cunningham's Txtzyme interpreter | |
// This is the cut down kernel with UART support - compiles to 658 bytes | |
// Lacking all features except p (print x) | |
// Additional features must be added as case statements in textEval() | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <avr/io.h> | |
// MACRO for a 62.5nS delay | |
#define NOP __asm__ __volatile__ ("nop\n\t") | |
// and then use it in code as follows | |
NOP; // delay 62.5ns on a 16MHz AtMega | |
#define F_CPU 16000000UL // define the clock frequency as 16MHz | |
#define BAUD 115200 | |
#include <util/setbaud.h> // Set up the Uart baud rate generator | |
#include <uart.h> | |
unsigned int x = 0; | |
int d = 6; // On WiNode set LED pin as 6 | |
int i, j, w, y, z ; | |
main() { | |
uart_init(); // Enable UART | |
DDRD = DDRD | B11111100; // Sets pins 2 to 7 as outputs without changing the value of pins 0 & 1, which are RX & TX | |
DDRB = DDRB | B11111111; // Port B (Pin 9 - 13) is also output | |
// PORTB &= B11111110; // Set pin 8 low | |
OK(); | |
while(1){ | |
char buf[64]; | |
textRead(buf, 64); | |
textEval(buf); | |
} | |
} | |
void textRead (char *p, byte n) { | |
byte i = 0; | |
while (i < (n-1)) { | |
// while (!Serial.available()); | |
char ch = u_getchar(); | |
if (ch == '\r' || ch == '\n') break; | |
if (ch >= ' ' && ch <= '~') { | |
*p++ = ch; | |
i++; | |
} | |
} | |
*p = 0; | |
} | |
void textEval (char *buf) { | |
unsigned int k = 0; | |
char *loop; | |
char ch; | |
while ((ch = *buf++)) { | |
switch (ch) { | |
case '0': | |
case '1': | |
case '2': | |
case '3': | |
case '4': | |
case '5': | |
case '6': | |
case '7': | |
case '8': | |
case '9': | |
x = ch - '0'; | |
while (*buf >= '0' && *buf <= '9') { | |
x = x*10 + (*buf++ - '0'); | |
} | |
break; | |
case 'p': | |
printlong(x); | |
crlf(); | |
break; | |
case '*': | |
// x=x*y; | |
break; | |
} | |
} | |
} | |
//-------------------------------------------------------------------------------------- | |
// UART Routines | |
//-------------------------------------------------------------------------------------- | |
void uart_init(void) | |
{ | |
UBRR0H = UBRRH_VALUE; | |
UBRR0L = UBRRL_VALUE; | |
UCSR0A |= _BV(U2X0); | |
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); // 8-bit data | |
UCSR0B = _BV(RXEN0) | _BV(TXEN0); // Enable RX and TX | |
} | |
void u_putchar(char c) { | |
loop_until_bit_is_set(UCSR0A, UDRE0); // Wait until data register empty | |
UDR0 = c; | |
} | |
char u_getchar(void) { | |
loop_until_bit_is_set(UCSR0A, RXC0); // Wait until data exists | |
return UDR0; | |
} | |
//----------------------------------------------------------------------------------------- | |
// Print a 16 bit int number | |
// Put a Number | |
static void printlong(unsigned short num) { | |
if (num / (unsigned short)10 != 0) printlong(num / (unsigned short)10); | |
u_putchar((char)(num % (unsigned short)10) + '0'); | |
return; | |
} | |
//---------------------------------------------------------------------------------------------------------- | |
// Print a string | |
void printstring(char *buf) | |
{ | |
} | |
//---------------------------------------------------------------------------------------------------------- | |
// Print a CR-LF | |
void crlf(void) // send a crlf | |
{ | |
u_putchar(10); | |
u_putchar(13); | |
} | |
//--------------------------------------------------------------------------------------------------------- | |
// Print OK | |
void OK(void) // send OK crlf | |
{ | |
u_putchar('O'); | |
u_putchar('K'); | |
u_putchar(10); | |
u_putchar(13); | |
} | |
//--------------------------------------------------------------------------------------------------------- | |
// Generate a multiple uS delay | |
void delay_uS(int w) | |
{ | |
int k; | |
for(k = 0; k <= w; k++) | |
{ | |
/* | |
NOP; | |
NOP; | |
*/ | |
NOP; | |
} | |
} | |
//--------------------------------------------------------------------------------------------------------- | |
void delay_mS(int z) | |
{ | |
for(j = 0; j <= z; j++) | |
{ | |
delay_uS(1000); // 1000 us delay | |
} | |
} | |
//------------------------------------That's All Folks!------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment