mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 """
elessair 0:f269e3021894 2 mbed SDK
elessair 0:f269e3021894 3 Copyright (c) 2011-2013 ARM Limited
elessair 0:f269e3021894 4
elessair 0:f269e3021894 5 Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 6 you may not use this file except in compliance with the License.
elessair 0:f269e3021894 7 You may obtain a copy of the License at
elessair 0:f269e3021894 8
elessair 0:f269e3021894 9 http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 10
elessair 0:f269e3021894 11 Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 12 distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 14 See the License for the specific language governing permissions and
elessair 0:f269e3021894 15 limitations under the License.
elessair 0:f269e3021894 16 """
elessair 0:f269e3021894 17
elessair 0:f269e3021894 18 import re
elessair 0:f269e3021894 19 import sys
elessair 0:f269e3021894 20 import uuid
elessair 0:f269e3021894 21 from sys import stdout
elessair 0:f269e3021894 22 from socket import socket, AF_INET, SOCK_DGRAM
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 class UDPEchoServerTest():
elessair 0:f269e3021894 25 ECHO_SERVER_ADDRESS = ""
elessair 0:f269e3021894 26 ECHO_PORT = 0
elessair 0:f269e3021894 27 s = None # Socket
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)"
elessair 0:f269e3021894 30 re_detect_server_ip = re.compile(PATTERN_SERVER_IP)
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 def test(self, selftest):
elessair 0:f269e3021894 33 result = True
elessair 0:f269e3021894 34 serial_ip_msg = selftest.mbed.serial_readline()
elessair 0:f269e3021894 35 if serial_ip_msg is None:
elessair 0:f269e3021894 36 return selftest.RESULT_IO_SERIAL
elessair 0:f269e3021894 37 selftest.notify(serial_ip_msg)
elessair 0:f269e3021894 38 # Searching for IP address and port prompted by server
elessair 0:f269e3021894 39 m = self.re_detect_server_ip.search(serial_ip_msg)
elessair 0:f269e3021894 40 if m and len(m.groups()):
elessair 0:f269e3021894 41 self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4])
elessair 0:f269e3021894 42 self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method
elessair 0:f269e3021894 43 selftest.notify("HOST: UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT))
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 # We assume this test fails so can't send 'error' message to server
elessair 0:f269e3021894 46 try:
elessair 0:f269e3021894 47 self.s = socket(AF_INET, SOCK_DGRAM)
elessair 0:f269e3021894 48 except Exception, e:
elessair 0:f269e3021894 49 self.s = None
elessair 0:f269e3021894 50 selftest.notify("HOST: Socket error: %s"% e)
elessair 0:f269e3021894 51 return selftest.RESULT_ERROR
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 for i in range(0, 100):
elessair 0:f269e3021894 54 TEST_STRING = str(uuid.uuid4())
elessair 0:f269e3021894 55 self.s.sendto(TEST_STRING, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT))
elessair 0:f269e3021894 56 data = self.s.recv(len(TEST_STRING))
elessair 0:f269e3021894 57 received_str = repr(data)[1:-1]
elessair 0:f269e3021894 58 if TEST_STRING != received_str:
elessair 0:f269e3021894 59 result = False
elessair 0:f269e3021894 60 break
elessair 0:f269e3021894 61 sys.stdout.write('.')
elessair 0:f269e3021894 62 stdout.flush()
elessair 0:f269e3021894 63 else:
elessair 0:f269e3021894 64 result = False
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 if self.s is not None:
elessair 0:f269e3021894 67 self.s.close()
elessair 0:f269e3021894 68 return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE