
/*
 * shellCmds.c
 *
 *  Created on: 1 Oct 2019
 *      Author: Mike
 */


#include <stdio.h>
#include <string.h>

#include "ch.h"
#include "hal.h"
//#include "test.h"
#include "shell.h"
#include "evtimer.h"
#include "chprintf.h"

#include "useLidar.h"

#include "shellCmds.h"


static void cmd_mem(BaseChannel *chp, int argc, char *argv[]) {
	size_t n, size , largest;

	(void) argv;
	if (argc > 0) {
		chprintf(chp, "Usage: mem\r\n");
		return;
	}
	n = chHeapStatus(NULL, &size, &largest);
	chprintf(chp, "core free memory : %u bytes\r\n", chCoreGetStatusX());
	chprintf(chp, "heap fragments   : %u \r\n",n);
	chprintf(chp, "largest fragment : %u \r\n",largest);
	chprintf(chp, "heap free total  : %u bytes\r\n", size);
}

static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) {
	  static const char *states[] = {CH_STATE_NAMES};
	  thread_t *tp;

	  (void)argv;
	  if (argc > 0) {
	    shellUsage(chp, "threads");
	    return;
	  }
	  chprintf(chp, "stklimit    stack     addr refs prio     state         name\r\n" SHELL_NEWLINE_STR);
	  tp = chRegFirstThread();
	  do {
	#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE)
	    uint32_t stklimit = (uint32_t)tp->wabase;
	#else
	    uint32_t stklimit = 0U;
	#endif
	    chprintf(chp, "%08lx %08lx %08lx %4lu %4lu %9s %12s" SHELL_NEWLINE_STR,
	             stklimit, (uint32_t)tp->ctx.sp, (uint32_t)tp,
	             (uint32_t)tp->refs - 1, (uint32_t)tp->prio, states[tp->state],
	             tp->name == NULL ? "" : tp->name);
	    tp = chRegNextThread(tp);
	  } while (tp != NULL);
	}


static void cmd_pll (BaseChannel *chp, int argc, char *argv[])
{

	chprintf(chp,"pll ");

	if(argc >= 2)
	{
		if(strncmp(argv[0],"gain",4)==0)
		{
	    int v = atoi(argv[0]);
	    v=v==0?1:v;
	//    setGain(v);
	    chprintf(chp,"setGain(%d)\r\n",v);
		}


	}
	else
	{
		chprintf(chp,"no args\r\n");
	}


}






const ShellCommand shellCommands[] = { { "mem", cmd_mem }, { "threads",
		cmd_threads }, { "pll", cmd_pll }, { "lidar", shellLidar },  { NULL, NULL } };


