summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 051449c019470692217f2bb039955ffd11fe7411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "libs/tty.h"
#include "libs/utils.h"
#include "libs/gpio.h"
#include "libs/interrupts.h"
#include "libs/clock.h"

#define MOTD "WELCOME TO RP2040 TTY\n\n\r"
#define PROMPT "rp2040> "
#define HELP "\tblink\t\tmake led blink for almost 1s\n\r"

char cmd[64];
char *cmdptr=cmd;

// Execute command from cmd buffer
// This is a test function which is buggy
void exec(){
  if(cmd == cmdptr)
	return;
  else if(!strncmp(cmd, "blink", 5))
	gpio_blink_led(1);
  else if(!strncmp(cmd, "help", 4))
	tty_putstr(HELP);
  else if(cmdptr != cmd)
	tty_putstr("Unknown command (see help)\n\r");
}

void main(){
  // Finishing boot
  interrupts_init();
  xosc_init();
  gpio_init();
  tty_init();


  // REPL
  // TODO: Handling arrows etc.
  // Pressing arrow freezes the shell
  char c=tty_getchar();
  tty_putstr(MOTD);
  tty_putstr(PROMPT);
  while(1){
	c=tty_getchar();
	if(c=='\r'){
	  tty_putstr("\n\r");
	  exec();
	  tty_putstr(PROMPT);
	  cmdptr=cmd;
	}
	else if (c ==0x8){
	  if(cmdptr-cmd){
		tty_putstr("\b \b"); // Erase last character
		cmdptr--;
	  }
	}
	else if(c>=32 && c <= 127){ // Printable char
	  *cmdptr=c;
	  cmdptr++;
	  tty_putchar(c);
	}
  }

  return;
}