def generate_csv_events(rising_locations, falling_locations, timebase, writer):
  """
  Generate CSV formatted events similar to Scaramuzze et al: http://rpg.ifi.uzh.ch/davis_data.html
  :param rising_locations: A list of pixel locations where rising threshold was triggered
  :param falling_locations: A list of pixel locations where falling threshold was triggered
  :param timebase: The last known time before events triggered
  :param writer: A CSV file writer
  :return: Updated timestamp
  """
  rising_count =  len(rising_locations)
  falling_count = len(falling_locations)
  ultimax = max(rising_count, falling_count)
  timestamp = timebase

  for idx in range(0,ultimax):
    row_list = []
    if idx < rising_count:
      timestamp += 0.000001 # 1E-6
      coord = rising_locations[idx][0]
      row = ['{0:.9f}'.format(timestamp) , coord[0], coord[1], 1]
      row_list.append(row)
    if idx < falling_count:
      timestamp += 0.000001 # 1E-6
      coord = falling_locations[idx][0]
      row = ['{0:.9f}'.format(timestamp) , coord[0], coord[1], 0]
      row_list.append(row)

    writer.writerows(row_list)

  return timestamp