Shop OBEX P1 Docs P2 Docs Learn Events
Shifting Data — Parallax Forums

Shifting Data

oodesoodes Posts: 131
edited 2013-01-18 01:54 in General Discussion
Hello Guys ,

I'm a bit of a newby so this might be a simple question for some of you I hope. Im reading data from a text file on a SD card and displaying it on an LCD and also the PST. The Data stream is 40 characters long. I have 2 text files and I choose which one to open and read by the state of a pin. If my pin is 0 I open " Bibliog.txt" and read in the first 40 characters and display it.
But I want it so that when I push the button again (active low) I read in the following 40 characters (40-79) of the text file. Do I need to shift the address >>40 times?? Apologies for the poor code , i'm a bit of a virgin

x := sdfat.popen(string("Bibliog.txt"), "r")
if x <> 0
lcd.move(1,1)
lcd.str(string("File Not Found"))
repeat

sdfat.pread(@data1,40)
sdfat.pclose
lcd.move(1,1)
lcd.str(string("Bibliog.txt"))
delay
lcd.clear
lcd.move(1,1)
lcd.str(@data1)

pst.Start(115_200)
pst.Str(@data1)
delay
lcd.clear


repeat
if INA[11]==0 ' My push button connected here
???????????????? 'here's my problem
DAT
data1 long "Bibliog.txt",0
data2 long "Cheats.txt",0

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-14 09:42
    attachment.php?attachmentid=78421&d=1297987572
  • oodesoodes Posts: 131
    edited 2013-01-14 10:16
    Thanks Mike

    Hello Guys ,

    I'm a bit of a newby so this might be a simple question for some of you I hope. Im reading data from a text file on a SD card and displaying it on an LCD and also the PST. The Data stream is 40 characters long. I have 2 text files and I choose which one to open and read by the state of a pin. If my pin is 0 I open " Bibliog.txt" and read in the first 40 characters and display it.
    But I want it so that when I push the button again (active low) I read in the following 40 characters (40-79) of the text file. Do I need to shift the address >>40 times?? Apologies for the poor code , i'm a bit of a virgin
    PUB Init1 | x , 
    
    
       
       x := sdfat.popen(string("Bibliog.txt"), "r")
       if x <> 0
          lcd.move(1,1)
          lcd.str(string("File Not Found"))
          repeat
          
       sdfat.pread(@data1,40)
       sdfat.pclose
       lcd.move(1,1)
       lcd.str(string("Bibliog.txt"))
       delay
       lcd.clear
       lcd.move(1,1)
       lcd.str(@data1)
       
       pst.Start(115_200)
       pst.Str(@data1) 
       delay
       lcd.clear
    
    
       repeat
          if  INA[11]==0
          ????????????  ' Here is the problem
    
    DAT
    data1 long "Bibliog.txt",0
    data2 long "Cheats.txt",0     
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-14 11:54
    Are you only talking about 2 groups of 40 characters from each of 2 files or is this open ended (40 bytes for each button push)?

    I ask because, if it's only 2 groups of 40 characters x 2 files, then you could just read in all 4 strings of 40 bytes during initialization and select which one you want to display. How big are these files (how many 40 byte entries)? How you implement this depends a lot on that. For example, if you have a 64K byte EEPROM, you could read both files into fixed locations in the EEPROM, then access the entries randomly very easily. If these files could be large, maybe you could use one of the SD card drivers that supports more than one file open at a time (for reading) and just open both of them and read 40 bytes at a time from both selecting which one you want each time the button is pushed.
  • oodesoodes Posts: 131
    edited 2013-01-14 12:07
    The files are 25kB and the other is 180kB. Only one file is open at a time depending on the state on selected pins. So I am reading it from the SD card
    PUB Begin 
    
    
    
    
       DIRA[16]~~   'DISPLAY ON/OFF 
       OUTA[16]~~   'DISPLAY ON
       lcd.start
       lcd.clear
       lcd.move(6,2)
       lcd.str(string("Hello Rivtec"))
       waitcnt(clkfreq * 2 + cnt )
       lcd.clear
       lcd.Home
       lcd.str(string("Mounting Card"))
       MountCard
       Delay
    
    
       if  INA[11]==0
          lcd.move(1,1)
          lcd.str(string("Bibliog.txt selected"))
          delay
          lcd.clear
          Init1
    
    
       if  INA[14]==0
          lcd.move(1,1)
          lcd.str(string("Cheats.txt selected"))
          delay
          lcd.clear
          Init2
    
    

    Basically I just read the first 40 bytes of whatever file is open and display it on the LCD and PST. If I push the button again ,clear the LCD read the next 40 bytes and display, and if pushed again the next 40 and so on. I'm just unsure how to structure the code
  • oodesoodes Posts: 131
    edited 2013-01-15 02:29
    I see that I have been opening the file reading 40 characters and closing it and then displaying it. So to read the next 40 characters I need to re-open the file but how do I locate the second batch of 40 characters? Is it possible to shift the '@data1>>40' and read it? Ive read that this '@data1' is the compile time address location so do I need to use the ''@@" symbol ??
  • oodesoodes Posts: 131
    edited 2013-01-15 05:22
    Ok , got it sorted :smile:
  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-15 07:17
    Your problem is that you need to reposition the file(s) when you switch them. I believe Kye's SD card driver supports repositioning files to a specific location (relative to the beginning of the file). FSRW 2.6 has a file position method, but it only works within a file extent (as I remember). I still think you'd be better off keeping both files open and just reading from the one selected by the I/O pin. Kye's driver supports multiple open files (by having more than one object instance defined).
  • oodesoodes Posts: 131
    edited 2013-01-18 01:54
    Maybe this will help someone with similar difficulties I had
    ''File : Project 2
    {{1)      Copy the files 'Bibliog.txt' and 'Cheats.txt' to the SD card and insert in ProGen.
      2)      Read the filenames from the Sd card and display on two differnt lines of LCD.
      3)      Using the two opto-isolated inputs of ProGen (P11 & P14), select either the upper file (P11) or lower file (P14).
      4)      Open the selected file.
      5)      If P11 is operated, display the first 80 characters of the text file on LCD and transmit to serial terminal.
            If P14 is operated, display the last 80 characters of teh text file on LCD and transmit to serial terminal.
      6)      Each time P11 is operated, display and transmit the next 80 characters from text file.
            Each time P14 is operated, display and transmit the previous 80 characters from text file.
      7)      If '~' is pressed at any stage then close the open file and return to step 2. "       }}
    
    
    
    
    CON
    
    
      _clkmode = xtal1 + pll16x                              ' set the clk mode
      _xinfreq = 5_000_000                                   ' set the incoming frequency
    
    
    
    
    OBJ                                                      ' objects being used and required
    
    
      pst: "Parallax Serial Terminal"
      lcd : "LCD_20x4_4Bit"
      sdfat: "fsrw"
    
    
    
    
    PUB Begin | r  ,d
    
    
    
    
       DIRA[16]~~                                                    ' DISPLAY ON/OFF 
       OUTA[16]~~                                                    ' DISPLAY ON
       lcd.start
       lcd.clear
       lcd.move(6,2)
       lcd.str(string("Hello Rivtec"))
       waitcnt(clkfreq * 2 + cnt )
       lcd.clear
       lcd.str(string("Mounting Card"))
       MountCard                                                     ' Call Mountcard Method
       repeat
         r := sdfat.popen(string("Bibliog.txt"), "r")                ' Open and Read the file name Bibliog.txt is present
        if r <> 0                                                    ' if other than 0 is returned 
           lcd.move(1,1)
           lcd.str(string("File Not Found"))                         ' Display File not found
           repeat
    
    
        lcd.move(1,1)
        lcd.str(string("Bibliog.txt"))                               ' Display Bibliog.txt
         
         r := sdfat.popen(string("Cheats.txt"), "r")                 ' Open and Read the file name Cheats.txt is present
        if r <> 0                                                    ' if other than 0 is returned
           lcd.move(1,3)
           lcd.str(string("File Not Found"))                         ' Display File not found
           repeat
    
    
        lcd.move(1,3)
        lcd.str(string("Cheats.txt"))                                ' Display Cheats.txt
        delay
        lcd.clear
                                                                     ' Initialize  local variable d = 0
        d:=0
        repeat until d ==1                                           ' repeat loop below until d =1
         
         if INA[11] & INA[14] ==1                                    ' If the pins P11 and P14 are high (push button switch connected , 1 when not pushed
            lcd.str(string("No file selected"))                      ' Display No file selected
            repeat while INA[11] & INA[14] ==1                       ' repeat while both switches are not pushed
           
         if  INA[11]==0                                              ' If switch on P11 is pushed
            lcd.clear                                                 
            lcd.move(1,1)
            lcd.str(string("Bibliog.txt selected"))                  ' Display Bibliog.txt selected
            delay
            lcd.clear
            Init1                                                    ' Call the Init1 method
            d:=1                                                     ' set d = 1
          
         if  INA[14]==0                                              ' If switch on P14 is pushed
            lcd.clear
            lcd.move(1,1)
            lcd.str(string("Cheats.txt selected"))                   ' Display Cheats.txt selected
            delay
            lcd.clear
            Init2                                                    ' Call Init2
            d:=1
                       
    PUB Init1  | x, Idx, a  ,c
    
    
       
       x := sdfat.popen(string("Bibliog.txt"), "r")                  ' Open the file Bibliog for read
       if x <> 0                                                     ' If other than 0 is returned
          lcd.move(1,1)
          lcd.str(string("File Not Found"))                          ' Display File not found
          repeat
          
      
       pst.Start(115_200)                                            ' Start the pst and set the baud rate to 115,200
       pst.clear                                                     ' clear the pst
       lcd.clear                                                     ' clear the lcd
       
       repeat Idx from 0 to 79                                       ' Repeat Idx from 0 to 70 (we want 80 characters which fills LCD)
         a:=sdfat.pgetc                                              ' gets a character at a time from Bibliog.txt , character is declared a local variable 'a'
         case Idx
            0  : lcd.move(1,1)                                       ' For values of Idx from 0-19 display on top line on the LCD
            20 : lcd.move(1,2)                                       ' For values of Idx from 20-39 display on 2nd line on the LCD
            40 : lcd.move(1,3)                                       ' For values of Idx from 40-59 display on 3rd line on the LCD
            60 : lcd.move(1,4)                                       ' For values of Idx from 60-79 display on 4th line on the LCD
        if a > $19 and a < $7F                                       ' if a is not a control code in ascii chart (prevents junk being diplayed that may be in text file)
          lcd.char(a)                                                ' display  'a' on  lcd 
          pst.char(a)                                                ' display  'a' on pst
        
       c:=0                                                          ' Initize 'c' local variable to 0
       repeat until c == $7E                                         ' repeat the loop below until '~' is pressed
         
        if  INA[11]==0                                               ' if P11 is pushed again 
          lcd.clear                                                  ' clear the lcd
         
          
            repeat Idx from 0 to 79                                  ' And display the next 80 charcters on the file
              a:=sdfat.pgetc
              case Idx
                  0  : lcd.move(1,1)
                  20 : lcd.move(1,2)
                  40 : lcd.move(1,3)
                  60 : lcd.move(1,4)
              if a > $19 and a < $7F
                lcd.char(a)
                pst.char(a)
        c:=pst.rxcheck                                               ' check if '~' has pushed , if so exit the loop 
      lcd.clear                                                      ' clear lcd screen
            
            
        
      
    PUB Init2 | y , Indx, b ,size ,position , c
    
    
       
       y := sdfat.popen(string("Cheats.txt"), "r")                   ' Open the file Cheats.txt for read 
       if y <> 0                                                     ' If other than 0 is returned  
          lcd.move(1,3)
          lcd.str(string("File Not Found"))                          ' Display File not found 
          repeat
    
    
       pst.Start(115_200)                                            ' Start the pst and set the baud rate to 115,200
       pst.clear                                                     ' Clear the pst
       lcd.clear                                                     ' clear the lcd
       size:=sdfat.get_filesize                                      ' get the size of the file so we can start displayed the data from the end of file
       position:=(size - 80)                                         ' declare local variable 'position' to be filesile - 80 charactes so we print the last 80 characters
       sdfat.seek(position)                                          ' seek the position, which is end of text (file - 80 characters)
       
       repeat Indx from 0 to 79                                      ' Repeat Indx from 0 to 70 (we want 80 characters which fills LCD)
         b:=sdfat.pgetc                                              ' gets a character at a time from cheats.txt , character is declared a local variable 'b'                                     
         case Indx                                                   ' case statement
              0 : lcd.move(1,1)                                      ' For values of Idx from 0-19 display on top line on the LCD
              20: lcd.move(1,2)                                      ' For values of Idx from 20-39 display on 2nd line on the LCD
              40: lcd.move(1,3)                                      ' For values of Idx from 40-59 display on 3rd line on the LCD
              60: lcd.move(1,4)                                      ' For values of Idx from 60-79 display on 4th line on the LCD
        if b > $19 and b < $7F                                       ' if b is not a control code in ascii chart (prevents junk being diplayed that may be in text file)
             lcd.char(b)                                             ' display whatever 'b' is on lcd
             pst.char(b)                                             ' display whatever 'c' is on pst
    
    
       delay
    
    
       c:=0                                                         ' Initize 'c' local variable to 0
       repeat until c == $7E
                                                                    ' repeat the loop below until '~' is pressed
        if  INA[14]==0                                              ' if P14 is pushed again
          lcd.clear                                                 ' clear the lcd
          position:=position-80                                     ' position is now (position - 80 charcters)
          sdfat.seek(position)                                      ' seek this position
          repeat Indx from 0 to 79                                  ' repeat the process 80 times
             
             b:=sdfat.pgetc                                         ' display on LCD
             case Indx
                   0 : lcd.move(1,1)                                ' In line sequence
                   20: lcd.move(1,2)
                   40: lcd.move(1,3)
                   60: lcd.move(1,4)
           if b > $19 and b < $7F                                   ' no junk
              lcd.char(b)                                           ' display on LCD
              pst.char(b)                                           ' display on Pst
        c:=pst.rxcheck                                              ' check if '~' has pushed , if so exit the loop 
      lcd.clear                                                     ' clear lcd screen 
    PUB MountCard|r
    {{ Checks if an Sd card is mounted}}        
      
       if INA[7] == 1                                               ' if card detect is high( 0 when card present)  'INA' monitors the state of a pin                         
          lcd.move(1,2)
          lcd.str(string("No Card Inserted"))                       ' Display No card inserted  
          lcd.move(1,3)
          lcd.str(string("Insert Card"))                            ' Display Insert card 
          outa[27]~                                                 ' Pin 27 set to 0 (input)
          repeat until INA[7]==0                                    ' repeat until  INA[7] goes low  i.e card present   
          reboot                                                    ' reboot when card is present
          
    
    
       if INA[2] == 1                                               'write protect same as above only now checks if card is write protected
          lcd.move(1,2)
          lcd.str(string("Card Is"))
          lcd.move(1,3)
          lcd.str(string("Write Protected."))
          repeat
    
    
                
       r := sdfat.mount(3)                                          ' BASEPIN (D0)= 3, CLK = 4, DI = 5, CS = 6
       if r <> 0                                                    ' if return value is other than 0 
          lcd.move(1,2)
          lcd.str(string("Cannot find Card"))                       ' display cannot find card 
          repeat
    
    
       lcd.move(1,2)
       lcd.str(string("Card Mounted"))                              ' if successful display Card mounted.
       delay
       lcd.clear
                     
    
    
    PUb Delay
    {{ just a delay function to save writing it all the time}}
    
    
      waitcnt(clkfreq * 2 + cnt)
      
    
Sign In or Register to comment.