Skip to content

Commit

Permalink
Fixed issue 30 (Character Ctrl+Z on OpenLog). Escape characters (in t…
Browse files Browse the repository at this point in the history
…his case Ctrl+Z) are no longer appended to the file. Fixed some other small terminal endline problems like for example 2 newlines displayed after each other when running terminal commands. Small indentation fixes.
  • Loading branch information
Paul Ring committed Jun 3, 2010
1 parent 64c23de commit dad9d45
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
72 changes: 45 additions & 27 deletions Code/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,21 +537,33 @@ char setting_uart_speed; //This is the baud rate that the system runs at, defaul
char setting_system_mode; //This is the mode the system runs in, default is MODE_NEWLOG
char setting_escape_character; //This is the ASCII character we look for to break logging, default is ctrl+z
char setting_max_escape_character; //Number of escape chars before break logging, default is 3

volatile uint8_t escape_chars_received; //Keeps track of the number of escape received
volatile uint8_t append_mode; //1 if in append mode
uint8_t feedback_mode = (ECHO | EXTENDED_INFO | READABLE_TEXT_ONLY);

//Circular buffer UART RX interrupt
//Is only used during append
ISR(USART_RX_vect)
{
input_buffer[read_spot] = UDR0;
read_spot++;
STAT1_PORT ^= (1<<STAT1); //Toggle the STAT1 LED each time we receive a character
if(read_spot == BUFF_LEN) read_spot = 0;
char ch = (char)UDR0;

// Do not add the escape character to the incoming buffer if this is the append mode
if ((append_mode == 1) && (ch == setting_escape_character))
escape_chars_received++;
else
{
escape_chars_received = 0;
input_buffer[read_spot] = ch;

read_spot++;
if(read_spot == BUFF_LEN) read_spot = 0;
}
}

int main(void)
{
//Do basic initialization
ioinit();

//If we are in new log mode, find a new file name to write to
Expand All @@ -570,6 +582,7 @@ int main(void)

void ioinit(void)
{
append_mode = 0;
//Init Timer0 for delay_us
//TCCR0B = (1<<CS00); //Set Prescaler to clk/1 (assume we are running at internal 1MHz). CS00=1
TCCR0B = (1<<CS01); //Set Prescaler to clk/8 : 1click = 1us(assume we are running at internal 8MHz). CS01=1
Expand Down Expand Up @@ -1202,7 +1215,11 @@ void command_shell(void)
uart_putc('!');
#endif
//print prompt
uart_puts_p(PSTR("\n>"));
char* p;
if ((p = get_cmd_arg(0)) != 0)
if (strlen(p) > 0 && count_cmd_args() == 1)
uart_putc('\n');
uart_putc('>');

//read command
char* command = buffer;
Expand Down Expand Up @@ -1410,6 +1427,8 @@ void command_shell(void)
#ifdef INCLUDE_SIMPLE_EMBEDDED
command_succedded = 1;
#endif
if ((feedback_mode & EMBEDDED_END_MARKER) == 0)
uart_putc('\n');
}
else if(strncmp_P(command_arg, PSTR("read"), 4) == 0)
{
Expand Down Expand Up @@ -1490,6 +1509,8 @@ void command_shell(void)
#ifdef INCLUDE_SIMPLE_EMBEDDED
command_succedded = 1;
#endif
if ((feedback_mode & EMBEDDED_END_MARKER) == 0)
uart_putc('\n');
}
else if(strcmp_P(command_arg, PSTR("disk")) == 0)
{
Expand Down Expand Up @@ -1524,6 +1545,8 @@ void command_shell(void)
command_succedded = 1;
#endif
}
if ((feedback_mode & EMBEDDED_END_MARKER) == 0)
uart_putc('\n');
}
#if FAT_WRITE_SUPPORT
else if(strncmp_P(command_arg, PSTR("rm"), 2) == 0)
Expand Down Expand Up @@ -1619,7 +1642,6 @@ void command_shell(void)
continue;
}

uart_putc('\n');
//Seek file position
if(!fat_seek_file(fd, &offset, FAT_SEEK_SET))
{
Expand Down Expand Up @@ -1900,6 +1922,7 @@ void command_shell(void)
uint8_t append_file(char* file_name)
{

append_mode = 1;
//search file in current directory and open it
struct fat_file_struct* fd = open_file_in_dir(fs, dd, file_name);
if(!fd)
Expand All @@ -1909,6 +1932,7 @@ uint8_t append_file(char* file_name)
uart_puts_p(PSTR("!error opening "));
uart_puts(file_name);
}
append_mode = 0;
return(0);
}

Expand All @@ -1925,6 +1949,7 @@ uint8_t append_file(char* file_name)
uart_puts(file_name);
}
fat_close_file(fd);
append_mode = 0;
return(0);
}

Expand All @@ -1940,14 +1965,12 @@ uint8_t append_file(char* file_name)

sbi(STAT1_PORT, STAT1); //Turn on indicator LED

char escape_chars_received = 0;

escape_chars_received = 0;
read_spot = 0;
checked_spot = 0;

//Clear circular buffer
for(uint16_t i = 0 ; i < BUFF_LEN ; i++)
input_buffer[i] = 0;
memset((uint8_t*)input_buffer, 0, BUFF_LEN);

//Start UART buffered interrupts
UCSR0B |= (1<<RXCIE0); //Enable receive interrupts
Expand All @@ -1966,7 +1989,10 @@ uint8_t append_file(char* file_name)
uint16_t timeout_counter = 0;

while(checked_spot == read_spot)
{
{
if(escape_chars_received >= setting_max_escape_character) //Check if we received any escape characters
break;

if( ++timeout_counter > 5000 )
{
timeout_counter = 0;
Expand Down Expand Up @@ -2021,26 +2047,17 @@ uint8_t append_file(char* file_name)
//delay_ms(1);
}
}

delay_ms(1); //Hang out while we wait for the interrupt to occur and advance read_spot
}

if(input_buffer[checked_spot] == setting_escape_character) //Scan for escape character
if(escape_chars_received >= setting_max_escape_character) //Check if we received any escape characters
{
escape_chars_received++;

if(escape_chars_received == setting_max_escape_character)
{
//Disable interrupt and we're done!
cli();
UCSR0B &= ~(1<<RXCIE0); //Clear receive interrupt enable

break;
}
//Disable interrupt and we're done!
cli();
UCSR0B &= ~(1<<RXCIE0); //Clear receive interrupt enable
break;
}
else
escape_chars_received = 0;


checked_spot++;

if(checked_spot == (BUFF_LEN/2)) //We've finished checking the first half the buffer
Expand Down Expand Up @@ -2101,7 +2118,8 @@ uint8_t append_file(char* file_name)
uart_puts_p(PSTR("Done!\n"));
#endif
uart_puts_p(PSTR("~")); //Indicate a successful record

append_mode = 0;

return(1); //Success!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* SparkFun OpenLog test program
*
* @version 1.0 April, 2010
* @version 1.1 June, 2010
* @author Paul Ring - hexor@coolbox.se
*/

Expand Down Expand Up @@ -48,7 +48,6 @@ public static void main() {
}

// Initialize the card

if (!openLog.openLogInit()) {
System.out.println("*** Error: OpenLog cannot be initialized *** ");
System.out.println("Stopping...");
Expand Down Expand Up @@ -208,4 +207,4 @@ public static void listFiles(OpenLog openLog, StringBuffer buffer) {
openLog.listDirectoryEnd();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
* current directory.
*
* 2. Open a file. Returns value true/false. Read the file contents into buffer <buffer>. Please note that
* if you are only using 2 pins (no handshaking) to connect the Javelin to OpenLog you will have to think
* since you are only using 2 pins (no handshaking) to connect the Javelin to OpenLog you will have to think
* that the UART buffer is 256 bytes (that according to the Javelin Stamp documentation version 1.0, page 188).
* Do not try to read more than that or you will most likely loose data. My suggestion is that you start with
* a small value, e.g. 50 bytes and increase this after your needs.
Expand All @@ -77,12 +77,12 @@
*
* 4. Delete file <file name>. Returns true/false.
*
* 5. This function will trigger a OpemLog hardware reset Please note that after you calling this function you *must*
* also call openLogInit() in order to set some parameters used internally in the OpenLog internal firmware.
* 5. This function will trigger a OpenLog hardware reset Please note that after you are calling this function you *must*
* also call openLogInit() in order to set some parameters used internally in the OpenLog firmware.
*
* 6. Returns the corresponding FileInfo class containing file information for <filename> like the file size.
* 6. Returns the corresponding FileInfo class containing file information for <file name> like the file size.
*
* 7. Create a directory and enter that directory. Please note that this will invalidate any directory listing or open file.
* 7. Create a directory and enter that directory. Please note that this will invalidate any directory listing or open file
* handle.
*
* 8. This function is performing a "cd .." command on the OpenLog. You will have to keep track yourself of where in the
Expand All @@ -95,7 +95,7 @@
* function.
*
* @author Paul Ring - hexor@coolbox.se
* @version 1.0 - June, 2010
* @version 1.01 - June, 2010
*/

public class OpenLog {
Expand All @@ -108,7 +108,7 @@ public class FileInfo {
public Int32 filePosition = new Int32(0,0); // File position
}

public class StringSplit {
private class StringSplit {
public int startIndex; // Points to first character in command line argument
public int endIndex; // Pointes to the last character in command line argument

Expand All @@ -133,7 +133,7 @@ public void reset() {
private final static char CR = '\r'; // Carriage return
private static final char EscChar = (char)0x1A; // Default Ctrl-Z

private boolean dirListingStarted = false; // Used to indicate a directory listning
private boolean dirListingStarted = false; // Used to indicate that a directory listning is started
private int dirCountFiles = -1; // Number of files in current directory
private int dirListingIndex = -1; // Current file index
private StringBuffer opBuffer = new StringBuffer(32);
Expand All @@ -155,7 +155,7 @@ public OpenLog(Uart txUart,
stringSplit[i] = new StringSplit();
}
}
//Returns the number of command line arguments

private int countSplitStrings()
{
int count = 0;
Expand Down Expand Up @@ -345,10 +345,7 @@ private boolean openLogExecute(String command, StringBuffer reply, char promptCh
public static boolean isNumeric(char ch) {
return ((ch >= '0') && (ch <= '9') || (ch == '-') || (ch == '+'));
}
/**
* Starts a directory listning of files (internal function).
* @return true if the directory listing could be started or false otherwise
*/

private boolean listDirStart() {

// Calling the "dirInfoInvalid" function will invalidate
Expand All @@ -362,7 +359,7 @@ private boolean listDirStart() {
// Add the characters
opBuffer.clear();
for (int i = stringSplit[1].startIndex; i < stringSplit[1].endIndex; i++) {
if (!isNumeric(ch[i])) {break; /*enough here, we are not execpting anything more*/}
if (!isNumeric(ch[i])) {break;} // enough here, we are not execpting anything more
opBuffer.append(ch[i]);
}

Expand Down Expand Up @@ -406,7 +403,7 @@ public FileInfo listDirectoryNextEntry() {
// Add the file size
opBuffer.clear();
for (int i = stringSplit[1].startIndex; i < stringSplit[1].endIndex; i++) {
if (!isNumeric(ch[i])) {break; /*enough here, we are not execpting anything more*/}
if (!isNumeric(ch[i])) {break;} // enough here, we are not excepting anything more
opBuffer.append(ch[i]);
}

Expand All @@ -430,9 +427,9 @@ public boolean listDirectoryStart() {
if (dirListingStarted) {return false;}
int openLogInit = 0;

// @Note: We are trying to open a file - if that is failing then try to init
// the memory card. It might be that the memory card has been ejected for a while
// or there is some other problem - either way; restart it!
// @Note: We are trying to start a directory listing - if that is failing then try to init
// the memory card. It might be that the memory card has been ejected or there is
// some other problem - either way; restart it!
while (!(dirListingStarted = listDirStart())) {
if (!openLogRestart()) { break; } // Restart the card
openLogInit(); // Try to initialize
Expand Down Expand Up @@ -495,15 +492,14 @@ public boolean readFile(StringBuffer buffer,
buffer.clear();

opBuffer.clear();
opBuffer.append("read "); // Read command
opBuffer.append(fileInfo.fileName.toString()); // File name
opBuffer.append("read "); // Read command
opBuffer.append(fileInfo.fileName.toString()); // File name

opBuffer.append(" ");
opBuffer.append(fileInfo.filePosition.toString()); // File position
opBuffer.append(" ");
opBuffer.append(length); // Length to read

fileInfo.filePosition.add(length); // Keep track of the file position
opBuffer.append(length); // Length to read
fileInfo.filePosition.add(length); // Keep track of the file position

// Get the data from the OpenLog
return (openLogExecute(opBuffer.toString(), buffer, shellReady));
Expand Down Expand Up @@ -540,7 +536,7 @@ public boolean createDir(String dirName) {
opBuffer.append("md ");
opBuffer.append(dirName);

return openLogExecute(opBuffer.toString(), null, shellReady);
return (openLogExecute(opBuffer.toString(), null, shellReady));
}
public boolean prevDir() {
if (!openLogAlive()) { return false; }
Expand Down Expand Up @@ -603,7 +599,7 @@ public FileInfo fileSize(String fileName) {
opBuffer.clear();
for (int i = 0; i < length; i++) {
char ch = tempBuffer.charAt(i);
if (!isNumeric(ch)) { // enough here, we are not execpting anything more
if (!isNumeric(ch)) { // enough here, we are not excepting anything more
break;
}
opBuffer.append(ch);
Expand Down Expand Up @@ -663,4 +659,4 @@ public boolean openLogInit() {

return true;
}
}
}

0 comments on commit dad9d45

Please sign in to comment.