/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "breakpoint.h"

void breakpointListInit(breakpointlist_t *list) {
  list->breakpointCount = 0;
  list->user = NULL;
  list->onBreakpoint = NULL;
  list->breakpointCurrent = 0x00;
}

uint8_t breakpointGet(breakpointlist_t *list, float width) {
  uint8_t bp, i;
  bp = 0xFF;
  for(i = 0; i < list->breakpointCount; i++) {
    if(list->breakpoints[i] > width) break;
    bp = i;
  }
  if(bp == 0xFF) bp = 0;
  return bp;
}

void breakpointAdd(breakpointlist_t *list, float width) {
  list->breakpoints[list->breakpointCount++] = width;
}

void breakpointResize(breakpointlist_t *list, float width) {
  uint8_t bp;

  // Determine breakpoint
  bp = breakpointGet(list, width);
  if(list->breakpointCurrent == bp) return;
  list->breakpointCurrent = bp;

  // Fire event.
  if(list->onBreakpoint != NULL) {
    list->onBreakpoint(list->user, bp, list->breakpoints[bp]);
  }
}