Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Example use of "FirstTime" library function: GUI i

 
Post new topic   Reply to topic    Forum Index -> Files
Author Message
kranenborg



Joined: 27 Jul 2009
Posts: 51
Location: Groningen, The Netherlands

Posted: 11 March 2015, 6:47 AM    Post subject: Example use of "FirstTime" library function: GUI i Reply with quote

I have found the "FirstTime" Library function (which returns True the first time it is called after a program download, otherwise False) very useful in an application (MahYong score calculator) where an educational but rather lengthy start-up message about the use of a device only needs to be shown a few times for novice users, after which the message can be skipped since the user is assumed to be sufficiently knowledgeable and only the short welcome greeting needs to be shown.

In the following code excerpt a class "DisplayGUI" has a constructor in which a counter is managed whose task is to keep track of how often the class has been called after a program download. The first time after download the counter (in persistent EEPROM memory) is set to a predefined value, subsequent calls will decrement its value if it is larger than zero.

The class method "ShownEnoughTimes" returns False if the counter is non-zero, True otherwise, and can be used by the application. This method is then used in the "Greeting" method which prints the start-up message, which may be long or short depending on the download history. No user intervention is needed.



Code:

Class DisplayGUI

   Private Const DownloadCounterAddress as Integer = 100
   Private Const maxIntro as Byte = 10
   Dim downloadStatus as Byte
   
   
   Public Sub _Create()

      If FirstTime Then
         Call PersistentPoke(maxIntro,DownloadCounterAddress)
      Else
         downloadStatus = PersistentPeek(DownloadCounterAddress)
         If downloadStatus > 0 then
            downloadStatus = downloadStatus - 1
            Call PersistentPoke(downloadStatus,DownloadCounterAddress)
         End If
      End If

   End Sub


   Public Function ShownEnoughTimes() as Boolean

      downloadStatus = PersistentPeek(DownloadCounterAddress)
      If downloadStatus > 0 Then
         ShownEnoughTimes = False
      Else
         ShownEnoughTimes = True
      End If

   End Function

' ...
' ...

' Public method for start-up message:

   Public Sub Greeting()
 
   If Not ShownEnoughTimes Then
       'Generate introductory & educational message here
   End If       

   'Standard greeting message here

   End Sub

End Class
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2982
Location: Portland, OR

Posted: 11 March 2015, 15:03 PM    Post subject: Reply with quote

Very nice.

One suggestion would be to use a Persistent class variable, e.g.
Code:

Class DisplayGUI
   Private Static DownloadCounter as Persistent Integer
   ...
End Class

The advantage of doing this is that it allows the compiler to assign the address in Persistent memory rather than managing the address manually.
Back to top
kranenborg



Joined: 27 Jul 2009
Posts: 51
Location: Groningen, The Netherlands

Posted: 11 March 2015, 22:11 PM    Post subject: Reply with quote

Thanks Don,

Somehow I had forgotten that ZBASIC variables also can be located in persistent memory; some other systems treat EEPROM as a separate memory bank accessible only through Peek/Poke-like operations. Your proposal will make the solution much more straightforward and simple.

Just one question: to me the necessity of the Static keyword is not entirely clear to me: Since the variable is in persistent memory it will have a permanent location, and the constructor will create a proper initialization as well, effectively making it a "static-like" variable?

BR, Jurjen
Back to top
kranenborg



Joined: 27 Jul 2009
Posts: 51
Location: Groningen, The Netherlands

Posted: 11 March 2015, 22:47 PM    Post subject: Reply with quote

Found the asnwer amyself after compiling and then reading section 4.17 of the manual: Class members with the persistent attribute must be declared static ...

/Jurjen
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2982
Location: Portland, OR

Posted: 11 March 2015, 23:32 PM    Post subject: Reply with quote

kranenborg wrote:
Class members with the persistent attribute must be declared static ...
The Static attribute on a class data member means that the member belongs to the class itself and that it won't be replicated for each instance of the class. The Static attribute requirement for Persistent data items is due to the fact that there is no mechanism for run-time allocation of Persistent memory.
Back to top
kranenborg



Joined: 27 Jul 2009
Posts: 51
Location: Groningen, The Netherlands

Posted: 12 March 2015, 8:29 AM    Post subject: Reply with quote

Thanks Don, the solution then simplifies to the following code, tested on a ZX-328nu:

Code:

Option Objects

Class DisplayGUI

   Private Const maxIntro as Byte = 4
   Private Static DownloadCounter as Persistent Byte
     
     
   Public Sub _Create()

      If FirstTime Then
         DownloadCounter = MaxIntro
      Else
         If DownloadCounter  > 0 then
            DownloadCounter = DownloadCounter - 1
         End If
      End If

   End Sub


   Public Function ShownEnoughTimes() as Boolean

      ShownEnoughTimes = Not Cbool(DownloadCounter)

   End Function

 ' ...
 ' ...

 ' Public method for start-up message:

   Public Sub Greeting()
   
      If Not ShownEnoughTimes Then
         Debug.Print "Educational message: So many times left: "; CStr(DownloadCounter)
      End If       

      Debug.Print "Standard greeting message here"

   End Sub

 End Class
 
 
 Sub Main()
 
   Dim GUIobject As DisplayGUI = _Create
 
   Call GUIobject.Greeting
   
 End Sub
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> Files Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2017 Elba Corp. All rights reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group