|
| 1 | + |
| 2 | +# Copyright (c) 2015 Calin Crisan |
| 3 | +# This file is part of motionPie. |
| 4 | +# |
| 5 | +# motionEye is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +import logging |
| 19 | +import os.path |
| 20 | + |
| 21 | +from config import additional_config |
| 22 | + |
| 23 | + |
| 24 | +CONFIG_TXT = '/boot/config.txt' |
| 25 | + |
| 26 | +OVERCLOCK = { |
| 27 | + 700: '700|250|400|0', |
| 28 | + 800: '800|250|400|0', |
| 29 | + 900: '900|250|450|0', |
| 30 | + 950: '950|250|450|0', |
| 31 | + 1000: '1000|500|600|6' |
| 32 | +} |
| 33 | + |
| 34 | +def _get_board_settings(): |
| 35 | + gpu_mem = 128 |
| 36 | + camera_led = True |
| 37 | + arm_freq = 700 |
| 38 | + |
| 39 | + if os.path.exists(CONFIG_TXT): |
| 40 | + logging.debug('reading board settings from %s' % CONFIG_TXT) |
| 41 | + |
| 42 | + with open(CONFIG_TXT) as f: |
| 43 | + for line in f: |
| 44 | + line = line.strip() |
| 45 | + if not line or line.startswith('#'): |
| 46 | + continue |
| 47 | + |
| 48 | + parts = line.split('=', 1) |
| 49 | + if len(parts) != 2: |
| 50 | + continue |
| 51 | + |
| 52 | + name, value = parts |
| 53 | + name = name.strip() |
| 54 | + value = value.strip() |
| 55 | + |
| 56 | + if name.startswith('gpu_mem'): |
| 57 | + gpu_mem = int(value) |
| 58 | + |
| 59 | + elif name == 'arm_freq': |
| 60 | + arm_freq = int(value) |
| 61 | + |
| 62 | + elif name == 'disable_camera_led': |
| 63 | + camera_led = value == '0' |
| 64 | + |
| 65 | + overclock = OVERCLOCK.get(arm_freq, '700|250|400|0') |
| 66 | + |
| 67 | + s = { |
| 68 | + 'gpuMem': gpu_mem, |
| 69 | + 'overclock': overclock, |
| 70 | + 'cameraLed': camera_led |
| 71 | + } |
| 72 | + |
| 73 | + logging.debug('board settings: gpu_mem=%(gpuMem)s, overclock=%(overclock)s, camera_led=%(cameraLed)s' % s) |
| 74 | + |
| 75 | + return s |
| 76 | + |
| 77 | + |
| 78 | +def _set_board_settings(s): |
| 79 | + s.setdefault('gpuMem', 128) |
| 80 | + s.setdefault('overclock', '700|250|400|0') |
| 81 | + s.setdefault('cameraLed', True) |
| 82 | + |
| 83 | + seen = set() |
| 84 | + |
| 85 | + logging.debug('writing board settings to %s: ' % CONFIG_TXT + |
| 86 | + 'gpu_mem=%(gpuMem)s, overclock=%(overclock)s, camera_led=%(cameraLed)s' % s) |
| 87 | + |
| 88 | + arm_freq, gpu_freq, sdram_freq, over_voltage = s['overclock'].split('|') |
| 89 | + |
| 90 | + lines = [] |
| 91 | + if os.path.exists(CONFIG_TXT): |
| 92 | + with open(CONFIG_TXT) as f: |
| 93 | + lines = f.readlines() |
| 94 | + |
| 95 | + for i, line in enumerate(lines): |
| 96 | + line = line.strip() |
| 97 | + if not line: |
| 98 | + continue |
| 99 | + |
| 100 | + line = line.strip('#') |
| 101 | + |
| 102 | + try: |
| 103 | + name, _ = line.split('=', 1) |
| 104 | + name = name.strip() |
| 105 | + |
| 106 | + except: |
| 107 | + continue |
| 108 | + |
| 109 | + seen.add(name) |
| 110 | + |
| 111 | + if name.startswith('gpu_mem'): |
| 112 | + lines[i] = '%s=%s' % (name, s['gpuMem']) |
| 113 | + |
| 114 | + elif name == 'arm_freq': |
| 115 | + lines[i] = 'arm_freq=%s' % arm_freq |
| 116 | + |
| 117 | + elif name in ['gpu_freq', 'core_freq']: |
| 118 | + lines[i] = '%s=%s' % (name, gpu_freq) |
| 119 | + |
| 120 | + elif name == 'sdram_freq': |
| 121 | + lines[i] = 'sdram_freq=%s' % sdram_freq |
| 122 | + |
| 123 | + elif name == 'over_voltage': |
| 124 | + lines[i] = 'over_voltage=%s' % over_voltage |
| 125 | + |
| 126 | + elif name == 'disable_camera_led': |
| 127 | + lines[i] = 'disable_camera_led=%s' % ['1', '0'][s['cameraLed']] |
| 128 | + |
| 129 | + if 'gpu_mem' not in seen: |
| 130 | + lines.append('gpu_mem=%s' % s['gpuMem']) |
| 131 | + |
| 132 | + if 'gpu_mem_256' not in seen: |
| 133 | + lines.append('gpu_mem_256=%s' % s['gpuMem']) |
| 134 | + |
| 135 | + if 'gpu_mem_512' not in seen: |
| 136 | + lines.append('gpu_mem_512=%s' % s['gpuMem']) |
| 137 | + |
| 138 | + if 'arm_freq' not in seen: |
| 139 | + lines.append('arm_freq=%s' % arm_freq) |
| 140 | + |
| 141 | + if 'gpu_freq' not in seen: |
| 142 | + lines.append('gpu_freq=%s' % gpu_freq) |
| 143 | + |
| 144 | + if 'sdram_freq' not in seen: |
| 145 | + lines.append('sdram_freq=%s' % sdram_freq) |
| 146 | + |
| 147 | + if 'over_voltage' not in seen: |
| 148 | + lines.append('over_voltage=%s' % over_voltage) |
| 149 | + |
| 150 | + if 'disable_camera_led' not in seen: |
| 151 | + lines.append('disable_camera_led=%s' % ['1', '0'][s['cameraLed']]) |
| 152 | + |
| 153 | + logging.debug('remounting /boot read-write') |
| 154 | + if os.system('mount -o remount,rw /boot'): |
| 155 | + logging.error('failed to remount /boot read-write') |
| 156 | + |
| 157 | + with open(CONFIG_TXT, 'w') as f: |
| 158 | + for line in lines: |
| 159 | + if not line.strip(): |
| 160 | + continue |
| 161 | + if not line.endswith('\n'): |
| 162 | + line += '\n' |
| 163 | + f.write(line) |
| 164 | + |
| 165 | + |
| 166 | +@additional_config |
| 167 | +def boardSeparator(): |
| 168 | + return { |
| 169 | + 'type': 'separator', |
| 170 | + 'section': 'expertSettings', |
| 171 | + 'advanced': True |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | +@additional_config |
| 176 | +def gpuMem(): |
| 177 | + return { |
| 178 | + 'label': 'GPU Memory', |
| 179 | + 'description': 'set the amount of memory reserved for the GPU (choose at least 96MB if you use the CSI camera board)', |
| 180 | + 'type': 'number', |
| 181 | + 'min': '16', |
| 182 | + 'max': '448', |
| 183 | + 'section': 'expertSettings', |
| 184 | + 'advanced': True, |
| 185 | + 'reboot': True, |
| 186 | + 'get': _get_board_settings, |
| 187 | + 'set': _set_board_settings, |
| 188 | + 'get_set_dict': True |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | +@additional_config |
| 193 | +def cameraLed(): |
| 194 | + return { |
| 195 | + 'label': 'Enable CSI Camera Led', |
| 196 | + 'description': 'control the led on the CSI camera board', |
| 197 | + 'type': 'bool', |
| 198 | + 'section': 'expertSettings', |
| 199 | + 'advanced': True, |
| 200 | + 'reboot': True, |
| 201 | + 'get': _get_board_settings, |
| 202 | + 'set': _set_board_settings, |
| 203 | + 'get_set_dict': True |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | +@additional_config |
| 208 | +def overclock(): |
| 209 | + return { |
| 210 | + 'label': 'Overclocking', |
| 211 | + 'description': 'choose an overclocking preset for your Raspberry PI', |
| 212 | + 'type': 'choices', |
| 213 | + 'choices': [ |
| 214 | + ('700|250|400|0', 'none (700/250/400/0)'), |
| 215 | + ('800|250|400|0', 'modest (800/250/400/0)'), |
| 216 | + ('900|250|450|0', 'medium (900/250/450/0)'), |
| 217 | + ('950|250|450|0', 'high (950/250/450/0)'), |
| 218 | + ('1000|500|600|6', 'turbo (1000/500/600/6)') |
| 219 | + ], |
| 220 | + 'section': 'expertSettings', |
| 221 | + 'advanced': True, |
| 222 | + 'reboot': True, |
| 223 | + 'get': _get_board_settings, |
| 224 | + 'set': _set_board_settings, |
| 225 | + 'get_set_dict': True |
| 226 | + } |
| 227 | + |
0 commit comments