use object across cog
Hi to all.
I have the following code:
I want to start lcd only once and ti use it with many cogs in cuncurrency, so when with one cog writes the current time, another write temperature.
If I started lcd in main cog with LCD.START, when I start another cog it cannot use lcd. Why?
Thanx
Post Edited (Nyarlathotep) : 8/16/2008 7:04:31 AM GMT
I have the following code:
CON _CLKMODE = XTAL1 + PLL16X _XINFREQ = 5_000_000 _STACK = 1024 UkFormat = 0 'False (zero) UsaFormat = 1 'True (non-zero) VAR byte Counter byte Secs 'Seconds passed long StackClock[noparse][[/noparse]18] 'Stack space for new cog long StackWrite[noparse][[/noparse]18] OBJ LCD : "LCD" CommPort : "FullDuplexSerial" RTC: "RealTimeClock" PUB MAIN CommPort.Start(31, 30, 0, 115200) 'Start Real time clock RTC.Start RTC.SetTime(07,40,00) RTC.SetDate(16,08,08) 'LCD.START cognew(CLOCK, @StackClock) 'cognew(WRITE, @StackWrite) PUB CLOCK LCD.START repeat 'Wait for next second to pass, rather than constantly sending time/date to debug comms Secs := RTC.ReadTimeReg(0) 'Read current second repeat while Secs == RTC.ReadTimeReg(0) 'Wait until second changes 'Spit out start of message to terminal CommPort.Str(String("Time:")) 'Spit out string time to terminal CommPort.Str(RTC.ReadStrTime) LCD.MOVE(1,4) LCD.STR(RTC.ReadStrTime) 'Spit out more of message to terminal CommPort.Str(String(" Date:")) 'Spit out string date to terminal CommPort.Str(RTC.ReadStrDate(UkFormat)) 'LCD.STR(String(" ")) 'LCD.STR(RTC.ReadStrDate(UkFormat)) 'Spit out raw data message to terminal 'CommPort.Str(String(" Raw data: ")) 'Repeat Counter from 0 to 5 ' CommPort.Dec(RTC.ReadTimeReg(Counter)) ' Commport.tx(" ") CommPort.Tx(13) '<CR> PUB WRITE LCD.START LCD.MOVE(3,1) LCD.STR(STRING("Prima riga"))
I want to start lcd only once and ti use it with many cogs in cuncurrency, so when with one cog writes the current time, another write temperature.
If I started lcd in main cog with LCD.START, when I start another cog it cannot use lcd. Why?
Thanx
Post Edited (Nyarlathotep) : 8/16/2008 7:04:31 AM GMT
Comments
The basic principle requires an understanding of the difference between the VAR and DAT sections of an object. DAT variables are common to all instances of an object, but VAR variables are unique for each instance of the object.
What you may need to do is a (hopefully minimal) rewrite of the LCD object to move all the variables into the DAT section. It's not too difficult once you get the hang of it!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
if more than one cog sends data to the LCD it can happen that the serial bitstreams are messed up if two or more cogs send data at the same time
To avoid this you could use a variablebuffer. ONE cog is dedicated to send the data to the LCD.
All other cogs write their data into that buffer.
One principle could be that the LCD-cog writes the complete buffer every second (10times a second).
Another principle is to use two buffers (Inputbuffer already-written-buffer). Then the LCD-cog compares the content of the two buffers
and if he detects a difference in the content of a byte then send the new characters
and after sending it updating the already-written-buffer.
All methods inside ONE spinfile can share their global variables.
So you would just define two bytearrays
best regards
Stefan
John Abshier
I've used buffers and a new cog to manage lcd (HD44780) and now it works.
For future reviews, I've attached new code.
Bye