Skip to content

Instantly share code, notes, and snippets.

@dagon666
dagon666 / hk.c
Created December 31, 2013 16:23
history keys
static void _cli_history_up(void* a_ctx) {
t_cli_ctx *ctx = (t_cli_ctx *)a_ctx;
char prompt[12] = {0x00};
if ((ctx->hhead != ctx->htail) &&
strlen(ctx->history[ctx->hpos])) {
memset(ctx->cmd, 0x00, CLI_CMD_BUFFER_SIZE);
strcpy(ctx->cmd, ctx->history[ctx->hpos]);
@dagon666
dagon666 / history.c
Created December 31, 2013 16:22
history handling
static void _cli_history_add(t_cli_ctx *a_ctx) {
if (a_ctx->hhead == CLI_CMD_HISTORY_LEN &&
a_ctx->htail == CLI_CMD_HISTORY_LEN) {
a_ctx->hhead = 0;
a_ctx->htail = 0;
}
else {
a_ctx->hhead++;
a_ctx->hhead %= CLI_CMD_HISTORY_LEN;
@dagon666
dagon666 / interpreter2.c
Created December 31, 2013 16:19
interpreter2
void cli_read(t_cli_ctx *a_ctx) {
unsigned char i = 0x00;
unsigned char res = E_CMD_OK;
// if no character available - then exit
if (!CLI_IO_INPUT(&i)) return;
/// multi-code matching
if (a_ctx->mc.pos && a_ctx->mc.pos < MULTICODE_INPUT_MAX_LEN) {
@dagon666
dagon666 / cli_reinterpret.c
Created December 31, 2013 16:17
reinterpretter
static void _cli_reinterpret_cmd(t_cli_ctx *a_ctx, unsigned char a_response) {
switch(a_response) {
case E_CMD_NOT_FOUND: {
char str[] = "\r\nCommand not found";
CLI_IO_OUTPUT(str, strlen(str));
}
break;
case E_CMD_TOO_SHORT: {
char str[] = "\r\nCommand too short";
@dagon666
dagon666 / cli_interpret_cmd.c
Created December 31, 2013 10:13
cli command interpretation
typedef enum _t_cmd_status {
E_CMD_OK = 0,
E_CMD_NOT_FOUND,
E_CMD_TOO_SHORT,
E_CMD_EMPTY
} t_cmd_status;
static unsigned char _cli_interpret_cmd(t_cli_ctx *a_ctx) {
unsigned char i = 0;
@dagon666
dagon666 / cli_interpreter.c
Created December 31, 2013 10:12
cli interpreter
#define POSINC(__x) (((__x) < (CLI_CMD_BUFFER_SIZE - 1)) ? (__x + 1) : (__x))
#define POSDEC(__x) ((__x) ? ((__x) - 1) : 0)
#define KEY_CODE_BACKSPACE 0x7f
#define KEY_CODE_DELETE 0x1b
#define KEY_CODE_ENTER 0x0a
#define CLI_IO_INPUT(__data) \
linux_getc(__data)
@dagon666
dagon666 / linux_io_cli.c
Created December 31, 2013 10:10
linux tty io for CLI
unsigned char linux_getc(unsigned char *a_data) {
*a_data = getchar();
return 1;
}
unsigned char linux_putc(unsigned char *data, unsigned char a_len) {
while (a_len--) {
fputc(*data++, stdout);
}
}
@dagon666
dagon666 / cm_test.c
Created December 31, 2013 10:08
linux tty with canonical mode turned off - non blocking getchar()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
void cm_off(void)
{
struct termios t;
@dagon666
dagon666 / gpio.c
Created December 13, 2013 13:31
gpio abstraction layer for atmega mcu
#include <avr/io.h>
#include <stdint.h>
#include <util/delay.h>
typedef struct _gpio_pin {
volatile uint8_t *port;
uint8_t pin;
} gpio_pin;
@dagon666
dagon666 / soft_rtc.c
Created November 17, 2013 19:47
using timer as RTC
#include "pca.h"
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
volatile static uint8_t cnt = 0x00;
volatile static uint32_t g_epoch = 0x00;
ISR(TIMER0_COMPA_vect) {