| 
									
										
										
										
											2017-06-05 16:02:39 +03:00
										 |  |  | # Copyright (c) 2014-present PlatformIO <contact@platformio.org> | 
					
						
							| 
									
										
										
										
											2015-11-18 17:16:17 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #    http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import click | 
					
						
							| 
									
										
										
										
											2019-08-17 20:55:16 +03:00
										 |  |  | from tabulate import tabulate | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  | from platformio import fs | 
					
						
							| 
									
										
										
										
											2019-06-05 17:57:22 +03:00
										 |  |  | from platformio.compat import dump_json_to_unicode | 
					
						
							| 
									
										
										
										
											2016-05-26 19:43:36 +03:00
										 |  |  | from platformio.managers.platform import PlatformManager | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 23:29:17 +02:00
										 |  |  | @click.command("boards", short_help="Embedded Board Explorer") | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | @click.argument("query", required=False) | 
					
						
							| 
									
										
										
										
											2016-05-26 19:43:36 +03:00
										 |  |  | @click.option("--installed", is_flag=True) | 
					
						
							| 
									
										
										
										
											2015-01-10 16:20:09 +02:00
										 |  |  | @click.option("--json-output", is_flag=True) | 
					
						
							| 
									
										
										
										
											2016-05-26 19:43:36 +03:00
										 |  |  | def cli(query, installed, json_output):  # pylint: disable=R0912 | 
					
						
							| 
									
										
										
										
											2015-01-10 16:20:09 +02:00
										 |  |  |     if json_output: | 
					
						
							| 
									
										
										
										
											2017-03-03 23:29:17 +02:00
										 |  |  |         return _print_boards_json(query, installed) | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     grpboards = {} | 
					
						
							| 
									
										
										
										
											2016-05-26 19:43:36 +03:00
										 |  |  |     for board in _get_boards(installed): | 
					
						
							| 
									
										
										
										
											2017-03-03 23:29:17 +02:00
										 |  |  |         if query and query.lower() not in json.dumps(board).lower(): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         if board["platform"] not in grpboards: | 
					
						
							|  |  |  |             grpboards[board["platform"]] = [] | 
					
						
							|  |  |  |         grpboards[board["platform"]].append(board) | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 23:29:17 +02:00
										 |  |  |     terminal_width, _ = click.get_terminal_size() | 
					
						
							|  |  |  |     for (platform, boards) in sorted(grpboards.items()): | 
					
						
							| 
									
										
										
										
											2015-04-20 17:20:27 +01:00
										 |  |  |         click.echo("") | 
					
						
							|  |  |  |         click.echo("Platform: ", nl=False) | 
					
						
							|  |  |  |         click.secho(platform, bold=True) | 
					
						
							| 
									
										
										
										
											2019-08-17 20:55:16 +03:00
										 |  |  |         click.echo("=" * terminal_width) | 
					
						
							| 
									
										
										
										
											2017-03-03 23:29:17 +02:00
										 |  |  |         print_boards(boards) | 
					
						
							| 
									
										
										
										
											2017-12-15 22:16:37 +02:00
										 |  |  |     return True | 
					
						
							| 
									
										
										
										
											2014-12-27 23:59:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 23:29:17 +02:00
										 |  |  | def print_boards(boards): | 
					
						
							|  |  |  |     click.echo( | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         tabulate( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 ( | 
					
						
							|  |  |  |                     click.style(b["id"], fg="cyan"), | 
					
						
							|  |  |  |                     b["mcu"], | 
					
						
							|  |  |  |                     "%dMHz" % (b["fcpu"] / 1000000), | 
					
						
							|  |  |  |                     fs.format_filesize(b["rom"]), | 
					
						
							|  |  |  |                     fs.format_filesize(b["ram"]), | 
					
						
							|  |  |  |                     b["name"], | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 for b in boards | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             headers=["ID", "MCU", "Frequency", "Flash", "RAM", "Name"], | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2016-05-26 19:43:36 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _get_boards(installed=False): | 
					
						
							| 
									
										
										
										
											2017-09-03 22:40:00 +03:00
										 |  |  |     pm = PlatformManager() | 
					
						
							|  |  |  |     return pm.get_installed_boards() if installed else pm.get_all_boards() | 
					
						
							| 
									
										
										
										
											2016-05-28 22:51:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-06 20:57:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 23:29:17 +02:00
										 |  |  | def _print_boards_json(query, installed=False): | 
					
						
							| 
									
										
										
										
											2016-05-26 19:43:36 +03:00
										 |  |  |     result = [] | 
					
						
							| 
									
										
										
										
											2017-09-03 22:40:00 +03:00
										 |  |  |     for board in _get_boards(installed): | 
					
						
							| 
									
										
										
										
											2015-01-10 20:10:48 +02:00
										 |  |  |         if query: | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             search_data = "%s %s" % (board["id"], json.dumps(board).lower()) | 
					
						
							| 
									
										
										
										
											2015-01-10 20:10:48 +02:00
										 |  |  |             if query.lower() not in search_data.lower(): | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2016-05-26 19:43:36 +03:00
										 |  |  |         result.append(board) | 
					
						
							| 
									
										
										
										
											2019-06-05 17:57:22 +03:00
										 |  |  |     click.echo(dump_json_to_unicode(result)) |