{"action":"created","issue":{"url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/160","labels_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/160/labels{/name}","comments_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/160/comments","events_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/160/events","html_url":"https://github.com/bbcmicrobit/micropython/pull/160","id":127469011,"number":160,"title":"Add gesture methods to MicroBitAccelerometer class.","user":{"login":"dpgeorge","id":6187689,"avatar_url":"https://avatars.githubusercontent.com/u/6187689?v=3","gravatar_id":"","url":"https://api.github.com/users/dpgeorge","html_url":"https://github.com/dpgeorge","followers_url":"https://api.github.com/users/dpgeorge/followers","following_url":"https://api.github.com/users/dpgeorge/following{/other_user}","gists_url":"https://api.github.com/users/dpgeorge/gists{/gist_id}","starred_url":"https://api.github.com/users/dpgeorge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpgeorge/subscriptions","organizations_url":"https://api.github.com/users/dpgeorge/orgs","repos_url":"https://api.github.com/users/dpgeorge/repos","events_url":"https://api.github.com/users/dpgeorge/events{/privacy}","received_events_url":"https://api.github.com/users/dpgeorge/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2016-01-19T15:20:26Z","updated_at":"2016-01-20T15:42:02Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/bbcmicrobit/micropython/pulls/160","html_url":"https://github.com/bbcmicrobit/micropython/pull/160","diff_url":"https://github.com/bbcmicrobit/micropython/pull/160.diff","patch_url":"https://github.com/bbcmicrobit/micropython/pull/160.patch"},"body":"This PR adds basic gesture methods to the accelerometer object.  They are:\r\n```python\r\naccelerometer.is_gesture('gesture name') # is this gesture currently active?\r\naccelerometer.was_gesture('gesture name') # get and reset if this gesture was active since last call\r\naccelerometer.get_gestures('gesture name') # get number of times that this gesture occurred\r\naccelerometer.reset_gestures('gesture name') # reset the number of times that this gesture occurred\r\n```\r\nSee #9 for background.\r\n\r\nThings I don't like:\r\n\r\n1. The name get_gestures is not intuitive at all.  It seems like it should return a list of gestures or something.\r\n\r\n2. I don't think the get_gestures/reset_gestures functionality is even needed.  Does one need to know how many times the device was tilted left?\r\n\r\n3. get_gestures can't tell you the order in which pending gestures occurred, just how many of a certain kind.\r\n\r\n4. There's no easy way to tell if any gesture occurred, only if a specific one did.\r\n\r\nOne way to improve/fix these things is to remove get_gestures and reset_gestures as they are now, and repurpose the get_gestures method to return a list of all pending gestures.  This could have an upper limit (eg 10) so we don't need heaps of RAM to store them.  Eg:\r\n```python\r\n>>> print(accelerometer.get_gestures())\r\n['left', 'face up', 'shake', 'face up']\r\n>>> print(accelerometer.get_gestures())\r\n[]\r\n```\r\nSo calling get_gestures() would reset the pending status of *all* gestures.  Thus was_gesture('xx') would return False after calling get_gestures (until a gesture actually occurred again).\r\n\r\nThe other problem is that for the gestures to be recognised, the underlying DAL update function must be called regularly.  Usually it's called by the DAL idle handler, but we never call that (because uPy is never idle).  So in the PR it's called only when an accelerometer method is called (this is how it's been previous to this PR, nothing has changed here).  That means that if you actually want to detect gestures you need to sit in a loop calling one of the accelerometer methods, eg:\r\n```python\r\nwhile True:\r\n    if accelerometer.was_gesture('shake'):\r\n        print('shake!')\r\n    sleep(10)\r\n```\r\nThere's currently no way to do \"other things\" and for a gesture to be detected and recorded in the background.\r\n"},"comment":{"url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/comments/173243305","html_url":"https://github.com/bbcmicrobit/micropython/pull/160#issuecomment-173243305","issue_url":"https://api.github.com/repos/bbcmicrobit/micropython/issues/160","id":173243305,"user":{"login":"dpgeorge","id":6187689,"avatar_url":"https://avatars.githubusercontent.com/u/6187689?v=3","gravatar_id":"","url":"https://api.github.com/users/dpgeorge","html_url":"https://github.com/dpgeorge","followers_url":"https://api.github.com/users/dpgeorge/followers","following_url":"https://api.github.com/users/dpgeorge/following{/other_user}","gists_url":"https://api.github.com/users/dpgeorge/gists{/gist_id}","starred_url":"https://api.github.com/users/dpgeorge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpgeorge/subscriptions","organizations_url":"https://api.github.com/users/dpgeorge/orgs","repos_url":"https://api.github.com/users/dpgeorge/repos","events_url":"https://api.github.com/users/dpgeorge/events{/privacy}","received_events_url":"https://api.github.com/users/dpgeorge/received_events","type":"User","site_admin":false},"created_at":"2016-01-20T15:42:02Z","updated_at":"2016-01-20T15:42:02Z","body":"@ntoll done!"}}