| 8 | |
| 9 | # self is the thread object this runs in |
| 10 | def sample_task(self): |
| 11 | |
| 12 | ### Setup code here |
| 13 | |
| 14 | |
| 15 | ### End Setup code |
| 16 | |
| 17 | # Pass control back to RTOS |
| 18 | yield |
| 19 | |
| 20 | # Thread loop |
| 21 | while True: |
| 22 | |
| 23 | # Check messages |
| 24 | msgs = self.recv() |
| 25 | for msg in msgs: |
| 26 | |
| 27 | ### Handle messages by adding elifs to this |
| 28 | if msg.type == pyRTOS.QUIT: # This allows you to |
| 29 | # terminate a thread. |
| 30 | # This condition may be removed if |
| 31 | # the thread should never terminate. |
| 32 | |
| 33 | ### Tear down code here |
| 34 | print("Terminating task:", self.name) |
| 35 | print("Terminated by:", msg.source) |
| 36 | |
| 37 | ### End of Tear down code |
| 38 | return |
| 39 | elif msg.type == REQUEST_DATA: # Example message, using user |
| 40 | # message types |
| 41 | self.send(pyRTOS.Message(SENT_DATA, |
| 42 | self, |
| 43 | msg.source, |
| 44 | "This is data")) |
| 45 | ### End Message Handler |
| 46 | |
| 47 | ### Work code here |
| 48 | # If there is significant code here, yield periodically |
| 49 | # between instructions that are not timing dependent. |
| 50 | # Also, it is generally a good idea to yield after |
| 51 | # I/O commands that return instantly but will require |
| 52 | # some time to complete (like I2C data requests). |
| 53 | # Each task must yield at least one per iteration, |
| 54 | # or it will hog all of the CPU, preventing any other |
| 55 | # task from running. |
| 56 | |
| 57 | |
| 58 | |
| 59 | ### End Work code |
| 60 | |
| 61 | yield [pyRTOS.timeout(0.5)] |
| 62 | |
| 63 | if self.name == "task1": |
| 64 | target = "task2" |
| 65 | else: |
| 66 | target = "task1" |
| 67 | |