<commit_before>"""
 PyBBIO - bbio.py - v0.4
 Created: 12/2011
 Author: <NAME> - <EMAIL> - www.alexanderhiam.com
 Website: https://github.com/alexanderhiam/PyBBIO

 A Python library for hardware IO support on the TI Beaglebone.
 Currently only supporting basic digital and analog IO, but more 
 peripheral support is on the way, so keep checking the Github page
 for updates.

 16-bit register support mod from sbma44 - https://github.com/sbma44

 Copyright 2012 <NAME>

 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.
"""

import sys

try:
  from mmap import mmap
except:
  print "\n mmap module not found; to install:\n\
   # opkg update && opkg install python-mmap\n"
  sys.exit(0)
try:
  import serial
except:
  print "\n pyserial module not found; to install:\n\
   # opkg update && opkg install python-pyserial\n"
  sys.exit(0)
import struct, os, time

from config import *

sys.path.append(LIBRARIES_PATH)

ADDITIONAL_CLEANUP = [] # See add_cleanup() below.
START_TIME_MS = 0 # Set in run() - used by millis() and micros().


# Create global mmap:
MEM_FILE = "/dev/mem"
with open(MEM_FILE, "r+b") as f:
  __mmap = mmap(f.fileno(), MMAP_SIZE, offset=MMAP_OFFSET)


def run(setup, main):
  """ The main loop; must be passed a setup and a main function.
      First the setup function will be called once, then the main
      function wil be continuously until a stop signal is raised, 
      e.g. CTRL-C or a call to the stop() function from within the
      main function. """
  global START_TIME_MS
  START_TIME_MS = time.time()*1000
  try:
    bbio_init()
    setup()
    while (True):
      main()
  except KeyboardInterrupt:
    # Manual exit signal, clean up and exit happy
    bbio_cleanup()
  except Exception, e:
    # Something may have gone wrong, clean up and print exception
    bbio_cleanup()
    print e
      
def stop():
  """ Preffered way for a program to stop itself. """
  raise KeyboardInterrupt # Expected happy stop condition in run()

def bbio_init():
  """ Pre-run initialization, i.e. starting module clocks, etc. """
  _analog_init()

def _analog_init():
  """ Initializes the on-board 8ch 12bit ADC. """
  # Enable ADC module clock, though should already be enabled on
  # newer Angstrom images:
  _setReg(CM_WKUP_ADC_TSC_CLKCTRL, MODULEMODE_ENABLE)
  # Wait for enable complete:
  while (_getReg(CM_WKUP_ADC_TSC_CLKCTRL) & IDLEST_MASK): time.sleep(0.1)

  # Software reset:
  _setReg(ADC_SYSCONFIG, ADC_SOFTRESET)
  while(_getReg(ADC_SYSCONFIG) & ADC_SOFTRESET): pass

  # Make sure STEPCONFIG write protect is off:
  _setReg(ADC_CTRL, ADC_STEPCONFIG_WRITE_PROTECT_OFF)

  # Set STEPCONFIG1-STEPCONFIG8 to correspond to ADC inputs 0-7:
  for i in xrange(8):
    config = SEL_INP('AIN%i' % i)
    _setReg(eval('ADCSTEPCONFIG%i' % (i+1)), config)
  # Now we can enable ADC subsystem, leaving write protect off:
  _orReg(ADC_CTRL, TSC_ADC_SS_ENABLE)


def bbio_cleanup():
  """ Post-run cleanup, i.e. stopping module clocks, etc. """
  # Run user cleanup routines:
  for cleanup in ADDITIONAL_CLEANUP:
    try:
      cleanup()<commit_msg>added ADC step delay and sample averaging for slightly better accuracy<commit_after>"""
 PyBBIO - bbio.py - v0.4
 Created: 12/2011
 Author: <NAME> - <EMAIL> - www.alexanderhiam.com
 Website: https://github.com/alexanderhiam/PyBBIO

 A Python library for hardware IO support on the TI Beaglebone.
 Currently only supporting basic digital and analog IO, but more 
 peripheral support is on the way, so keep checking the Github page
 for updates.

 16-bit register support mod from sbma44 - https://github.com/sbma44

 Copyright 2012 <NAME>

 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.
"""

import sys

try:
  from mmap import mmap
except:
  print "\n mmap module not found; to install:\n\
   # opkg update && opkg install python-mmap\n"
  sys.exit(0)
try:
  import serial
except:
  print "\n pyserial module not found; to install:\n\
   # opkg update && opkg install python-pyserial\n"
  sys.exit(0)
import struct, os, time

from config import *

sys.path.append(LIBRARIES_PATH)

ADDITIONAL_CLEANUP = [] # See add_cleanup() below.
START_TIME_MS = 0 # Set in run() - used by millis() and micros().


# Create global mmap:
MEM_FILE = "/dev/mem"
with open(MEM_FILE, "r+b") as f:
  __mmap = mmap(f.fileno(), MMAP_SIZE, offset=MMAP_OFFSET)


def run(setup, main):
  """ The main loop; must be passed a setup and a main function.
      First the setup function will be called once, then the main
      function wil be continuously until a stop signal is raised, 
      e.g. CTRL-C or a call to the stop() function from within the
      main function. """
  global START_TIME_MS
  START_TIME_MS = time.time()*1000
  try:
    bbio_init()
    setup()
    while (True):
      main()
  except KeyboardInterrupt:
    # Manual exit signal, clean up and exit happy
    bbio_cleanup()
  except Exception, e:
    # Something may have gone wrong, clean up and print exception
    bbio_cleanup()
    print e
      
def stop():
  """ Preffered way for a program to stop itself. """
  raise KeyboardInterrupt # Expected happy stop condition in run()

def bbio_init():
  """ Pre-run initialization, i.e. starting module clocks, etc. """
  _analog_init()

def _analog_init():
  """ Initializes the on-board 8ch 12bit ADC. """
  # Enable ADC module clock, though should already be enabled on
  # newer Angstrom images:
  _setReg(CM_WKUP_ADC_TSC_CLKCTRL, MODULEMODE_ENABLE)
  # Wait for enable complete:
  while (_getReg(CM_WKUP_ADC_TSC_CLKCTRL) & IDLEST_MASK): time.sleep(0.1)

  # Software reset:
  _setReg(ADC_SYSCONFIG, ADC_SOFTRESET)
  while(_getReg(ADC_SYSCONFIG) & ADC_SOFTRESET): pass

  # Make sure STEPCONFIG write protect is off:
  _setReg(ADC_CTRL, ADC_STEPCONFIG_WRITE_PROTECT_OFF)

  # Set STEPCONFIG1-STEPCONFIG8 to correspond to ADC inputs 0-7:
  for i in xrange(8):
    config = SEL_INP('AIN%i' % i) | ADC_AVG2
    _setReg(eval('ADCSTEPCONFIG%i' % (i+1)), config)
    _setReg(eval('ADCSTEPDELAY%i' % (i+1)), SAMPLE_DELAY(15))
  # Now we can enable ADC subsystem, leaving write protect off:
  _orReg(ADC_CTRL, TSC_ADC_SS_ENABLE)


def bbio_cleanup():
  """ Post-run cleanup, i.e. stopping module clocks, etc. """
  # Run user cleanup routines:
  for cleanup in ADDITIONAL_CLEANUP:
    try:
      cleanup()
{'fraction_non_alphanumeric': 0.09165053912081836, 'fraction_numerical': 0.011611833010782417, 'mean_word_length': 3.7504924491135916, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7075768', 'n_tokens_mistral': 2394, 'n_tokens_neox': 2296, 'n_words': 857}