Mouse code for the MacroRat
mbed-dev/drivers/FileSystemLike.cpp@32:69acb14778ea, 2017-05-26 (annotated)
- Committer:
- kyleliangus
- Date:
- Fri May 26 03:46:03 2017 +0000
- Revision:
- 32:69acb14778ea
- Parent:
- 18:6a4db94011d3
PID values still wonky. IR PID vs Encoder PID also wonky
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | #include "drivers/FileSystemLike.h" |
sahilmgandhi | 18:6a4db94011d3 | 17 | |
sahilmgandhi | 18:6a4db94011d3 | 18 | namespace mbed { |
sahilmgandhi | 18:6a4db94011d3 | 19 | |
sahilmgandhi | 18:6a4db94011d3 | 20 | class BaseDirHandle : public DirHandle { |
sahilmgandhi | 18:6a4db94011d3 | 21 | public: |
sahilmgandhi | 18:6a4db94011d3 | 22 | /* |
sahilmgandhi | 18:6a4db94011d3 | 23 | We keep track of our current location as the n'th object in the |
sahilmgandhi | 18:6a4db94011d3 | 24 | FileSystemLike list. Using a Base* instead would cause problems if that |
sahilmgandhi | 18:6a4db94011d3 | 25 | object were to be destroyed between readdirs. |
sahilmgandhi | 18:6a4db94011d3 | 26 | Using this method does mean though that destroying/creating objects can |
sahilmgandhi | 18:6a4db94011d3 | 27 | give unusual results from readdir. |
sahilmgandhi | 18:6a4db94011d3 | 28 | */ |
sahilmgandhi | 18:6a4db94011d3 | 29 | off_t n; |
sahilmgandhi | 18:6a4db94011d3 | 30 | struct dirent cur_entry; |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | BaseDirHandle() : DirHandle(0), n(0), cur_entry() { |
sahilmgandhi | 18:6a4db94011d3 | 33 | } |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | virtual int closedir() { |
sahilmgandhi | 18:6a4db94011d3 | 36 | // No lock can be used in destructor |
sahilmgandhi | 18:6a4db94011d3 | 37 | delete this; |
sahilmgandhi | 18:6a4db94011d3 | 38 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 39 | } |
sahilmgandhi | 18:6a4db94011d3 | 40 | |
sahilmgandhi | 18:6a4db94011d3 | 41 | virtual struct dirent *readdir() { |
sahilmgandhi | 18:6a4db94011d3 | 42 | lock(); |
sahilmgandhi | 18:6a4db94011d3 | 43 | FileBase *ptr = FileBase::get(n); |
sahilmgandhi | 18:6a4db94011d3 | 44 | if (ptr == NULL) { |
sahilmgandhi | 18:6a4db94011d3 | 45 | unlock(); |
sahilmgandhi | 18:6a4db94011d3 | 46 | return NULL; |
sahilmgandhi | 18:6a4db94011d3 | 47 | } |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | /* Increment n, so next readdir gets the next item */ |
sahilmgandhi | 18:6a4db94011d3 | 50 | n++; |
sahilmgandhi | 18:6a4db94011d3 | 51 | |
sahilmgandhi | 18:6a4db94011d3 | 52 | /* Setup cur entry and return a pointer to it */ |
sahilmgandhi | 18:6a4db94011d3 | 53 | std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX); |
sahilmgandhi | 18:6a4db94011d3 | 54 | unlock(); |
sahilmgandhi | 18:6a4db94011d3 | 55 | return &cur_entry; |
sahilmgandhi | 18:6a4db94011d3 | 56 | } |
sahilmgandhi | 18:6a4db94011d3 | 57 | |
sahilmgandhi | 18:6a4db94011d3 | 58 | virtual off_t telldir() { |
sahilmgandhi | 18:6a4db94011d3 | 59 | lock(); |
sahilmgandhi | 18:6a4db94011d3 | 60 | off_t offset = n; |
sahilmgandhi | 18:6a4db94011d3 | 61 | unlock(); |
sahilmgandhi | 18:6a4db94011d3 | 62 | return offset; |
sahilmgandhi | 18:6a4db94011d3 | 63 | } |
sahilmgandhi | 18:6a4db94011d3 | 64 | |
sahilmgandhi | 18:6a4db94011d3 | 65 | virtual void seekdir(off_t offset) { |
sahilmgandhi | 18:6a4db94011d3 | 66 | lock(); |
sahilmgandhi | 18:6a4db94011d3 | 67 | n = offset; |
sahilmgandhi | 18:6a4db94011d3 | 68 | unlock(); |
sahilmgandhi | 18:6a4db94011d3 | 69 | } |
sahilmgandhi | 18:6a4db94011d3 | 70 | |
sahilmgandhi | 18:6a4db94011d3 | 71 | virtual void rewinddir() { |
sahilmgandhi | 18:6a4db94011d3 | 72 | lock(); |
sahilmgandhi | 18:6a4db94011d3 | 73 | n = 0; |
sahilmgandhi | 18:6a4db94011d3 | 74 | unlock(); |
sahilmgandhi | 18:6a4db94011d3 | 75 | } |
sahilmgandhi | 18:6a4db94011d3 | 76 | |
sahilmgandhi | 18:6a4db94011d3 | 77 | protected: |
sahilmgandhi | 18:6a4db94011d3 | 78 | PlatformMutex _mutex; |
sahilmgandhi | 18:6a4db94011d3 | 79 | |
sahilmgandhi | 18:6a4db94011d3 | 80 | virtual void lock() { |
sahilmgandhi | 18:6a4db94011d3 | 81 | _mutex.lock(); |
sahilmgandhi | 18:6a4db94011d3 | 82 | } |
sahilmgandhi | 18:6a4db94011d3 | 83 | |
sahilmgandhi | 18:6a4db94011d3 | 84 | virtual void unlock() { |
sahilmgandhi | 18:6a4db94011d3 | 85 | _mutex.unlock(); |
sahilmgandhi | 18:6a4db94011d3 | 86 | } |
sahilmgandhi | 18:6a4db94011d3 | 87 | }; |
sahilmgandhi | 18:6a4db94011d3 | 88 | |
sahilmgandhi | 18:6a4db94011d3 | 89 | FileSystemLike::FileSystemLike(const char *name) : FileBase(name, FileSystemPathType) { |
sahilmgandhi | 18:6a4db94011d3 | 90 | |
sahilmgandhi | 18:6a4db94011d3 | 91 | } |
sahilmgandhi | 18:6a4db94011d3 | 92 | |
sahilmgandhi | 18:6a4db94011d3 | 93 | FileSystemLike::~FileSystemLike() { |
sahilmgandhi | 18:6a4db94011d3 | 94 | |
sahilmgandhi | 18:6a4db94011d3 | 95 | } |
sahilmgandhi | 18:6a4db94011d3 | 96 | |
sahilmgandhi | 18:6a4db94011d3 | 97 | DirHandle *FileSystemLike::opendir() { |
sahilmgandhi | 18:6a4db94011d3 | 98 | return new BaseDirHandle(); |
sahilmgandhi | 18:6a4db94011d3 | 99 | } |
sahilmgandhi | 18:6a4db94011d3 | 100 | |
sahilmgandhi | 18:6a4db94011d3 | 101 | } // namespace mbed |