Shop OBEX P1 Docs P2 Docs Learn Events
Save_me/Restore_me on my uSD — Parallax Forums

Save_me/Restore_me on my uSD

EmptyBitEmptyBit Posts: 72
edited 2010-12-29 02:18 in Propeller 1
Impressive or Elegant it may not be. Functional it is! If anything it shows what a noob can do with this platform.

In an effort to reduce writes to the SD every count, I thought it wiser to place the save function on a watchdog or pre-brownout detector. I've seen a couple examples here, but not much super simple without extra components. I am not sure if that takes a dedicated cog or how to implement it reliably yet. I imagine a cap to sustain it while it writes its last hurra and another pin to detect the event.

If you choose to run the code. Probably best to comment out the restore_me call first. The commented out D1-D4 values and setcolor should be uncommented and/or changed at will for the first run. Once the data.txt file is created after a first wheel press, these will need to be commented out again. Then the code can be stored to the eeprom and subsequent changes to the color or count digit settings will return as expected on reboot.

Pretty much what I set out to do. Now my little counter display increments, voices and retains color and count settings on the full version. This could morph into a batch counter as well, counting down from a preset with lights, bells, whistles or horns.

Comments, pointers etc. much appreciated!



''***************************************
''*  Voiced Counter & Display           *
''*                                     *
''*                                     *
''*  Copyright (c) 12/27/2010           *               
''*  See end of file for terms of use.  *               
''***************************************


CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
  _stack = ($2800 + 100) >> 2   'accomodate display memory and stack

  x_tiles = 16
  y_tiles = 10

  paramcount = 14       
  bitmap_base = $5000
  display_base = $5000

  lines = 5
  thickness = 2
  bufferlen = 400


VAR

  long  mousex, mousey
 
  long  tv_status     '0/1/2 = off/visible/invisible           read-only
  long  tv_enable     '0/? = off/on                            write-only
  long  tv_pins       '%ppmmm = pins                           write-only
  long  tv_mode       '%ccinp = chroma,interlace,ntsc/pal,swap write-only
  long  tv_screen     'pointer to screen (words)               write-only
  long  tv_colors     'pointer to colors (longs)               write-only               
  long  tv_hc         'horizontal cells                        write-only
  long  tv_vc         'vertical cells                          write-only
  long  tv_hx         'horizontal cell expansion               write-only
  long  tv_vx         'vertical cell expansion                 write-only
  long  tv_ho         'horizontal offset                       write-only
  long  tv_vo         'vertical offset                         write-only
  long  tv_broadcast  'broadcast frequency (Hz)                write-only
  long  tv_auralcog   'aural fm cog                            write-only

  word  screen[x_tiles * y_tiles]

  long  old1, old2, old3, old4
  long  D1, D2, D3, D4, setcolor
  byte  settings[5]
  long  f_ext
  byte  f_name1
  byte  f_name2[2]
  byte  f_name_ext[6]
  byte buff[bufferlen]
OBJ

  wpl : "WavePlayerSdAccess"
  tv    : "tv" 
  gr    : "graphics" 
  mouse : "mouse"
  
  

PUB start | i, dx, dy, numx

wpl.start(0, 10, 11, @buff, bufferLen)

Restore_me                                    'Fetch settings off SD card from "data.txt"

  longmove(@tv_status, @tvparams, paramcount)
  tv_screen := @screen
  tv_colors := @colors
  tv.start(@tv_status)
  

  'init colors
  'setcolor := 1                                 
  setcolor := settings[4]-48                   'convert setcolor ASCII chr. back to dec
  colorswap(setcolor)
  

  'init tile screen                                         
  repeat dx from 0 to tv_hc - 1
    repeat dy from 0 to tv_vc - 1
      screen[dy * tv_hc + dx] := display_base >> 6 + dy + dx * tv_vc 



  'start and setup graphics
  gr.start
  gr.setup(16, 10, 128, 80, bitmap_base)      '(x_tiles, y_tiles, x_origin, y_origin, base_ptr)

  'start mouse
  mouse.start(24, 25)

  'D1 := D2 := D3 := D4 := "0"
  longmove(@old1, @D1, 4)


  'set font
  gr.textmode(10,16,6,5)      'textmode(x_scale, y_scale, spacing, justification)
                                 'my counter set at (10,16,6,5)

  'D1 := D2 := D3 :=D4 :="9"                               
  repeat
    'clear only changed area
    gr.colorwidth(0,8)    'text color and width  0,8
    dx := 70
    if old2<>D2
      dx += 65
    if old3<>D3
       dx += 65
    if old3<>D3
       dx += 65
    gr.box(135-dx,-72, dx, 145)

    'draw digits
    gr.colorwidth(2,8)    'text color and width  2, 8
    gr.text(-95,0,@D4)
    gr.text(-30,0,@D3)
    gr.text(35,0,@D2)
    gr.text(100,0,@D1)
   
    longmove(@old1, @D1, 4)   'copy new digits to old
    
   
    repeat until mouse.button(0)                  'Left Mouse, would be count up 1
                                                'Not enough mouse buttons for debugging in whole
      if mouse.button(1)                         'Count will be M'sw input,
          repeat while mouse.button(1)          'Clear and Voice counter will be P.button
          setcolor := setcolor+1                    '
          if setcolor > 8                             'save will be auto at every powerdown
             setcolor := 1 
         colorswap(setcolor)  
      if mouse.button(2)                                  'Mouse wheel click for now           
        Save_me                                             'Save settings to SD card
        

    repeat while mouse.button(0)
 

Pri colorswap(NUMX)                                                         
       
 tv_colors := @colors[setcolor-1]

return
Pri Save_me |i                                            'eventually triggered via a power down watchdog?
    repeat i from 0 to 3                                  'Store array settings to the SD card
      bytemove(@settings[i],@D1[i],1)                     'Save D1-D4
  settings[4]:= setcolor+48                               'convert setcolor to an ascii chr. for storing
  wpl.popen(string("data.txt"),"w")                       'Open the file name Data.txt
  wpl.pwrite(@settings, 5)                                'write settings to it
  wpl.pclose                                              'close data file
return 
Pri Restore_me|i                                         'Restore count and color settings on reboot

wpl.popen(string("data.txt"),"r")                        'open stored data array settings to the SD card
wpl.pread(@settings, 5)                                  'read data into settings array
wpl.pclose                                               'close data file
  repeat i from 0 to 4                                   'extract settings to D1-4 and setcolor ASCII chr.
   bytemove(@D1[i],@settings[i],1)
return        
DAT

tvparams                long    0               'status
                        long    1               'enable
                        long    %001_0101       'pins
                        long    %0000           'mode
                        long    0               'screen
                        long    0               'colors
                        long    x_tiles         'hc
                        long    y_tiles         'vc
                        long    10              'hx
                        long    1               'vx
                        long    0               'ho
                        long    0               'vo
                        long    0               'broadcast
                        long    0               'auralcog
                      
                                                           '   Foreground     Background color 
colors                  long    $00_1B_00_07                     '1B=Blue        07=White
                        long    $00_07_00_1B                     '07=White       1B=Blue
                        long    $00_8E_00_1B                     '8E=Yellow      1B=Blue
                        long    $00_1B_00_8E                     '8E=Blue        1B=Yellow 
                        long    $00_8E_00_02                     '8E=Yellow      02=Black
                        long    $00_02_00_8E                     '02=Black       8E=Yellow
                        long    $00_07_00_02                     '07=White       02=Black
                        long    $00_02_00_07                     '02=Black       07=White     
                        
                      
Sign In or Register to comment.